Shell Programming (ii)

Source: Internet
Author: User
Tags ming


Bo main name: Li Changming


Post Address: http://keep88.blog.51cto.com

This note is from the old boy books: Learn Linux OPS Shell programming with old boys

The advanced and practice of shell variable knowledge

1. Special positional parameter variables in the shell:


          & nbsp             role description
$0
$n n=1..9   gets the nth parameter value of the currently executing shell script, n is 0 o'clock, the output script file name, n>9, must be enclosed in curly braces, such as ${10}
$#
$*
[email protected] Gets the arguments for all parameters of the current shell script, and if [email protected] is given a double quote, "[email protected]" treats all parameters as separate strings, such as "$", "$"   "$ $" ...



For example:

1), $n use: N is {1..15}

Tips: [[email protected] test]# echo \${1..15}$1 $2 $3 $4 $5 $6  $7 $8 $9 $10 $11 $12 $13 $14 $15  [[email  Protected] test]# echo \a{1..10}a1 a2 a3 a4 a5 a6 a7 a8  a9 a10        #>==  Use this technique to quickly output a regular string  [[email  protected] test]# pwd/root/test[[email protected] test]# vim t1.sh [[ email protected] test]# cat t1.sh echo $1[[email protected] test]#  sh t1.sh  testtest[[email protected] test]# sh t1.sh   Zhangsan  lisizhangsan[[email protected] test]# vim t1.sh[[email protected]  test]# cat t1.sh echo $1 $2 $3[[email protected] test]# sh  t1.sh zhangsan wangwu lisizhangsan wangwu lisi 

Be aware that:

When the argument is greater than 9 o'clock, it must be enclosed in curly braces with ${10} ${11} ${12}.....${15}

2), the use of $ A:

[[email protected] test]# vim T2.sh[[email protected] test]# cat t2.sh echo $0[[email protected] test]# sh t2.sht2.sh[[ema Il protected] test]# sh t2.sh zhangsant2.sh Note: List two commands using 1), DirName and basename dirname: Get path basename: Acquired Fetch file name [[email protected] test]# dirname/root/test/t1.sh/root/test [[email protected] test]# basename/root/ Test/t1.sh t1.sh

3), $ #的使用:

[Email protected] test]# vim t2.sh [[email protected] test]# cat t2.sh echo $#[[email protected] test]# sh t2.sh zhangsan Lisi Wangwu Test1 Test25

4), $* [email protected] "$*" "[email protected]" The difference:

Combining the above concepts, examples of these four differences are analyzed:

1), use set to set positional parameters (same as command line script) [[email protected] ~]# set --  "I am"  smart  Boy. [[email protected] ~]# echo $ #3 [[email protected] ~]# echo $1i am [[email protected] ~]# echo $2smart[[email protected] ~]# echo $3boy.2 ), Test $* and [email protected], without double quotes [[Email protected] ~]# echo $*i am smart  boy. [[email protected] ~]# echo [email protected]i am smart  Boy. Use the For loop to output all parameters:   [[email protected] ~]# for i in $*;d O echo   $i;d Oneiamsmartboy. [[email protected] ~]# for i in [email protected];d o echo  $i; doneiamsmartboy.3), test "$*"   and   "[email protected]"  , note the use of double quotation marks [[email protected] ~]#  echo  "$*" I am smart boy. [[email protected] ~]# echo  "[Email&nbsP;protected] "I am smart boy. Use for output all parameters:[[email protected] ~]# for i  in  "$*";d o echo  $i;d onei am smart boy. [[email protected] ~]# for i in  "[email protected]";d o echo  $i; Donei amsmartboy. Summary:   Review at the beginning we wrote the concept analysis:   $* and [email protected] without quotes, output all parameters, in the For statement, If there is a space-delimited string, the split output   $* and [email protected] are quoted, with the example "$*"   "[email protected]", at which point the difference:     $*  will have all parameters as the full string output, as shown above     [email protected]  will all parameters, As a standalone single character output, if there is a space-delimited string, the output is not split as a separate single character output. Comment:  set  and eval commands in detail:   set can be used as command line   eval command: First scan the command line for all permutations before executing the command. This command applies to variables that scan for a time that does not function. This command scans the variable two times. These variables, which need to be scanned two times, are sometimes referred to as complex variables    for example: [[email protected] ~]# a= "cat t1.sh" [[email  protected] ~]# echo  $Acat  t1.sh[[email protected] ~]# eval  $A  #>==   the eval command now displaces the variable and executes the command i am smart  Boy.set with the use of Eval, the example used in scripting: Example 1), [[Email protected] ~]# number=$ (Set -- $ (cat  t4.sh); eval  "echo \$$#") Comment:  set  set incoming parameters,     eval perform two scans,   $#: Outputs the total number of positional parameters,          $$#: The total number, which is the value of the last positional parameter, Assign a value to number[[email protected] ~]# echo  $Number 5 cases 2), [[email protected] ~]#  runleveln 5[[email protected] ~]# runlevel=$ (set -- $ (Runlevel);eval  "Echo  \$$# ") [[email protected] ~]# echo  $Runlevel 5

