码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-06-17 14:21:46      阅读:1123      评论:0      收藏:0      [点我收藏+]

标签:code   pts   sql   actor   for   div   factor   else   dog   

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  : 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
    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,数组,替换,分割,查找)

标签:code   pts   sql   actor   for   div   factor   else   dog   

原文地址:http://www.cnblogs.com/lxjshuju/p/7040056.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!