Bash Shell 常用例子

25 篇文章 0 订阅
定义一个数组,并打印出列表让用户选择其中一个值:
# 定义:
projects=("svc-manager" "admin" "sms" "log-monitor")

# 列出projects中的所有值给用户选择,选择结果存放在project变量中。PS3是系统提示语,默认值是"#?"
PS3="请选择一个项目:"
select project in ${projects[*]};do
  if [ $project ];then
    break
  fi
done

定义一个map对象,并列出keys给用户选择:
# 定义:
declare -A projects=(["registry"]="注册中心" ["user-service"]="用户服务")

# 列出所有keys给用户选择,选择结果存放在project中。PS3是系统提示语,默认值是"#?"
PS3="请选择一个项目:"
select project in ${!projects[*]} "Exit(退出)";do
  if [ "$project" = "Exit(退出)" ]; then
    echo "谢谢使用!Good-Bye!"
    exit
  fi
  if [ $project ];then
    break
  fi
done

# 获取被选择的key的value:
project_name=${projects[$project]}

列出某个目录中的所有文件,让用户选择一个:
select file in ${backup_dir}/${project}/* ;do
  echo $file
done

开始运行脚本时加个锁,避免两个人同时运行:
#加锁:
LOCKFILE="/opt/shell/spring-boot-deploy-git.lock"
#用 trap 命令设置一个对信号的监听器
#程序运行中当监听到信号 1,2,3,9,15 20就会删除锁文件,并退出脚本执行
#比如说,当脚本自行运行结束、被用户 Ctrl+C 掉、被 Kill 掉、终端被关闭
#系统关机或重启的情况下,都需将临时文件删除,否则脚本以后都没机会执行
#运行 trap -l 查看到所以信号
trap 'echo "\n解锁成功。";rm -f ${LOCKFILE}; exit' 1 2 3 9 15 20
if [ -f $LOCKFILE ]; then
  echo "另外一个人正在部署,请稍后再试!"
  echo "如果发生了异常,请清除锁文件后再试:rm -f ${LOCKFILE}"
  exit 0
fi
touch $LOCKFILE #touch新建一个锁文件
chmod 600 $LOCKFILE #把文件属性改为只建立者可读写

#在所有退出的地方解锁:
echo 谢谢使用!
rm -f ${LOCKFILE}
exit

发生错误时自动重试:
echo 开始从git服务器的master分支中pull代码...
git pull
while [ ! $? -eq 0 ];do#“$?”是上个命令的返回值,正常的话值是“0”
  echo "git pull 发生错误,自动重试……"
  git pull
done;

获取当前时间并格式化:
today=$(date +%Y-%m-%d-%H-%M-%S)
echo $today

文本中使用制表符,并输出到控制台和文件(使用“-e”参数是制表符生效):
log_info="$(date +%Y-%m-%d/%H:%M:%S)\tgitVersion:$git_version\t$project_name($project)\tserver:"
echo -e $log_info
echo -e $log_info >> /opt/web/maven/$deploy_log_file

删除字符串的最后一个字符:
log_info=${log_info::-1}
或如果知道最后那个字符是什么(假设是“,”):
list=${list%","}

使用ssh执行远端服务器的命令行:
#重启远端服务器的某个service:
/usr/bin/ssh -l root -p $ssh_port $target_ip "systemctl restart ${project}.service"

使用ssh拷贝本地文件到远端服务器:
# rsync方式:
/usr/bin/rsync -avzuP -e "ssh -p $ssh_port" target/$project.jar $target_ip:$spring_boot_dir/
# scp方式:
/usr/bin/scp -P $ssh_port $file $target_ip:$spring_boot_dir/$project.jar

布尔变量的定义和使用:
need_backup=true
if [ "$need_backup" == true ];then
  # do sth...
fi

提示“按任意键继续”:
read -s -n1 -p "按任意键继续……"

提示用户输入“y/n”并获取结果:
# 提示输入“y/n”,结果保存到变量needbackup中:
while [[ ! $needbackup =~ ^[yn]$ ]];do
  read -e -p "是否备份:(y/n)?" -i "y" needbackup
done
# 或:
while [ "$confirm" != "y" ] && [ "$confirm" != "n" ] ;do
  read -e -p "以上信息是否正确?(y/n)?" -i "y" confirm
done

倒计时:
seconds_left=10
echo "倒计时${seconds_left}秒开始……"
while [ $seconds_left -gt 0 ];do
  echo -n $seconds_left
  sleep 1
  seconds_left=$(($seconds_left - 1)) #变量减一
  echo -ne "\r \r" #清除本行文字
  # do some other thing,比如等待某个进程退出结束...
done

获取某个端口的pid:
pid=`netstat -nlp | grep $port | awk '{print $7}' | awk -F "/" '{print $1}'`
echo $pid

根据关键字找到相关进程的id:
process_keywords="/opt/app/tomcat/bin"
for pid in `ps -ef|grep $process_keywords|grep -v 'grep'|awk '{print $2}'`
do
  echo $pid
done

执行命令行的结果不输出到控制台(使用:&>/dev/null & ):
rm -f $2/logs/*.log &>/dev/null &
gzip * &>/dev/null &
/bin/sh $2/bin/startup.sh &>/dev/null &

根据日志文件判断某个进程是否已经完全启动(比如tomcat):
echo -n "请耐心等待tomcat完全启动……"
while [ ! "$started" ];do
  sleep 1
  # 根据log中是否出现"Server startup in [0-9]* ms"关键字来判断:
  started=$(grep "Server startup in [0-9]* ms" $2/logs/catalina.out)
  echo -n "…"
done
echo "tomcat已经完全启动。"





  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Shell脚本高级编程教程,希望对你有所帮助。 Example 10-23. Using continue N in an actual task: 1 # Albert Reiner gives an example of how to use "continue N": 2 # --------------------------------------------------------- 3 4 # Suppose I have a large number of jobs that need to be run, with 5 #+ any data that is to be treated in files of a given name pattern in a 6 #+ directory. There are several machines that access this directory, and 7 #+ I want to distribute the work over these different boxen. Then I 8 #+ usually nohup something like the following on every box: 9 10 while true 11 do 12 for n in .iso.* 13 do 14 [ "$n" = ".iso.opts" ] && continue 15 beta=${n#.iso.} 16 [ -r .Iso.$beta ] && continue 17 [ -r .lock.$beta ] && sleep 10 && continue 18 lockfile -r0 .lock.$beta || continue 19 echo -n "$beta: " `date` 20 run-isotherm $beta 21 date 22 ls -alF .Iso.$beta 23 [ -r .Iso.$beta ] && rm -f .lock.$beta 24 continue 2 25 done 26 break 27 done 28 29 # The details, in particular the sleep N, are particular to my 30 #+ application, but the general pattern is: 31 32 while true 33 do 34 for job in {pattern} 35 do 36 {job already done or running} && continue 37 {mark job as running, do job, mark job as done} 38 continue 2 39 done 40 break # Or something like `sleep 600' to avoid termination. 41 done 42 43 # This way the script will stop only when there are no more jobs to do 44 #+ (including jobs that were added during runtime). Through the use 45 #+ of appropriate lockfiles it can be run on several machines 46 #+ concurrently without duplication of calculations [which run a couple 47 #+ of hours in my case, so I really want to avoid this]. Also, as search 48 #+ always starts again from the beginning, one can encode priorities in 49 #+ the file names. Of course, one could also do this without `continue 2', 50 #+ but then one would have to actually check whether or not some job 51 #+ was done (so that we should immediately look for the next job) or not 52 #+ (in which case we terminate or sleep for a long time before checking 53 #+ for a new job).

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值