A summary of several cold knowledge of the shell (IFS, arrays, substitutions, splits, lookups)

Source: Internet
Author: User

Ifs:

A direct description of the usefulness of IFS, details of what ifs is doing ... Self Google

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

Then we look at LS:

--LSA  a A
Is this.

Then if we need to write a script to manipulate each file:

--For I in $ (LS); Do echo "$i";d oneaaa
But the result is, obviously, inconsistent with our demands.

Because the value of IFS is a space, a tab. Enter. So here we turn a A into 2 a.

But we changed the IFS after the following example:

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



Array:

A script to illustrate:

#!/bin/bash# *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*# > author:xiaojunyu/lunaw# > Mail: [email&n Bsp;protected] # > Gmail: [email protected] # > blog:http://blog.csdn.net/lunaw# > Web:http://lun aw.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 ") # 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 the 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} was not set/${packagename}=y/g ". C            Onfig 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} are 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 s            ET) 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|  Don't worry about it, it's just a few packages to choose from! |" Echo-e "\e[01;00m+---------------------------------+" Sleep 1


Replace:

Single replacement

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

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

Split:

There are several ways. The first type:

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

Xiaojunyu
Not the way we expected. Then another kind of cutting method

Ifs method:

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


Find:

Find if a string includes a 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

A summary of several cold knowledge of the shell (IFS, arrays, substitutions, splits, lookups)

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.