shell小結(9)

test 字符串比較命令:
 
-n str1         str1 is not null (has length greater than 0) 不爲空並且長度大於0
 
-z str1         str1 is null (has length 0)                   爲空或者長度爲0

更細緻的文檔推薦在字符串比較時儘量不要使用 -n ,而用 ! -z 來代替。(其中符號 "!" 表示求反操作) 
 
//對於字符串的比較,最好使用[[]]運算符
 #!/bin/bash

if [[ "abc" < "cde" ]]; then
        echo 'ok'

fi
-------------------------------------------------------------------------------------------

AIC2 ftp1204/.dd> cat case.sh

#!/bin/bash

i="li da"

###將所有的變量用“”是一個好習慣“,如下面的這種情況就必須使用“”,否則error
case "$i" in
li da) //應該寫爲:"li da")
        echo 'ok'
        ;;
*)
        echo 'no ok'

esac


//例子
#!/bin/bash

read -p "input your choose:"  i

case $i in
1 | 2)    ###########注意這裏的用法################
        echo 'ok'
        ;;
*)
        echo 'other'
        ;;
esac

-------------------------------------------------------------------------------------------
echo "this is the $numnd"

  上述腳本並不會輸出"this is the 2nd",只會打印"this is the ";
這是由於shell會去搜索變量numnd的值,而實際上這個變量此時並沒有值。
可以使用花括號來告訴shell我們要打印的是num變量:

代碼:
num=2
echo "this is the ${num}nd" 
---------------------------------------------------------------------------------

 find . -type f | xargs rm -rf ;
 
 find . -type d -ok file {} \;  //這種方式會每次都進行詢問
 
 任何形式的命令都可以在-exec選項中使用

 find . -name "*.log" -exec rm -rf {} \;
 
 ftp1204@AIC2:~/.dd/backdir> find  .  -name "dos.txt" -exec ls {} \;
./dos.txt

ftp1204@AIC2:~/.dd/backdir> find  .  -name "dos.txt" | xargs ls
./dos.txt

---------------------------------------------------------------------------------

打/解包到指定的目錄:使用 tar 的 -C 選項,如 tar xvf newarc.tar.gz -C tmp/a/b/c。

------------------------------------------------------------------------------------------- 
 #刪掉七天前的日誌

find $IDEA_LOG_DIR -mtime +7 -exec rm -rf {} \;      

tmp=`cat $fileLocation| sed -e "s/192.168.163.41/$madm_srv/g"  \
                              |sed -e "s/192.168.163.40/$aemc_srv/g"    \
                              |sed -e "s/bond0/$netcard/g"    \
                              |sed -e "s/255.255.255.240/$NetMask/g"    \
                              |sed -e "s/192.168.163.47/$BroadCast/g"    \
                              |sed -e "s/mad1/$MainHost/g"    \
                              |sed -e "s/mad2/$SlaveHost/g"    \
                              |sed -e "s/madcluster/$ClusterName/g"    \

修改權限時才用-R,其他的都是小寫的r
chmod -R 777 /sharedisks/

 

發佈了27 篇原創文章 · 獲贊 0 · 訪問量 1444
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章