Shell脚本模板 处理命令行参数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 # !/bin/bash while [ "$1" != "" ]; do case $1 in -s) shift #参数列表左移一位 echo "-s" ;; -d) shift echo "-d" ;; --p|p) shift echo "--p|p" ;; -h|help) usage #执行定义的方法或命令 exit ;; *) usage exit 1 ;; esac done
执行结果
1 2 3 4 5 [root@www testShell]# sh test.sh -s -d p -h -s -d --p|p test.sh: line 17: usage: command not found
命令行菜单 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 # !/bin/bash PS3="please enter your shoice:" options=("option 1" "option 2" "option 3" "quit") #数组 select opt in "${options[@]}" #使用 @ 或 * 可以获取数组中的所有元素 do case ${opt} in "option 1") echo "you chose choice 1" ;; "option 2") echo "you chose choice 2" ;; "option 3") echo "you chose choice $REPLY which is $opt" #内置变量,用户输入的值 ;; "quit") break ;; *) echo "invalid option $REPLY" ;; esac done
执行结果
1 2 3 4 5 6 7 8 9 10 11 12 [root@www testShell]# sh test.sh 1) option 1 2) option 2 3) option 3 4) quit please enter your shoice:1 you chose choice 1 please enter your shoice:2 you chose choice 2 please enter your shoice:3 you chose choice 3 which is option 3 please enter your shoice:4
颜色定义 1 echo -e "${Blu}blue ${Red}red ${RCol}etc...."
1 2 3 4 5 6 7 8 9 10 11 RCol='\e[0m' # Text Reset # Regular Bold Underline High Intensity BoldHigh Intens Background High Intensity Backgrounds Bla='\e[0;30m'; BBla='\e[1;30m'; UBla='\e[4;30m'; IBla='\e[0;90m'; BIBla='\e[1;90m'; On_Bla='\e[40m'; On_IBla='\e[0;100m'; Red='\e[0;31m'; BRed='\e[1;31m'; URed='\e[4;31m'; IRed='\e[0;91m'; BIRed='\e[1;91m'; On_Red='\e[41m'; On_IRed='\e[0;101m'; Gre='\e[0;32m'; BGre='\e[1;32m'; UGre='\e[4;32m'; IGre='\e[0;92m'; BIGre='\e[1;92m'; On_Gre='\e[42m'; On_IGre='\e[0;102m'; Yel='\e[0;33m'; BYel='\e[1;33m'; UYel='\e[4;33m'; IYel='\e[0;93m'; BIYel='\e[1;93m'; On_Yel='\e[43m'; On_IYel='\e[0;103m'; Blu='\e[0;34m'; BBlu='\e[1;34m'; UBlu='\e[4;34m'; IBlu='\e[0;94m'; BIBlu='\e[1;94m'; On_Blu='\e[44m'; On_IBlu='\e[0;104m'; Pur='\e[0;35m'; BPur='\e[1;35m'; UPur='\e[4;35m'; IPur='\e[0;95m'; BIPur='\e[1;95m'; On_Pur='\e[45m'; On_IPur='\e[0;105m'; Cya='\e[0;36m'; BCya='\e[1;36m'; UCya='\e[4;36m'; ICya='\e[0;96m'; BICya='\e[1;96m'; On_Cya='\e[46m'; On_ICya='\e[0;106m'; Whi='\e[0;37m'; BWhi='\e[1;37m'; UWhi='\e[4;37m'; IWhi='\e[0;97m'; BIWhi='\e[1;97m'; On_Whi='\e[47m'; On_IWhi='\e[0;107m';
执行结果
1 2 3 [root@www testShell]# Blu='\e[0;34m';Red='\e[0;31m';RCol='\e[0m' [root@www testShell]# echo -e "${Blu}blue ${Red}red ${RCol}etc...." blue red etc....
取当前运行脚本绝对路径 1 2 3 4 5 6 7 8 9 10 FILE="$0" while [[ -h ${FILE} ]]; do FILE="`readlink "${FILE}"`" done pushd "`dirname "${FILE}"`" > /dev/null DIR=`pwd -P` popd > /dev/null # Linux下可以用 dirname $(readlink -f $0)
在远程服务器运行一个本地脚本 1 2 3 4 # 无参数 ssh user@server 'bash -s' < local.script.sh # 有参数 ssh user@server ARG1="arg1" ARG2="arg2" 'bash -s' < local_script.sh
写出健壮性更好的脚本,一些相关的技巧
使用 -e 参数,如:set -e 或是 #!/bin/sh -e,这样设置会让你的脚本出错就会停止运行,这样一来可以防止你的脚本在出错的情况下还在拼拿地干活停不下来。
使用 -u 参数,如: set -eu,这意味着,如果你代码中有变量没有定义,就会退出。
对一些变理,你可以使用默认值。如:${FOO:-'default'}
处理你代码的退出码。这样方便你的脚本跟别的命令行或脚本集成。
尽量不要使用 ; 来执行多个命令,而是使用 &&,这样会在出错的时候停止运行后续的命令。
对于一些字符串变量,使用引号括起,避免其中有空格或是别的什么诡异字符。
如果你的脚有参数,你需要检查脚本运行是否带了你想要的参数,或是,你的脚本可以在没有参数的情况下安全的运行。
为你的脚本设置 -h 和 --help 来显示帮助信息。千万不要把这两个参数用做为的功能。
小心不同的平台,尤其是 MacOS 和 Linux 的跨平台。
对于 rm -rf 这样的高危操作,需要检查后面的变量名是否为空,比如:rm -rf $MYDIDR/* 如果 $MYDIR为空,结果是灾难性的。
考虑使用 “find/while” 而不是 “for/find”。如:for F in $(find . -type f) ; do echo $F; done 写成 find . -type f | while read F ; do echo $F ; done 不但可以容忍空格,而且还更快。
防御式编程,在正式执行命令前,把相关的东西都检查好,比如,文件目录有没有存在。
还可以使用ShellCheck 来帮助你检查你的脚本