shell中特殊的变量
1、$0示例:
image.png
2、@和$#
[root@play ~]# set -- "I am" handsome oldboy.
[root@play ~]# echo $1
I am
[root@play ~]# echo $2
handsome
[root@play ~]# echo $3
oldboy.
[root@play ~]# echo $#
3
[root@play ~]# echo $*
I am handsome oldboy.
[root@play ~]# echo $@
I am handsome oldboy.
[root@play ~]# for i in "$*";do echo $i;done
I am handsome oldboy.
[root@play ~]# for i in "$@";do echo $i;done
I am
handsome
oldboy.
[root@play ~]#
image.png
例子如下:
[root@web01 /server/scripts]# cat oldboy.sh
echo $0
echo $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10} ${11} ${12} $13 $14 $15
echo $#
echo $*
echo $@
echo $$|tee /tmp/a.log
sleep 30
[root@web01 /server/scripts]#
[root@web01 /server/scripts]#
[root@web01 /server/scripts]#
[root@web01 /server/scripts]# cat kill.sh
kill `cat /tmp/a.log`
[root@play ~]# OLDBOY="I am oldboy"
[root@play ~]# echo $OLDBOY
I am oldboy
[root@play ~]# echo ${#OLDBOY}
11
[root@play ~]# echo ${OLDBOY:2}
am oldboy
[root@play ~]# echo ${OLDBOY:2:2}
am
[root@play ~]# echo ${OLDBOY:5}
oldboy
[root@play ~]# OLDBOY=abcABC123ABCabc
[root@play ~]# echo $OLDBOY
abcABC123ABCabc
[root@play ~]# echo ${OLDBOY#a*C}
123ABCabc
[root@play ~]# echo ${OLDBOY##a*C}
abc
[root@play ~]# echo ${OLDBOY%a*C}
abcABC123ABCabc
[root@play ~]# echo ${OLDBOY%%a*C}
abcABC123ABCabc
[root@play ~]# echo ${OLDBOY%%a*c}
[root@play ~]# echo ${OLDBOY%a*c}
abcABC123ABC
[root@play ~]# echo ${OLDBOY/abc/oppo}
oppoABC123ABCabc
[root@play ~]# echo ${OLDBOY/oppo/abc}
abcABC123ABCabc
[root@play ~]# echo ${OLDBOY//abc/oppo}
oppoABC123ABCoppo
[root@play ~]# echo ${OLDBOY/%abc/oldgirl}
abcABC123ABColdgirl
获取变量长度的5中方法
[root@play ~]# echo $OLDBOY|wc -L
15
[root@play ~]# echo ${#OLDBOY}
15
[root@play ~]# expr length "$OLDBOY"
15
[root@play ~]# echo $OLDBOY|awk '{print length}'
15
[root@play ~]# echo $OLDBOY|awk '{print length($0)}'
15
[root@play ~]#
image.png
[root@web01 /server/scripts]# test=lele
[root@web01 /server/scripts]# echo $test
lele
[root@web01 /server/scripts]# result=${test:-unset}
[root@web01 /server/scripts]# echo $result
lele
[root@web01 /server/scripts]# unset test
[root@web01 /server/scripts]# echo $test
[root@web01 /server/scripts]# result=${test:-unset}
[root@web01 /server/scripts]# echo $result
unset
image.png
五种运算方法:推荐用let和小括号
[root@play ~]# a=1
[root@play ~]# let a=a+1
[root@play ~]# echo $a
2
[root@play ~]# a=10
[root@play ~]# ((a=a+1))
[root@play ~]# echo $a
11
[root@play ~]#
[root@play ~]# a=10
[root@play ~]# expr $a + 110
120
[root@play ~]#
[root@play ~]# a=10
[root@play ~]# expr $a + 110
120
`[root@play ~]# a=11`
`[root@play ~]# echo $[$a+11]`
`22`
```[root@play ~]# c="$[a+11]"
```[root@play ~]# echo $c````
22
[root@play ~]#
[root@play ~]# a=8
[root@play ~]# declare -i a=a+10
[root@play ~]# echo $a
18
[root@play ~]#
整数或者小数运算:awk或者bc
`[root@play ~]# echo 1 3|awk '{print $1-$2}'`
-2
`[root@play ~]# echo 1.9 3|awk '{print $1+$2}'`
4.9
`[root@play ~]# echo 1.998 3|awk '{print $1+$2}'`
4.998
`[root@play ~]# echo 1.998 3|awk '{print $1*$2}'`
5.994
`[root@play ~]# echo 1.998 3|awk '{print $1%$2}'`
1.998
[root@play ~]#
image.png
image.png
image.png
第六章shell条件测试
`[root@play ~]# test -f a && echo ddd >a ||echo 0`
0
`[root@play ~]# `
[root@play ~]# touch a
[root@play ~]# test -f a && echo ddd >a ||echo 0
[root@play ~]# cat a
ddd
[root@play ~]#
image.png
image.png
image.png
网友评论