2. Special state variables in the shell process:

$? Gets the execution state return value that executes the last instruction (0 for success, not 0 for failure)

3. Bash shell built-in variable command:

1), echo parameter
echo parameter options
-n
-e

Parsing escape character

transfer character:
\n newline
\t tab
\r enter
\b backspace
\v portrait tab


2), eval command use

[[email protected] ~]# cat T1.shecho \$$#[[email protected] ~]# sh t1.sh arg1 arg2$2 #>== $#: Total output position parameters 2 $$#:$2 echo Output
[[email protected] ~]# cat t1.sh eval echo \$$# #>== add eval command to output echo $ with command [[email protected] ~]# sh t1.sh arg1 Arg2 Arg3 Arg4arg4

3), shift

The main function of the shift command is to move the position parameter $1,$2, such as left, that is, if the position parameter is $3,$2,$1, then after a shift is performed, the $ $2,$2 becomes the $1,$1 and disappears.

For example:

[email protected] ~]# cat t4.sh echo $ $2if [$#-eq 2];then shift echo $1;fi[[email protected] ~]# sh t4.sh arg1 arg 2arg1 arg2arg2 #>== After the shift command is executed, the arg1 is left out, leaving the new $ $, which is arg2

4. Shell variable string knowledge and practice

An expression Description
${parameter} Returns the contents of the variable $parameter
${#parameter} Returns the length of the variable $parameter content
${parameter:offset} In variable ${parameter}, start extracting string to end after offset from position
${parameter:offset:length} In Variable ${parameter}, a string of length is extracted from the position offset after
${parameter#word} Delete the shortest matching word string from the beginning of the variable ${parameter}
${parameter# #word} Delete the longest matching word string from the beginning of the variable ${parameter}
${parameter%word} Delete the shortest matching word string starting at the end of the variable ${parameter}
${parameter%%word} Delete the longest matching word string starting at the end of the variable ${parameter}
${parameter/pattern/string} Use a string instead of the first matching pattern
${parameter//pattern/string} Use string instead of all matching pattern

Examples illustrate the use of built-in variables:

1), ${#parameter}: Calculated length

[Email protected] ~]# name= "lichangming" [[email protected] ~]# echo ${#name}11 #>== exactly 11 characters "lichangming"

2), ${parameter:offset}: Extracts the content to the end after the specified position, similar to the tiles in Python

[Email protected] ~]# name= "lichangming" [[email protected] ~]# echo ${name:3}hangming[[email protected] ~]# echo ${name: 2}changming

3), ${parameter:offset:length}: Start extracting string length from position offset

[[email protected] ~]# name= "lichangming" [[email protected] ~]# echo ${name:2:7}changmi #>== output 2nd position after 7th position content

Note:

The method of intercepting content on this, with the function of the CUT-C command

For example:

[[email protected] ~]# echo $namelichangming [[email protected] ~]# echo ${name} | Cut-c 2-7ichang #>== Cut command, followed by a number indicating: intercept the character of the 2~7 position and the difference between the built-in variable, the first offset of the built-in variable, the starting position is the content after offset

