Bash变量
?变量=值
?引用方式为:$变量
[[email protected] Desktop]# HI="Hello,and welcome to $(hostname)." [[email protected] Desktop]# echo $HI Hello,and welcome to localhost.localdomain. [[email protected] Desktop]#
? &> :重定向所有的输出
? 2>&1 :重定向STDERR到STDOUT
? > :重定向STDOUT
? 2> :重定向STDERR
[[email protected] test]# ll *.sh *.txt > AAA ls: cannot access *.txt: No such file or directory [[email protected] test]# cat AAA -rwxr--r--. 1 root root 158 Jun 28 06:36 test1.sh -rwxrwxrwx. 1 root root 59 Jun 28 06:33 test.sh
[[email protected] test]# ll *.sh *.txt 2> BBB -rwxr--r--. 1 root root 158 Jun 28 06:36 test1.sh -rwxrwxrwx. 1 root root 59 Jun 28 06:33 test.sh [[email protected]host test]# cat BBB ls: cannot access *.txt: No such file or directory
[[email protected] test]# ll *.sh *.txt &> CCC [[email protected] test]# cat CCC ls: cannot access *.txt: No such file or directory -rwxr--r--. 1 root root 158 Jun 28 06:36 test1.sh -rwxrwxrwx. 1 root root 59 Jun 28 06:33 test.sh
从文件重定向到标准输入:
[[email protected] test]# cat AAA -rwxr--r--. 1 root root 158 Jun 28 06:36 test1.sh -rwxrwxrwx. 1 root root 59 Jun 28 06:33 test.sh
[[email protected] test]# cat AAA | tr ‘a-z‘ ‘A-Z‘ -RWXR--R--. 1 ROOT ROOT 158 JUN 28 06:36 TEST1.SH -RWXRWXRWX. 1 ROOT ROOT 59 JUN 28 06:33 TEST.SH
[[email protected] test]# tr ‘a-z‘ ‘A-Z‘ <AAA -RWXR--R--. 1 ROOT ROOT 158 JUN 28 06:36 TEST1.SH -RWXRWXRWX. 1 ROOT ROOT 59 JUN 28 06:33 TEST.SH
for语句:
[[email protected] test]# cat for.sh #!/bin/bash for NAME in joe jak sky do MESSAGE=‘hello world‘ echo He name is $NAME. " $NAME say $MESSAGE !" done [[email protected] test]# ./for.sh He name is joe. joe say hello world ! He name is jak. jak say hello world ! He name is sky. sky say hello world ! [[email protected] test]#
[[email protected] test]# cat for1.sh
#!/bin/bash
for num in $(seq 1 6)
do
echo $num
done
[[email protected] test]# ./for1.sh
1
2
3
4
5
6
[[email protected] test]#
[[email protected] test]# cat if.sh #!/bin/bash if ping -c1 -w2 128.0.0.1 &>/dev/null; then echo network service is up ! elif grep network /weihu.txt &>/dev/null; then echo network service is in maintenance ! else echo station is down ! fi [[email protected] test]#
时间: 2024-11-05 11:54:10