Linux | Shell learning notes Shell process control if, case, for, while| read read the use of input | functions | cut, sed, awk, sort commands + Demo

Posted by chaking on Sun, 16 Jan 2022 04:10:52 +0100

reference material

Video link

Operating environment

  • windows10
  • Linux CentOS 7
  • Xshell 7

1, Process control

1.1 if judgment

  1. Basic grammar
#Writing method I
if [ Conditional judgment ]; then
 program
fi
#Writing method 2
if [ Conditional judgment]
   then
       program
fi

Precautions: (1) [conditional judgment], there must be a space between brackets and conditional judgment (2) there must be a space after if

  1. case

Enter a number to judge whether it is greater than 0. If it is greater than 0, it will output true. If it is less than or equal to 0, it will output false

#!/bin/bash
if [ $1 -gt 0 ]
then
        echo true 
elif [ $1 -le 0 ] 
then        
        echo false
fi

1.2 case statement

  1. Basic grammar
case $Variable name in 
  "Value 1")
  	If the value of the variable is equal to the value 1, execute program 1
  	...
  	;;
  "Value 2")
  	If the value of the variable is equal to the value 2, execute program 2
  	...
  	;;
  ...Omit other branches
   *)
    If none of the values of the variables are the above values, execute this procedure
    ;;
esac

matters needing attention:

  • The end of the case line must be the word "in", and each pattern match must end with a closing parenthesis ")".
  • Double semicolon ";" Indicates the end of the command sequence, which is equivalent to break in java.
  • The last "*" indicates the default mode, which is equivalent to default in java.
  1. case

Enter the user name and password. If not, an error message will be prompted

#!/bin/bash
if [ $# -eq 2 ]
then
        # Determine whether the user name is correct 
        case $1 in  
        "uni")
                echo 'The user name is correct!' 
                ;;
        *)
                echo 'User name error.'
                ;;
        esac
        # Determine whether the password is correct 
        case $2 in 
        "123")
                echo 'The password is correct!'
                ;;
        *)
                echo 'Password error!'
                ;;
        esac
