A summary of several cold knowledge of the shell (IFS, arrays, replacements, cuts, lookups)

Source: Internet
Author: User

IFS:

The use of IFS directly to explain what the specific ifs is doing ... Own Google

First create a file of "a", and "a":

Then we look at the LS:

--> ls
a A a  
That is true.

And then if we need to write a script to manipulate each file:

--> for I in $ (LS); Do echo "$i";d one a A a

But it turned out to be so, obviously inconsistent with our requirements.

Because the IFS value is a space, a tab character, a carriage return. So this turns a A to 2 a.

But we modified the IFS as follows:

--> oldifs= $IFS ifs=$ ' \ n '; For i in $ (LS); Do echo "$i";d one; ifs= $OldIfs
a
a
Well, that's what we're going to do, this time with the ' \ n ' as a separator.



Arrays:

A script is described:

#!/bin/bash # *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* # > Author:xiaojunyu/lunaw # > Mail:xiaoj Unyu5201314@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 # Default selected Package selectpackages= ("sed" "gawk" "grep" "BC" "Luci-lib-json" "Iperf" "TC" "Restorefactory" "xinetd") 
"N2N") # The default package priority is stronger than the selected package 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 $Nam e-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" &
            gt;>. 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_${unselect packages[$ (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" &
            gt;>. config echo-e "\e[01;36madd (# ${packagename} is not set) done!" Fi else Echo-e "\e[01;33mfound (# ${packagename} is not set)" fi do Else Echo-e "  \e[01;31mnot Found. config "fi echo-e" \e[01;00m+---------------------------------+ "ECHO-E" \e[01;00m|  Don't worry, it's just a couple of bags to choose from! | "Echo-e" \e[01;00m+---------------------------------+ "Sleep 1


Replace:

Single substitution

--> xiaoname= "Xiaojunyu"; echo "${xiaoname/x/j}"
Jiaojunyu
Replace All
--> xiaoname= "Xiaojunyu"; echo "${xiaoname//u/a}"
Xiaojanya
Delete

--> xiaoname= "Xiaojunyu"; echo "${xiaoname//u/}"
Xiaojny

Cutting:

There are several methods, the first of which are:

Xiaoch= "Xiao;jun;yu" for                                                                                                                     
i in ${xiaoch//;/}                                                                                                                  
Do                                                                                                                                       
    echo "$i"                                                                                                                            
Output:
Xiao
June
Yu
But if that's the case:
Xiaoch= "Xiao;j Un;yu" for                                                                                                                    
i in ${xiaoch//;/}                                                                                                                  
Do                                                                                                                                       
    echo "$i"                                                                                                                            
Output:

Xiao
J
un
yu
It's not what we expected, and then the second method of segmentation

Ifs method:

Xiaoch= "Xiao;j Un;yu"                                                                                                                            
oldifs= $IFS                                                                                                                               
ifs=$ '; '                                                                                                                        
                                                             Xiaoarr= ($XIAOCH)                                                                                                                   
For i in ${xiaoarr[@]}                                                                                                                                       
    Todo                          echo "$i"                                                                                                   
Done ifs= $OldIFS
Output:
Xiao
j un
yu


Find:

Find out if a string contains another string

--> xiaoa= "ABC"; Xiaob= "a"; [["${xiaoa/${xiaob}/}" = = "$XiaoA"]] && echo "No" | | echo "Yes"
Yes

--> xiaoa= "ABC"; Xiaob= "W"; [["${xiaoa/${xiaob}/}" = = "$XiaoA"]] && echo "No" | | echo "Yes"
No

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.