Bash variables
? variable = value
? Reference by: $ variable
[Email protected] desktop]# hi="Hello,and Welcome to $ (hostname). " Echo $HIHello, and welcome to localhost.localdomain.[ [Email protected] desktop]#
? &>: Redirect all outputs
? 2>&1: redirect stderr to stdout
? >: Redirected stdout
? 2>: Redirect StdErr
[[email protected] test]# ll *. sh *.txt > AAA ls : Cannot access *.txt:no such file or directory[[email protected] test]# cat Span style= "COLOR: #000000" > AAA -rwxr--r--. 1 root root 158 June 28 06 : 36 test1. sh -rwxrwxrwx. 1 root root 59 June 28 06 : 33 test. sh
[email protected] test]# ll*.SH*.txt2>BBB-rwxr--r--.1Root root158June - .: $Test1.SH-rwxrwxrwx.1Root root -June - .: -Test.SH[email protected] test]#CatBBBls: Cannot access *.txt:no suchfileor 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 June 28 06 : 36 test1. sh -rwxrwxrwx. 1 root root 59 June 28 06 : 33 test. sh
redirect from file to standard input:
Cat1158: test1. SH 1 root root : Test. SH
[[email protected] test]# cat AAA | tr " a-z " " a-z "-rwxr--r--. 1 root root 158 June 28 06 : 36 TEST1. SH -rwxrwxrwx. 1 root root 59 June 28 06 : 33 TEST. SH
TR ' A- z ' ' A- z ' <AAA1158: TEST1. SH1 root root : TEST. Sh
For statement:
[Email protected] test]#Cat for.SH #!/bin/Bash forNAMEinchJoe Jak Sky DoMESSAGE='Hello World' EchoThe He name is $NAME."$NAME say $MESSAGE!" Done[email protected] test]#./ for.SHHe name is Joe. Joe Say hello world!The 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/Bashif Ping-c1-w2128.0.0.1&>/dev/NULL; Then EchoNetwork SERVICE is up!elif grepNetwork/weihu.txt &>/dev/NULL; Then EchoNetwork Service isinchMaintenance!Else EchoStation was down!fi[email protected] test]#
shell--to Be Continued