elif [ $# -ne 2 ];
then
        echo 'The input parameters are incorrect. Please enter the user name and password'
fi

1.3 for loop

  1. Basic grammar 1
for (( Initial value;Cycle control conditions;Variable change ))
	do
		program
	done
  1. case
  • Calculate the value of 1 + 2 + 3... + 100
#!/bin/bash
sum=0 
for(( i=1;i<=100;i++))
	do          
        sum=$[$sum+$i]
	done        
            
echo $sum  

Operation result: 5050

  1. Basic grammar 2
for variable in Value 1 value 2 value 3...
	do 
		program
	done
  1. case

Sum the input parameters

#!/bin/bash
sum=0
for x in $*
    do
        sum=$[$sum+$x]
    done
echo $sum

Note: traversing "$*" will only traverse once, while traversing "$@" will traverse each input parameter

1.4 while loop

  1. Basic grammar
while [ Conditional expression ]
	do 
		program
	done
  1. case

Calculate the result of 1 + 2 + 3... + 100

#!/bin/bash
sum=0 
i=1
while [ $i -le 100 ]
do          
        sum=$[$sum + $i]
        i=$[$i + 1] 
done       
echo $sum

2, Read read console input

  1. Basic grammar

Read (option) (parameter)

  • option
    • -p: Specifies the prompt when reading the value
    • -t: Specifies the time (in seconds) to wait while reading the value
  • parameter
    • Variable: Specifies the variable name of the read value
  1. case

Within 5 seconds of prompt, read the input user name of the console

#!/bin/bash
read -t 5 -p "Please enter the user name within 5 seconds: " NAME
echo $NAME

3, Functions

3.1 system functions

basename function

Basic syntax: basename [string / pathname] [suffix]

Function Description: delete all suffixes, including the last ('/') character, and then print the string

Option Description: suffix is suffix. If specified, basename will remove suffix from pathname or string

Case: intercept the name of the current script after removing the suffix

basename ./uni.sh .sh

dirname function

Basic syntax: dirname [string / pathname] [suffix]

Function Description: remove the file name (non directory Part) from the given file name containing absolute path, and then return the remaining path (directory Part)

Case: get Uni File path of SH

dirname /home/s0125/uni.sh

3.2 user defined functions

Basic syntax:

[ function ] funname[()]
{
	Function content;
	[return int;]
}
funname

Tips:

  • You need to declare the function before calling the function. The shell script runs line by line and will not be compiled like other languages
  • Function return value can only be through $? Obtained from the system variable, you can display plus: return returns. If not, the result of the last command will be used as the return value, followed by the array n(0-255)

Case: use function to calculate the sum of two input parameters

#!/bin/bash
function sum()
{
        s=0;
        s=$[$1+$2]
        echo $s
}
read -p "Enter number a: " a
read -p "Enter number b: " b
sum $a $b

4, * Shell tools

4.1 cut

Basic syntax: cut [option parameter] filename

Function Description: it is responsible for cutting data in the file. This command cuts bytes, characters and fields from each line of the file and outputs these bytes, characters and fields.

Parameter Description:

Option parametersFunction description
-fColumn number, which column to extract
-dSeparator to split the column according to the specified separator

Case: cutting test text with spaces

Text content of test:

test.txt

hello world
this  is a test
i love  you
  • Cut first column

  • Cut the second and third columns

  • Get love field
cat test.txt | grep love

cat test.txt | grep love | cut -d " " -f 2

  • Gets all paths after the start of the second ":" of the system PATH variable
echo $PATH | cut -d ":" -f 3-

Where, 3 - after the - f parameter is represented from the third column to the last column, and - 3 can be represented as the first three columns

  • IP address printed after cutting ifconfig command
ifconfig ens33 | grep "inet 1" | cut -d " " -f 10

4.2 sed

Basic syntax: sed [option parameter] 'command' filename

Function Description: sed is a stream editor that processes one line of content at a time. During processing, the currently processed line is stored in the temporary cache called "mode space", and then the contents of the buffer are processed with the SED command. After processing, the contents of the buffer are sent to the screen. Then the next line is processed, which is repeated until the end of the file. The contents of the file are not changed unless redirected storage output is used.

Option parameters:

Option parametersFunction description
-eEdit the sed action directly in the command line mode

Command command parameters:

commandFunction description
aNew, a can be followed by a string, which appears on the next line
ddelete
sFind and replace

Case:

Test text sed txt

hello world
uni likes sleep

do you like run
i like eat rice
  • In sed Txt insert a line below the second line
sed "2a and run" sed.txt

  • Delete sed Txt file contains like lines
sed '/like/d' sed.txt

  • Set sed Txt file, replace like with love
sed 's/like/love/g' sed.txt

Note: g in the command is the abbreviation of global, indicating global replacement

  • Set sed Txt file and replace like with love
sed -e '2d' -e 's/like/love/g' sed.txt

4.3 awk

Basic syntax: awk [option parameter]'pattern1 {action1} pattern2 {action2}... ' filename

Function Description: a powerful text analysis tool, read the file line by line, slice each line with a space as the default separator, and analyze the cut part

Parameter Description:

  • Pattern: indicates the content found by AWK in the data, that is, the matching pattern
  • action: a series of commands executed when a match is found

Option parameters (partial):

Option parametersFunction description
-FSpecifies the input file separator
-vAssign a user-defined variable

Case:

  • Prepare data
sudo cp /etc/passwd ./
# Give read permission
sudo chown user name:user name passwd
cat passwd

  • Search the passwd file for all lines beginning with the root keyword and output the seventh column of the changed line
awk -F ':' '/^root/ {print $7}' passwd

Operation results:

/bin/bash
  • Search the passwd file for all rows starting with the root keyword, and output the first and seventh columns of the row, separated by ","
awk -F : '/^root/ {print $1","$7}' passwd

Operation results:

root,/bin/bash

Note: only the rows that match the pattern will execute the action

  • Only the first and seventh columns of the passwd file are displayed, separated by commas, and the column name user is added in front of all lines. The shell adds "uni, /bin/test" in the last line
awk -F : 'BEGIN{print "user,shell"} {print $1","$7} END{print "uni, bin/test"}' passwd 

Note: BEGIN is executed before all data rows are read; END is executed after all data is executed

  • Increase the user id in the passwd file by 1 and output it
awk -F : -v i=1 '{print $3+i}' passwd

Built in variables:

variableexplain
FILENAMEfile name
NRNumber of records read
NFNumber of fields for browsing records (number of columns after cutting)

Case:

  • Count the passwd file name, parentheses in each row, and the number of columns in each row
awk -F : '{print "filename:" FILENAME ", LineNumber: " NR ",columns: " NF}' passwd

Operation results:

filename:passwd, LineNumber: 1,columns: 7
filename:passwd, LineNumber: 2,columns: 7
filename:passwd, LineNumber: 3,columns: 7
filename:passwd, LineNumber: 4,columns: 7
...
  • Cutting IP
ifconfig ens33 | grep "inet 1" | awk -F ' ' '{print $2}'
  • Query sed The line number of the empty line in txt
awk '/^$/{print NR}' sed.txt

Where ^ $indicates that there is nothing between the beginning and the end, which can be used to represent an empty line

4.4 sort

Basic syntax: sort (option) (parameter)

Function Description: sort command is very useful in Linux. It sorts files and outputs the sorting results as standard

Parameter Description: (part)

parameterdescribe
-nSort by value
-rSort in reverse order
-tSets the separator character used when sorting
-kSpecify the columns to sort

Parameter: Specifies the list of files to be sorted

Case:

  • Prepare test data sort txt
a:40:5.4
b:20:4.2
c:50:2.3
d:10:3.5
e:30:1.6
  • Descending sort according to the second column of data
sort -t : -nrk 2 sort.txt

5, Shell command enterprise interview questions

5.1 JD

Question 1: use the Linux command to query the line number of the empty line in file1

Answer 1:

awk '/^$/{print NR}' sed.txt

Test site:

(1) Basic syntax of awk command 'pattern1 {action1}...' Action1 is executed only when pattern1 matches successfully

(2) The built-in Variable k represents the number of command lines that have been read

(3) The regular expression method of an empty line, ^ indicates that it starts with a symbol, and $indicates that it ends with a symbol. A space between them indicates an empty line

Question 2: give the following file: socre Txt, calculate the sum of the second and columns and output

Zhang San 40
 Li Si 50
 Wang wu60

Answer 2:

cat score.txt | awk -F " " '{sum+=$2} END{print sum}'

5.2 Sohu & Hexun

Question: how to check whether a file exists in the Shell script? What if it doesn't exist?

answer:

#!/bin/bash

if [ -f file.txt]; then
    echo "File exists."
else
    echo "file does not exist!"
fi

5.3 Sina

Problem: write a script with Shell to sort a column of numbers in an unordered text

Answer: you can use the awk command to sum

sort -n test.txt | awk '{a+=$0;print$0} END{print "sum="a}'

5.4 Jinhe network

Question: please use Shell script to write out the file name with the character "shen" in the contents of all files under the current folder (/ home)

answer:

grep -r "shen" /home | cut -d ":" -f 1

Topics: Linux Operation & Maintenance bash