标签:shell 编程 脚本 命令 语句
一些常用的Shell编程(脚本)命令和语句,可以满足一般需求。
接收到的命令参数:
命令本身:$0
第一个参数:$1
第二个参数:$2
……
退出命令:
exit
echo命令:
echo -n "请选择(y/n)?"
echo "欢迎使用\"正式服务器\"部署工具。"
echo "即将部署项目:$project_name"
echo -e "first\tsecond"
追加到文件尾:echo -e $log_info >> deploy.log
覆盖文件内容:echo -e $log_info > deploy.log
printf命令:
(可以代替echo,格式化输出,和C语言中的printf函数功能相同)
printf "The number is %.2f.\n" 100
左对齐:printf "%-20s %-15s %10.2f\n" "Stephen" "Liu" 35
右对齐:printf "|%10s|\n" hello
变量:
log_info="$log_info,$target_ip"
数组:
servers=("192.168.0.31" "192.168.0.39")
projects=("public" "industry" "logistics" "misc")
${projects[*]}
${#servers[@]}
if 语句(条件判断同样适用于while语句和for语句):
if [ $project ];then
echo "Variable \"project\" is null."
fi
if [ $# -ne 3 ];then
echo "命令行参数不是3个"
fi
if [ "$is_ip_correct" = false ];then
echo "无效的ip地址,请使用下面ip之一:"
echo ${servers[*]}
exit
fi
if [ "$confirm" == "y" ] && [ "$confirm" != "n" ];do
# do something...
fi
if [[ $1 =~ ^public|industry$ ]] && [[ $3 =~ ^[yn]$ ]];then
# do something...
fi
if [[ ! $deploy_more =~ [yn] ]];then
# do something...
fi
if [ ! -f target/$project.war ];then
# do something...
fi
if [ -d $2/webapps/$1 ];then
# do something...
fi
backup_file="/backup/java_data/$1-$today.war"
backup_file="/backup/java_data/$1-$today.gz"
if [ "${backup_file##*.}" = "war" ];then
cp $backup_file $1.war
elif [ "${backup_file##*.}" = "gz" ];then
tar zxvf $backup_file
else
echo "备份文件格式不对"
exit
fi
read 语句(读取用户输入的字符串):
- 最简单的用法:读取用户输入到变量yes_or_no
read yes_or_no
read -e -p "是否备份:(y/n)?" -i "y" needbackup
(参数说明:-e:不知有什么用,但如果去掉了,-i就失效了;-p:后面接着提示语句;-i:后面接着缺省输入;最后一个参数是保存用户输入的变量。)
select 语句(提示用户从列表中选择一个):
select project in ${projects[*]};do
if [ $project ];then
break
fi
done
或加上退出条件:
select target_ip in ${servers[*]} "Exit(退出)";do
if [ "$target_ip" = "Exit(退出)" ]; then
echo "谢谢使用!Good-Bye!"
break
fi
if [ $target_ip ]; then
# do something
fi
done
case 语句:
case $project in
public) project_name="大众版" ;;
logistics) project_name="配送版"
;;
misc) project_name="杂项版" ;;
esac
while 语句:
while [[ ! $needbackup =~ ^[yn]$ ]];do
read -e -p "是否备份:(y/n)?" -i "y" needbackup
done
echo -n "等待$port 端口打开……"
while [ ! $pid_new ];do
#sleep 1
pid_new=`netstat -nlp | grep $port | awk ‘{print $7}‘ | awk -F "/" ‘{print $1}‘`
echo -n "…"
done
for语句:
for ip_t in ${servers[*]};do
if [ "$2" = "$ip_t" ];then
is_ip_correct=true
break
fi
done
调用其他程序:
PS3="请选择一个分支:"
select branch in `svn list svn://<svn_host>/java/code/branches`;do
if [ $branch ];then
svn_dir="svn://<svn_host>/java/code/branches/$branch"
project_dir=$branch
break
fi
done
today=$(date +%Y-%m-%d/%H:%M:%S)
svn_version=$(svn info $svn_dir | grep "Last Changed Rev:" | awk ‘{print $4}‘)
- 无声地调用其他程序(不输出结果,即把结果输出到一个null设备中)
rm $2/logs/m* $2/logs/l* $2/logs/h* $2/logs/catalina.2015* &>/dev/null &
awk(用来对字符串进行切片处理,一般配合grep使用):
svn_version=$(svn info $svn_dir | grep "Last Changed Rev:" | awk ‘{print $4}‘)
awk -F "/" ‘{print $1}‘
for pid in `netstat -nlp | grep $port | awk ‘{print $7}‘ | awk -F "/" ‘{print $1}‘`
do
echo "==========================Warning========================"
echo "无法正常关闭进程,端口:$port,直接kill掉,进程号:$pid"
echo "==========================Warning========================"
kill $pid
done
(原创文章,转载请注明转自Clement-Xu的博客)
版权声明:本文为原创文章,转载请注明转自Clement-Xu的csdn博客。
Shell编程(脚本)的常用命令和语句
标签:shell 编程 脚本 命令 语句
原文地址:http://blog.csdn.net/clementad/article/details/46793827