对Shell几个冷知识的总结(IFS,数组,替换,分割,查找)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了对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
以上是关于对Shell几个冷知识的总结(IFS,数组,替换,分割,查找)的主要内容,如果未能解决你的问题,请参考以下文章