总结
方法
01、$0 当前shell脚本文件名
02、$n 当前shell脚本第n个参数
03、$# 当前shell脚本参数总个数
04、$* 获取当前shell脚本所有参数,"$*" 相当于 "$1 $2 $3"
05、$@ 获取当前shell脚本所有参数,"$@" 相当于 "$1" "$2" "$3"
06、cat n.sh
dirname $0 basename $0 ---------- bash /server/scripts/n.sh
07、cat q.sh
echo $1 $2
echo $#
----------
sh q.sh {a..z}
08、set -- "I am" handsome oldboy.;echo $# $1 $2 $3(--表示清除所有的参数变量)
09、echo $*;echo $@
10、for i in $*;do echo $i;done
11、for i in $@;do echo $i;done
12、echo "$*";echo "$@"
13、for i in "$*";do echo $i;done
14、for i in "$@";do echo $i;done
15、for i ;do echo $i;done(相当于in "#@")
16、$? 获取上一个命令的返回值
17、$$ 获取当前shell脚本的进程号(PID)
18、$! 获取上一个在后台工作的进程的进程号
19、$_ 获取之前执行的命令或脚本的最后一个参数
20、pwd;echo $?
21、cat test_pid.sh
#!/bin/bash pidpath=/tmp/a.pid if [ -f "$pidpath" ];then kill `cat $pidpath`>/dev/null 2>&1 rm -f $pidpath fi echo $$>$pidpath sleep 300 ---------- ps -ef|grep test_pid|grep -v grep sh test_pid.sh & ps -ef|grep test_pid|grep -v grep sh test_pid.sh & ps -ef|grep test_pid|grep -v grep
22、echo -n oldboy;echo oldgirl
23、echo -e "oldboy\toldgirl\noldboy\toldgirl"
24、printf "oldboy\toldgirl\noldboy\toldgirl"
25、echo -e "1\b23"
26、printf "1\b23\n"
27、cat eval.sh
eval "echo \$$#" ---------- sh eval.sh arg1 arg2
28、cat exec.sh
exec/tmp/tmp.log sh exec.sh ---------- seq 5>/tmp/tmp.log sh exec.sh
29、cat n.sh
echo $1 $2
if [ $# -eq 2 ];then
shift
echo $1
fi
----------
sh n.sh 1 2
30、${parameter} 返回变量parameter的内容
31、${#parameter} 返回变量parameter内容的长度(按字符)
32、${parameter:offset} 在变量${parameter}中,从位置offset之后开始提取子串到结尾
33、${parameter:offset:length} 在变量${parameter}中,从位置offset之后开始提取长度为length的子串
34、${parameter#word} 从变量${parameter}开头开始删除最短匹配的word子串
35、${parameter##word} 从变量${parameter}开头开始删除最长匹配的word子串
36、${parameter%word} 从变量${parameter}结尾开始删除最短匹配的word子串
37、${parameter%%word} 从变量${parameter}结尾开始删除最长匹配的word子串
38、${parameter/pattern/string} 使用string代替第一个匹配的pattern
39、${parameter//pattern/string}使用string代替所有匹配的pattern
40、OLD="I am oldboy"
41、echo ${OLD}
42、echo $OLD
43、echo ${#OLD} *最快速
44、echo $OLD|wc -L;expr length "$OLD"
45、echo "$OLD"|awk '{print length($0)}'
46、echo ${OLD:2}
47、echo ${OLD:2:2}
48、OLD="abcABC123ABCabc"
49、echo ${OLD#a*C};echo ${OLD#a*c}
50、echo ${OLD##a*c}
51、echo ${OLD%a*C};echo ${OLD#a*c}
52、echo ${OLD%%a*C};echo ${OLD%%a*c}
53、echo ${OLD/abc/456}
55、f=stu_102999_1_finished.jpg;mv $f`echo ${f//_finished/}`
55、${paramenter:-word} paramenter的值为空,则会返回word字符串并替代变量的值
56、${paramenter:=word} paramenter的值为空,则会设置成word字符串并返回
57、${paramenter:?word} paramenter的值为空,word字符串将被作为标准错误输出
58、${paramenter:+word} paramenter的值不为空,word字符串将被替代变量的值
59、echo $test
60、result=${test:-UNSET}
61、echo $result;echo ${test}
62、test=old;echo $test
63、result=${test:-UNSET};echo $result
66、result=${test-UNSET};echo $result
66、unset result;echo $result
66、unset test;echo $test
67、result=${test:=UNSET};echo $result
68、echo $test
69、result=${test=UNSET};echo $result;echo $test
70、echo ${key:?not defined};echo ${key?not defined}
71、key=1;echo ${key:?not defined}
72、echo ${keynot defined};unset key;echo ${key:?not defined}
73、old=${oldgirl:+word};echo $old
77、oldgirl=19;old=${oldgirl:+word};echo $oldboy
77、实现Apache服务启动脚本:cat /etec/init.d/httpd
HTTPD_LANG=${HTTPD_LANG-"C"}
prog=httpd
pidfile=${PIDFILE-/var/run/httpd.pid}
lockfile=${LOCKFILE-/var/lock/subsys/httpd}
77、删除7天前的过期备份数据:cat del.sh
find ${path-/tmp} -name "*.tar.gz" -type f -mtime +7|xargs rm -f
----------
sh -x del.sh
浙公网安备 33010602011771号