数组
使用括号及空格来赋值数组。
#!/bin/bash
user_array=("user01" "user02" "user03")
user_array_length=`echo ${#user_array[@]}`
echo "user array length : $user_array_length"
正则
使用=~
判断正则关系。
#!/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
包含关系
构建一个包含判断的方法进行使用。
#!/bin/bash
user_name=$1
user_array=("user01" "user02" "user03")
user_array_length=`echo ${#user_array[@]}`
echo "user array length : $user_array_length"
# 判断包含关系
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