In shell scripts, all of our variables are stored as strings. So it becomes very important for the interception of strings.
In a shell script, if you want to specify a string for interception, there are several ways to do it:
The following examples are used: str= "Hello World my Dear sister"
${str#*char} indicates that all characters to the left of the first char are deleted from the left, and the right string is truncated
#!/bin/bashstr= "Hello World my Dear sister" echo ${str}cut=${str#*}echo ${cut}
Results:
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/82/F0/wKioL1dmS-ezNpMnAAAZWU88Fpk887.png "title=" QQ picture 20160619153402.png "alt=" Wkiol1dms-eznpmnaaazwu88fpk887.png "/>
${str##*char} indicates that all characters to the left of the last char are deleted from the left, capturing the right string
Replace the example program with the following:
cut=${str##*/}
Results:
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/82/F1/wKiom1dmTF_iZxC0AAAVTdZp2JI417.png "title=" QQ picture 20160619153937.png "alt=" Wkiom1dmtf_izxc0aaavtdzp2ji417.png "/>
${str%char*} indicates that all characters to the right of the first char are deleted from the right, and the left string is truncated
Replace the example program with the following:
cut=${str%/*}
The same * can be seen as a wildcard character, indicating that all characters on the right are deleted, leaving the left side;
Results:
650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" border:1px solid rgb (221,221,221); Background-image:url ("/e/u261/lang/zh-cn/images/localimage.png"); background-position:50% 50%;background-repeat: No-repeat, "alt=" Spacer.gif "/>650" this.width=650; "src=" http://s5.51cto.com/wyfs02/M00/82/F0/ Wkiol1dmtmirygdqaaaza8cwvoy250.png "title=" qq picture 20160619154145.png "alt=" Wkiol1dmtmirygdqaaaza8cwvoy250.png "/>
${str%char*} indicates that all characters to the right of the last char are deleted from the right, capturing the left string
Replace the example procedure as follows:
Cut=${str%%/*}[object Object]
Results:
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/82/F0/wKioL1dmTR3Cg3RyAAAVHfKsIDg654.png "title=" QQ picture 20160619154308.png "alt=" Wkiol1dmtr3cg3ryaaavhfksidg654.png "/>
${str:n:m} means to intercept M characters starting from the nth character on the left, excluding the nth character
Replace the example program with the following:
Cut=${str:3:8}
Represents the next 8 characters starting from the 3rd character on the left, or 0 for the first character, but including the first character;
Results:
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/82/F1/wKiom1dmTXqwC4qJAAATpKZ27n4831.png "title=" QQ picture 20160619154441.png "alt=" Wkiom1dmtxqwc4qjaaatpkz27n4831.png "/>
${str:n} indicates that all subsequent characters are truncated from the nth character on the left, excluding the nth character
Change the above procedure to read as follows:
Cut=${str:7}
Represents all characters starting at the left of the 7th character, whichever follows;
Results:
650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s2.51cto.com/wyfs02/m01/82/f1/wkiom1dmtdmrdm7taaavjphdbwq929.png "title=" qq picture 20160619154612.png "alt=" Wkiom1dmtdmrdm7taaavjphdbwq929.png "/>
${str:0-n:m} means to intercept M characters starting from the nth character on the right, including the nth character
Replace the example program with the following:
Cut=${str:0-7:3}
Starts at the 7th character starting at the right, including the seventh character itself, and intercepts three characters;
Results:
The space does not output at the beginning of the line. Other characters are output, you can go down and try to include him.
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/82/F0/wKioL1dmTtDACcCrAAATGBxEgtg698.png "title=" QQ picture 20160619155006.png "alt=" Wkiol1dmttdacccraaatgbxegtg698.png "/>
${str:0-n} indicates that all subsequent characters are truncated from the nth character on the right, including the nth character
Replace the example program with the following:
CUT=${STR:0-7}
Represents the beginning of the 7th character from the right to intercept all subsequent characters;
Results:
The space is not output. Other characters are output, you can go down and try to include him.
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/82/F0/wKioL1dmT5WyDevfAAAWFhTuiqY420.png "title=" QQ picture 20160619155329.png "alt=" Wkiol1dmt5wydevfaaawfhtuiqy420.png "/>
${str} | cut-c n represents the nth character of the number from the left
Example code:
#!/bin/bashstr= "Hello World my Dear sister" echo ${str}echo ${str} | Cut-c 7
Represents the interception of the 7th character from the left;
Results:
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/82/F1/wKiom1dmUemAxEd9AAAUWK7UDpU199.png "title=" QQ picture 20160619155727.png "alt=" Wkiom1dmuemaxed9aaauwk7udpu199.png "/>
${str} | cut-c n -means to intercept all characters from the beginning of the nth character in the left-hand side, including nth
Change the program as above:
Echo ${str} | Cut-c 7-
Represents all the remaining characters starting from the Intercept from the 7th character on the left;
Results:
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/82/F1/wKiom1dmUiyTRqPcAAAb45YoQKw222.png "title=" QQ picture 20160619160442.png "alt=" Wkiom1dmuiytrqpcaaab45yoqkw222.png "/>
${str} | cut-c n-m represents the interception of all characters from the nth character to the first character in the left, including the nth character, but excluding the first m characters
Change the procedure to read as follows:
Echo ${str} | Cut-c 2-7
Represents the interception of all characters from the left 2nd character to the 7th character, including the 2nd character, excluding the 7th character;
Results:
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/82/F0/wKioL1dmUnbCH5X9AAAUPG0d5sA775.png "title=" QQ picture 20160619160559.png "alt=" Wkiol1dmunbch5x9aaaupg0d5sa775.png "/>
${str} | cut-c -n means to intercept all characters that go forward from the first nth character of the left, including the nth character
Change the procedure to read as follows:
Echo ${str} | Cut-c-7
Represents the first number of characters from the left of the 7th character to delete all characters, and the preceding characters are truncated;
Results:
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/82/F0/wKioL1dmUsjBSJkaAAAT6DAlDz4228.png "title=" QQ picture 20160619160721.png "alt=" Wkiol1dmusjbsjkaaaat6daldz4228.png "/>
expr substr "${str}" N m means to intercept m characters starting from the nth character on the left
Sample program:
#!/bin/bashstr= "Hello World my Dear sister" echo ${str}cut=$ (expr substr "${str}" 3 5) Echo $CUT
Because expr is a command, the above $ () is the command substitution, which means that the intercept starts from the 3rd character on the left and intercepts 5 characters;
Results:
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/82/F1/wKiom1dmU7zDlaaTAAAUTT9T9Bo852.png "title=" QQ picture 20160619161122.png "alt=" Wkiom1dmu7zdlaataaautt9t9bo852.png "/>
This article is from the "egg-left" blog, please be sure to keep this source http://memory73.blog.51cto.com/10530560/1790807
Shell script: The Interception of strings