1.判断字符串长度是否为0
delete="test"
if [ ! -z $delete ]
then
echo "delete is not NULL"
exit 1
else
echo "delete is NULL"
fi
2.比较
2.1 判断文件是否存在
if [ -f "vasys_version.tmp" ]; then
rm vasys_version.tmp
fi
2.2 判断文件夹是否存在
if [ -d "/usr/local" ]; then
echo "/usr/local is exit"
fi
4.数组定义及遍历
NS_DO_PATH="ns_inc/ ns_windows/ ns_centos/"
for DOPATH in $NS_DO_PATH; do
if [ -d $DOPATH ]; then
echo $DOPATH
fi
done
5.函数入参操作
TYPE_ALONE="alone_tag"
TYPE_WITH="with_tag"
show_usage="args: [-t , -b, -i, -d ]\
[--type=, --base=]"
#参数
opt_type=""
opt_base=""
opt_include=""
opt_delete=""
GETOPT_ARGS=`getopt -o t:d:i:b: -al type:,base:,include:,delete: -- "$@"`
eval set -- "$GETOPT_ARGS"
if [ $# -lt 2 ]
then
echo "usege param error!!"
exit 1
fi
#获取参数
while [ -n "$1" ]
do
case "$1" in
-t|--type)
opt_type=$2;
echo $opt_type
shift 2;;
-d|--base)
delete=$2;
echo "delete file $delete"
shift 2;;
-i|--include)
opt_include=$2;
echo "add path $opt_include"
shift 2;;
-b|--base)
opt_base=$2;
echo $opt_base
shift 2;;
--) break ;;
*) echo $1,$2,$show_usage; break ;;
esac
done
运行:./test_shell.sh -t alone_tag -d /usr/local -i test -b SE