标签:
data=‘name, sex, rollno, location‘ oldIFS=$IFS IFS=‘, ‘ for i in $data; do echo $i; done
$* 参数被作为一个单词 (这个变量很少被使用) $@ 参数按 IFS 分割 $? 退出参数 $$ 当前 shell 进程的 pid $! 后台运行的最后一个进程的 pid
var=‘xxxx‘ length=${#var} length=`echo -n $var | wc -m` word=`echo -n $var | wc -w`
s1=‘abc‘ s2=‘def‘ s3=`printf "%s%s" $s1 $s2` echo $s3
var=${var-‘value‘} # 如果 var 没被声明, 就用 value
a=‘abcd‘ echo ${a:0} # abcd echo ${a:1} # bcd echo ${a:1:2} # bc
{$var#str} # 从开头删除 str {$var##str} # 从开头删除最长匹配的 str {$var%str} # 从结尾删除 str {$var%%str} # 从结尾删除最长匹配的 str dir=/etc/httpd/httpd.conf echo ${dir#/*/} # httpd/httpd.conf echo ${dir##/*/} # httpd.conf echo ${dir%/*/} # 没有删除任何 echo ${dir%/*} # /etc/httpd echo ${dir%%/*} # 全部被删除 为空
str=‘abc 123 def abc‘ echo ${str/abc /def} # def123 def abc echo ${str//abc/def} # def 123 def def echo ${str//[0-9]/} # abc def abc
[ $var == "xxx" ] [ $var != "xxx" ] [ -n STRING ] # the length of STRING is nonzero [ -z STRING ] # the length of STRING is zero
[ $num -eq 10 ] [ $num -ne 10 ] [ $num -lt 10 ] [ $num -le 10 ] [ $num -gt 10 ] [ $num -ge 10 ]
[ -d $FILE_NAME ] # dir [ -e $FILE_NAME ] # exist [ -f $FILE_NAME ] # regular file [ -L $FILE_NAME ] # symbol link [ -r $FILE_NAME ] [ -w $FILE_NAME ] [ -x $FILE_NAME ] [ -S $FILE_NAME ] # socket [ -b $FILE_NAME ] # block [ -c $FILE_NAME ] # char
shell 的 $((RANDOM)) 会返回一个随机值
if [ ${#} -eq 0 ]; then echo $((RANDOM)) elif [ $1 == ‘--help‘ -o $1 == ‘-h‘ ]; then echo "usage: random num1, num2 [num1, num2)" elif [ ${#} -eq 1 ]; then echo $((RANDOM%$1)) else echo $((RANDOM%(($2-$1))+$1)) fi
echo $((100 / 3)) i=8 echo $((i + 12)) i=8 let i=5+3 # ‘=‘ 两侧不允许有空格 i=$((i+1)) let i++
# if if [ xxx ]; then xxx elif [ xxx ]; then xxx else xxx fi if [ xxx ] && [ xxx ]; then xxx fi # while while [ xxx ]; do xxx done while read line; do echo $line; done < input.txt; # for for i in `seq 1 10`; do echo $i; done for x in one two three four; do echo number $x done # case x="start" case $x in start) echo ‘start‘ ;; stop) echo "stop" ;; *) echo stop ;; esac x="restart" case $x in start|restart) echo ‘start‘ ;; stop) echo "stop" ;; *) echo stop ;; esac
myvar="hello" myfunc() { myvar="one two three" for x in $myvar; do echo $x done } myfunc # 执行函数 echo $myvar $x # one two three (说明变量是全局的)
myvar="hello" myfunc() { local x local myvar="one two three" for x in $myvar; do echo $x done } myfunc echo $myvar $x # hello ($x 没有输出)
repeat() { while true; do $@ && return sleep 5 done }
子 shell 就是 () 里面的内容,相当于重新开启一个 shell。这样做的好处是 子 shell 里面做的事情不会影响到当前的 shell,比如定义变量,cd 到别的路径..
pwd; (cd /bin; ls); pwd;
declare -A arr arr=([index1]=‘abc‘ [index2]=‘def‘) echo ${arr[index1]} echo ${arr[‘index1‘]} arr[index3]=‘xyz‘ echo ${arr[index3]}
for i in ${arr[@]}; do echo $i; # abc def xyz done for i in ${!arr[@]}; do echo $i; # index1 index2 index3 done
while getopts xyz opt; do case $opt in x) echo ‘x‘ ;; y) echo ‘y‘ ;; z) echo ‘z‘ ;; ?) echo ‘invalid‘ ;; esac done
./run -xyz (执行上面的代码)
while getopts x:y:z: opt; do case $opt in x) echo ‘x‘ $OPTARG ;; y) echo ‘y‘ $OPTARG ;; z) echo ‘z‘ $OPTARG ;; *) echo ‘invalid‘ ;; esac done
./run -x abc -y def -z ghi
免费提供最新Linux技术教程书籍,为开源技术爱好者努力做得更多更好:http://www.linuxprobe.com/
标签:
原文地址:http://www.cnblogs.com/linuxprobe/p/5598652.html