數(shù)組
使用括號(hào)及空格來賦值數(shù)組妨退。
#!/bin/bash
user_array=("user01" "user02" "user03")
user_array_length=`echo ${#user_array[@]}`
echo "user array length : $user_array_length"
正則
使用=~
判斷正則關(guān)系。
#!/bin/bash
user_name=$1
group_name=$2
if [[ $user_name =~ ^[a-z0-9_-]{1,32}$ && $group_name =~ ^[a-z0-9_-]{1,32}$ ]]
then
echo "user_name: $user_name, group_name: $group_name valid!"
else
echo "user_name: $user_name, group_name: $group_name invalid!"
exit 1
fi
包含關(guān)系
構(gòu)建一個(gè)包含判斷的方法進(jìn)行使用锭魔。
#!/bin/bash
user_name=$1
user_array=("user01" "user02" "user03")
user_array_length=`echo ${#user_array[@]}`
echo "user array length : $user_array_length"
# 判斷包含關(guān)系
function contains() {
local n=$#
local value=${!n}
for ((i=1;i < $#; i++)) {
if [ "${!i}" == "${value}" ]; then
echo "y"
retrun 0
fi
}
echo "n"
return 1
}
# judge
if [ $(contains "${user_array[@]" "$user_name") == "y" ];
then
echo "user_name: $user_name may be contains in array, please check it!"
exit 1
fi