对Shell几个冷知识的总结(IFS,数组,替换,切割,查找)

IFS:

对IFS的用处直接进行说明,具体IFS是干什么的...自行谷歌

首先创建一个 "a a",和"a"的文件:

然后我们 ls查看一下:

--> ls
a  a a
是这样的。

然后假如我们需要写脚本操作每一个文件:

--> for i in $(ls); do echo "$i" ;done
a
a
a
但是结果确实这样的,显然与我们的要求不符

因为IFS的值是空格,制表符,回车。所以这里把a a变成了2个a

但是我们修改了IFS以后如下:

--> OldIfs=$IFS;IFS=$'\n';for i in $(ls); do echo "$i" ;done ;IFS=$OldIfs
a
a 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
    done
else
    echo -e "\e[01;31mNot Found .config"
fi
echo -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 
输出:
xiao
jun
yu
但是假如是这样的话:
XiaoCh="xiao;j un;yu"                                                                                                                    
for i in ${XiaoCh//;/ }                                                                                                                  
do                                                                                                                                       
    echo "$i"                                                                                                                            
done 
输出:

xiao
j
un
yu
并不是我们预料的那样,然后第二种分割方法

IFS法:

XiaoCh="xiao;j un;yu"                                                                                                                    
OldIFS=$IFS                                                                                                                            
IFS=$';'                                                                                                                               
XiaoArr=($XiaoCh)                                                                                                                        
                                                                                                                                         
for i in ${XiaoArr[@]}                                                                                                                   
do                                                                                                                                       
    echo "$i"                                                                                                                            
done                                                                                                                                     
                                                                                                                                         
IFS=$OldIFS
输出:
xiao
j un
yu


查找:

查找一个字符串是否包含另一个字符串

--> XiaoA="abc"; XiaoB="a"; [[ "${XiaoA/${XiaoB}/}" == "$XiaoA" ]] && echo "No" || echo "Yes"
Yes

--> XiaoA="abc"; XiaoB="w"; [[ "${XiaoA/${XiaoB}/}" == "$XiaoA" ]] && echo "No" || echo "Yes"
No

你可能感兴趣的:(Linux杂记)