4), ${parameter#word}: Delete the shortest matching word string from the beginning

[[email protected] ~]# echo $namelichangming [[email protected] ~]# echo ${name#l*g}ming #>== matches the L to G string, remove it, note that it must be from the beginning Start matching, shortest match [[email protected] ~]# echo ${name#c*g}lichangming #>==c not match from the beginning, so no match found, no delete, output all

5), ${parameter# #word}: Delete the longest match from the beginning

[Email protected] ~]# name= "lichangmingcming" [[email protected] ~]# echo $namelichangmingcming [[email protected] ~]# echo ${name# #l *c}ming #>== longest match, matching LICHANGMINGC, deleting it, leaving Ming to see the results of the shortest match deletion: [[email protected] ~]# echo ${name#l*c} Hangmingcming #>== shortest match, matching lic, deleting

All of the above matching deletions are removed from the beginning.

6), ${parameter%word}: Delete shortest match from end

7), ${parameter%%word}: Delete longest match from end

For example:

[[email protected] ~]# echo $namelichangmingcming [[email protected] ~]# echo ${name%i*g}lichangmingcm[[email protected] ~]# echo ${name%%i*g}l[[email protected] ~]# echo ${name%%c*m}lichangmingcming[[email protected] ~]# echo ${name%%m*g} Lichang

Attention:

From the end of the match, refers to the first value and the end of the G-range string, in the above example, C*m, beginning with C, the middle is any value, the end is M, then did not find a matching result, output all, so, must remember is the first value and the end of the match.

8), ${parameter/pattern/string}: replace string with first match pattern

[Email protected] ~]# name= "Li2chang3ming4hehe8" [[email protected] ~]# echo $nameli 2chang3ming4hehe8[[email protected ] ~]# Echo ${name/3*4/test}li2changtesthehe8

9), ${parameter//pattern/string}: Replace string with all matching patterns pattern

[Email protected] ~]# name= "Zhangsan Lisi Wangwu Zhangsan liming" [[email protected] ~]# echo $namezhangsan Lisi Wangwu zh Angsan Liming[[email protected] ~]# echo ${name//zhangsan/someone}someone Lisi Wangwu someone liming

Note:

Use the method above to modify the file name in bulk

1), Generate test files:

[[email protected] test]# pwd/root/test[[email protected] test]# Ls[[email protected] test]# touch \smart_t{1..9}.sh[[ Email protected] test]# lssmart_t1.sh smart_t3.sh smart_t5.sh smart_t7.sh smart_t9.shsmart_t2.sh smart_t4.sh smart_t 6.sh smart_t8.sh

Batch rename: Change smart_t*.sh to Silly_t*.sh

[Email protected] test]# lssmart_t1.sh smart_t3.sh smart_t5.sh smart_t7.sh smart_t9.shsmart_t2.sh smart_t4.sh Smart _t6.sh Smart_t8.sh[[email protected] test]# for i in ' ls *.sh ';d o mv $i ' echo ${i//smart/silly} '; Done[[email protected] test]# lssilly_t1.sh silly_t3.sh silly_t5.sh silly_t7.sh silly_t9.shsilly_t2.sh silly_t4.sh s Illy_t6.sh silly_t8.sh

This method is the method of renaming that is implemented using a string substitution of variables.


2), rename with rename command:

[Email protected] test]# rename "smart" "silly"/root/test/*.sh[[email protected] test]# lssilly_t1.sh silly_t3.sh sil ly_t5.sh silly_t7.sh silly_t9.shsilly_t2.sh silly_t4.sh silly_t6.sh silly_t8.sh


3), for loop combined with SED for batch renaming:

[Email protected] test]# lssilly_t1.sh silly_t3.sh silly_t5.sh silly_t7.sh silly_t9.shsilly_t2.sh silly_t4.sh Silly _t6.sh Silly_t8.sh[[email protected] test]# for i in ' ls *.sh ';d o mv $i ' echo $i | Sed s/silly/smart/g ';d one[[email protected] test]# lssmart_t1.sh smart_t3.sh smart_t5.sh smart_t7.sh Smart_t9.shsmart_ t2.sh smart_t4.sh smart_t6.sh smart_t8.sh




This article is from the "Keep Keep your" blog, be sure to keep this source http://keep88.blog.51cto.com/11829099/1930977

Shell Programming (ii)

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.