標籤:code pts sql actor for div factor else dog
IFS:
對IFS的用處直接進行說明,詳細IFS是幹什麼的...自行Google
首先建立一個 "a a",和"a"的檔案:
然後我們 ls查看一下:
--> lsa a a
是這種。
然後假如我們須要寫指令碼操作每個檔案:
--> for i in $(ls); do echo "$i" ;doneaaa
可是結果確實這種,顯然與我們的要求不符
由於IFS的值是空格,定位字元。斷行符號。所以這裡把a a變成了2個a
可是我們改動了IFS以後例如以下:
--> OldIfs=$IFS;IFS=$‘\n‘;for i in $(ls); do echo "$i" ;done ;IFS=$OldIfsaa a
嗯,這樣就達到了我們的目的,這個時候是以‘\n‘為切割符
數組:
一個指令碼進行說明:
#!/bin/bash# *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*# > Author: xiaojunyu/LunaW# > Mail : [email protected] # > Gmail : [email protected] # > Blog : http://blog.csdn.net/lunaw# > Web : http://lunaw.org http://lunaw.net# *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*# DEPENDS:=+libsqlite3 +libcurl +libpthread +libc# 預設選擇的包SelectPackages=("sed""gawk""grep""bc""luci-lib-json""iperf""tc""restorefactory""xinetd""n2n")# 預設不選擇的包 優先順序強於選擇的包UnSelectPackages=("horst""vim-full""vim-runtime""qos-scripts""wifidog""libcurl""curl""tc""sqlite3-cli""libsqlite3")if [[ -f .config ]]; then for Name in `seq 1 ${#SelectPackages[*]}` do PackageName="CONFIG_PACKAGE_${SelectPackages[$(expr $Name - 1)]}" if [[ `cat .config| grep "${PackageName}=y"| wc -l` == 0 ]]; then echo -e "\e[01;31mNot Found (${PackageName}=y) " if [[ `cat .config| grep "# ${PackageName} is not set"| wc -l` == 1 ]]; then echo -e "\e[01;33mFound (# ${PackageName} is not set) " sed -i "s/# ${PackageName} is not set/${PackageName}=y/g" .config echo -e "\e[01;36mReplace (${PackageName}=y) done!" else echo -e "\e[01;31mNot Found (# ${PackageName} is not set) " echo "${PackageName}=y" >> .config echo -e "\e[01;36mAdd (${PackageName}=y) done!" fi else echo -e "\e[01;33mFound (${PackageName}=y) " fi done echo "--------------------------------------" for Name in `seq 1 ${#UnSelectPackages[*]}` do PackageName="CONFIG_PACKAGE_${UnSelectPackages[$(expr $Name - 1)]}" if [[ `cat .config| grep "# ${PackageName} is not set"| wc -l` == 0 ]]; then echo -e "\e[01;31mNot Found (# ${PackageName} is not set) " if [[ `cat .config| grep "${PackageName}=y"| wc -l` == 1 ]]; then echo -e "\e[01;33mFound (${PackageName}=y) " sed -i "s/${PackageName}=y/# ${PackageName} is not set/g" .config echo -e "\e[01;36mReplace (# ${PackageName} is not set) done!" else echo -e "\e[01;31mNot Found (${PackageName}=y) " echo "# ${PackageName} is not set" >> .config echo -e "\e[01;36mAdd (# ${PackageName} is not set) done!" fi else echo -e "\e[01;33mFound (# ${PackageName} is not set) " fi doneelse echo -e "\e[01;31mNot Found .config"fiecho -e "\e[01;00m+---------------------------------+"echo -e "\e[01;00m| 不用操心,這僅僅是選擇了幾個包! |"echo -e "\e[01;00m+---------------------------------+"sleep 1
替換:
單一替換
--> XiaoName="xiaojunyu"; echo "${XiaoName/x/j}"jiaojunyu
所有替換
--> XiaoName="xiaojunyu"; echo "${XiaoName//u/a}"xiaojanya
刪除
--> XiaoName="xiaojunyu"; echo "${XiaoName//u/}"xiaojny
分割:
有幾種方法。第一種:
XiaoCh="xiao;jun;yu" for i in ${XiaoCh//;/ } do echo "$i" done
輸出:
xiaojunyu
可是假如是這種話:
XiaoCh="xiao;j un;yu" for i in ${XiaoCh//;/ } do echo "$i" done
輸出:
xiaojunyu
並非我們預料的那樣。然後另外一種切割方法
IFS法:
XiaoCh="xiao;j un;yu" OldIFS=$IFS IFS=$‘;‘ XiaoArr=($XiaoCh) for i in ${XiaoArr[@]} do echo "$i" done IFS=$OldIFS
輸出:
xiaoj unyu
尋找:
尋找一個字串是否包括還有一個字串
--> XiaoA="abc"; XiaoB="a"; [[ "${XiaoA/${XiaoB}/}" == "$XiaoA" ]] && echo "No" || echo "Yes"Yes
--> XiaoA="abc"; XiaoB="w"; [[ "${XiaoA/${XiaoB}/}" == "$XiaoA" ]] && echo "No" || echo "Yes"No
對Shell幾個冷知識的總結(IFS,數組,替換,分割,尋找)