對Shell幾個冷知識的總結(IFS,數組,替換,切割,尋找)

來源:互聯網
上載者:User

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  : xiaojunyu5201314@163.com #  > Gmail : lunaw.org@gmail.com #  > 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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.