關于變量
一,在shell 腳本中定義默認值
1. 對默認變量直接賦值
read -p "input>>:" nub
nub=${nub:-30}
echo ${nub}
2. 對默認變量用可變值賦值
read -p "input>>:" nub
nub=${nub:-$(date +%Y%m%d)}
echo ${nub}
$@ 和 $* 的區(qū)別
[lf@x201t~]$vim my_fun.sh
#!/bin/bash
# $@ and $*
my_fun (){
echo "$#"
}
echo 'the number of parameter in "$@" is' $(my_fun "$@")
echo 'the number of parameter in $@ is' $(my_fun $@)
echo 'the number of parameter in "$*" is' $(my_fun "$*")
echo 'the number of parameter in $* is' $(my_fun $*)
執(zhí)行測試
[lf@x201t ~]$ sh a.sh p1 "p2 p3" p4
the number of parameter in "$@" is 3
the number of parameter in $@ is 4
the number of parameter in "$*" is 1
the number of parameter in $* is 4
看出區(qū)別了嗎?關鍵 $@ 靠譜一點
- 關于 > 和 <
[lf@x201t ~]$ cat <file >file.bak
[lf@x201t ~]$ cat file;cat file.bak
this is a test
this is a test
[lf@x201t ~]$ cat <file >file
[lf@x201t ~]$ cat file
[lf@x201t ~]$ =====> 這行輸出是空的,文件 file 已經(jīng)沒內容了
為什么在最后 cat 看不到 file 文件的內容了呢?
這是因為文件 file 這時真的是個空文件。 why???
因為 IO重定向中,標準輸出是要先準備好,才會從標準輸入讀入到標準輸出。是不是有點繞。
就像接力賽中,傳遞接力棒一樣的道理;你沒看到接棒的人,你肯定不會把接力棒送出去一樣。
這里 接棒人就是 輸出即>, 送棒人就是 輸入<, 就是把輸入重定向接收的內容,輸出到輸出重定向。
那么,輸出重定向就要先準備好,也就是先被執(zhí)行,執(zhí)行的結果就是把 file 文件清空。所以, cat <file >file 命令中, > 會先把文件 file 清空,之后 才會執(zhí)行 > file;結果就是空空。