Ten string processing

 

String interception

 

 

String interception format
Use the ${} expression ${ var : start position: length }; the numbering starts from 0 and can be omitted
Use expr substr expr substr " $var " start position length; start position numbering starts at 1
Use the cut tool echo $ var | cut -b start position - end position; the start position number starts from 1

 

 

String matching delete

 

 

     The format                         
 ${variable name#* keyword}       is from left to right, and the shortest match is deleted; # is used to delete the head, *wildcard 
${variable name##* keyword}      is from left to right, and the longest match is deleted; # Used to remove the header, * wildcard    

${variable name%keyword* }      From right to left, the shortest match is deleted; % is used to delete the tail, *wildcard     
${variable name%%keyword*} From right to left, the longest match is deleted; % is used remove tail, * wildcard   

 

 

Batch file renaming

 

 

 

Example: Change the extension .doc to .txt

 


#!/bin/bash
for FILE in *.doc
do
  mv $FILE ${FILE%.doc}.txt
done

 

 

string replacement

 

 

 

 

 

    The format
 ${ var/old/ new }   replaces only the first matching result    ${ var // old/new}   replaces all matching results    

 

 

 

 

Variable initial value processing

 

 

 

value

 

 

value, ${ var: -word}

 

 


Returns the value of $var if variable var already exists and is not null
Otherwise, the string " word " is returned , and the value of the variable var remains unchanged.
Purpose: Returns the default value if the variable is not defined

 

 

assign

 

Assignment, ${var:=word}

Returns the value of $var if variable var already exists and is not null
Otherwise return the string " word " and assign it to the variable var
Purpose: Assign a value to a variable if the variable is not defined

 

 

 

 

Prompt when there is value

 

 

 

Prompt when there is a value, ${var:+" prompt information "}

If the variable var already exists and is not null, give a hint
Otherwise return NULL (null value)
Purpose: Test whether a variable is defined

 

 

 

Prompt when no value

 

 

 

Prompt when no value, ${var: ? " Information "}

Returns the value of $var if variable var already exists and is not null
Otherwise, give prompt information (if omitted, the default prompt will be used)
Purpose: Catch errors caused by undefined variables

 

 

 

practise:

 

a . Prompt to enter a positive integer x , find the sum from 1 to x

 

b . If the user does not enter a value (enter directly), assign x=1

 

#!/bin/bash
read -p " Please enter a positive integer: " x
x=${x:-1};i=1;SUM=0
while [ $i -le $x ]
do
  let SUM+=i;let i++
done
echo " The sum from 1 to $x is: $SUM "

 

 

 

 

 

example:

 

mycluster.sh

 

Check the physical connection status of the hosts in the cluster, the requirements are as follows

 

Ability to set the number of hosts to  check and  which hosts to check

 

Save the IP address and time of the offline host to the stat.txt file in the /clusterdir directory and output the IP address  and time  number  of the offline host to the screen

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325252229&siteId=291194637