First, the use of variables

1. Name variables

Variables are defined without a dollar sign ($is required for variables in PHP), as in:

your_name="yikoulinux"
Copy the code

Note that there can be no Spaces between variable names and equals signs, which may be different from any programming language you’re familiar with. In addition, the naming of variable names must follow the following rules:

  • The name must contain only letters, digits, and underscores (_). The first character cannot start with a number.
  • There can be no Spaces between them. Instead, use underscores (_).
  • Do not use punctuation marks.
  • You can’t use keywords in bash (see the help command for reserved keywords).
  • Variable names are generally capitalized

Examples of valid Shell variable names are as follows:

RUNOOB
LD_LIBRARY_PATH
_var
var2
Copy the code

Invalid variable name:

? var=123
user*name=runoob
Copy the code

2. Common variables

Variables in Linux Shell are divided into system variables and user-defined variables.

  1. System variables: HOME, HOME, HOME, PWD, SHELL, SHELL, SHELL, USER etc. For example: echo $HOME etc..

  2. User-defined variables:

  1. Define variables: Variables = values

2) Display all variables in the current shell: set 3) Undo variable: unset variable 4) Declare static variable: readonly variable, note: cannot unset

  1. Assign the return value of a command to a variable (important)

1) A= ls-la, run the command inside, and return the result to variable A 2)

A=$(ls -la)
Copy the code

$is equivalent to backquotes

3)

for file in `ls /etc`
Copy the code

or

for file in $(ls /etc)
Copy the code

The above statement loops out the filename of the directory under /etc.

3. For example

Case 1:The meanings are as follows:

  1. Define a variable named name that is a mouthful of Linux
  2. Print the value of the variable name
  3. Define a variable named number with an initial value of 22
  4. Print the value of the variable number
  5. Directly output a string with variables
  6. Output a string with variables using double quotes
  7. Output a string with variables using single quotes
  8. Use double quotes to print a string with non-existent variables, which default to null
  9. Use double quotes to declare variables in strings
  10. Declare variables in a string using curly braces {& variable name}

Note: the above variables are temporary variables and will disappear when the terminal is closed.

Example 2: Delete a variable and view the specified variable

  1. Unset name Deletes variable name
  2. Look at the name variable

String operations

String-related operations are often involved in shell batch programs. There are many command statements, such as awk,sed can do various string operations.

In fact, the shell has a series of built-in operation symbols, which can achieve a similar effect. As you know, using internal operators will omit the time to start external programs, so the speed is very fast.

1. String manipulation (length, read, replace)

expression meaning
${#string} $string length
${string:position} In $string, the substring is extracted from the position $position
${string:position:length} In $string, a substring of length $length is extracted starting at position $position
${string#substring} Remove the shortest substring matching $substring from the beginning of the variable $string
${string##substring} From the beginning of the variable $string, remove the longest substring matching $substring
${string%substring} Remove the shortest substring matching $substring from the end of the variable $string
${string%%substring} Remove the longest string matching $substring from the end of the variable $string
${string/substring/replacement} Use $replacement to replace the first matching $substring
${string//substring/replacement} Use $replacement to replace all matching $substrings
${string/#substring/replacement} If the prefix of $string matches $substring, $replacement is used to replace the matched $substring
${string/%substring/replacement} If the suffix of $string matches $substring, $replacement is used to replace the matched $substring

Note: “* $substring “can be a regular expression.

2. Example of string operations

A) Calculate the length of the string

root@ubuntu:/home/peng# test='I love china'
root@ubuntu:/home/peng# echo ${#test}
12
Copy the code

${# variable name} gets the length of the string

B) String interception

root@ubuntu:/home/peng# test='I love china'
root@ubuntu:/home/peng# echo ${test:5}
e china
root@ubuntu:/home/peng# echo ${test:5:10}
e china
root@ubuntu:/home/peng# root@ubuntu:/home/peng# echo ${test:4:10}
ve china
Copy the code

${variable name: start: length} returns a substring

C) String deletion

root@ubuntu:/home/peng# test='c:/windows/boot.ini'
root@ubuntu:/home/peng# echo ${test#/}
c:/windows/boot.ini
root@ubuntu:/home/peng# echo ${test#*/}
windows/boot.ini
root@ubuntu:/home/peng# echo ${test##*/}
boot.ini
root@ubuntu:/home/peng# echo ${test%/*}
c:/windows
root@ubuntu:/home/peng# echo ${test%%/*}
c:
Copy the code

The ${variable names# subString regular expressions} start with substrings and delete matching expressions.${variable name % SUBString regular expression} is equipped with substring from the end of the string to delete matching expressions.Copy the code

Note:

${test##*/},${test%/*} is the easiest way to get the filename or directory address, respectively.Copy the code

D) String replacement

root@ubuntu:/home/peng# test='c:/windows/boot.ini'
root@ubuntu:/home/peng# echo ${test/\/ / \ \}
c:\windows/boot.ini
root@ubuntu:/home/peng# echo ${test/ / / / / \ \}
c:\windows\boot.ini
Copy the code

${variable/find/replace value} a "/" to replace the first,"// "means to replace all, when the search occurs:"/" please add the escape character "\/" to indicate.
Copy the code

Note that the string position starts at 0 and -1 represents the last position in the string; When intercepting a string, it closes and opens, starting at the left position and ending at the right position, excluding the right position.

Script creation and execution

Shell scripts are not considered an official programming language because they run in the shell of Linux, so they are called shell scripts. In fact, a shell script is a collection of commands. We usually record all the operations in a document and then invoke the commands in the document, so that the operation can be done in one step. Shell scripts are usually stored in /usr/local/sbin.

1) Establishment of shell scripts

In Linux, shell scripts (bash shell programs) are usually written in an editor (such as vi/vim). They consist of Unix/Linux commands, bash shell commands, program structure control statements, and comments. The Vim editor is recommended.

2) Start of script (first line)

The first line of a standard shell script indicates which program (interpreter) executes the contents of the script, which in Linux bash programming is typically:

#! /bin/bashCopy the code

or

#! /bin/sh <==255Less than a characterCopy the code

The beginning “#!” Also known as a magic number, when executing a bash script, the kernel uses the “#! Note that this line must be the first line at the top of each script, or the script comment line if it is not the first line, as in the example below.

root@ubuntu:/home/peng# cat test1.sh#! /bin/bash echo"scajy start"#! /bin/bash <== /bin/sh echo"scajy en:"
Copy the code

Difference between sh and bash

root@ubuntu:/home/peng# ls -l /bin/sh
lrwxrwxrwx 1 root root 4 Sep 21  2015 /bin/sh -> bash
Copy the code

Sh is a soft connection to bash. /bin/bash

Bash is the default shell of GNU/Linux. It is compatible with Bourne shell (sh). Bash adopts the features of Korn shell (Ksh) and C shell (CSH). Complies with IEEE POISIX P10003.2/ISO 9945.2 shell and Tools standards.

For Centos and Redhat Linux, the default shell is bash. Therefore, we can write shell scripts without #! / bin/bash. But if the current shell is not your default shell, such as TCSH, then you must write #! . Otherwise, the script file will only execute a collection of commands and will not be able to use the shell’s built-in commands. It is recommended that readers get into the habit of adding the opening language identifier to any script, which will be mentioned in the shell programming specification later. If the script does not specify a parser at the beginning, the script is executed using the corresponding interpreter. For example, run bash test.sh

3) Script comments

In a shell script, the contents of the following number (# #) said annotation, used to annotated script, comments will not be executed, just give a person look, annotations can be since the end of a line, also can behind script commands and command on the same line, development script, if there are no comments, others will be hard to understand what the script, In a long time, they will forget. Therefore, we should try to make it a habit to annotate the work we do (scripts, etc.), not only for the convenience of others, but also for ourselves. Otherwise, you may not remember the purpose of the script after you write it and waste precious time rereading it. It’s also bad for teamwork.

4) for

Create file first.sh and copy the following information to the file:

#cd usr/local/sbin
# vim first.sh
#! /bin/bash

##this is my first shell script
# Wirten by Linux 2021.5.3

date
echo "Hello world"
Copy the code

Shell scripts usually have an.sh suffix

The script can be executed in any of the following ways:

#sh first.sh
Copy the code
#bash first.sh
Copy the code
#./first.sh
Copy the code
#./first.sh
Copy the code

If the report permission is not enough, you can:

#chmod +x first.sh
Copy the code

4. Use of environment variables

1

  1. Variables in Linux Shell are divided into system variables and user-defined variables.
  2. System variables: HOME, HOME, HOME, PWD, SHELL, SHELL, SHELL, USER etc. For example: echo $HOME etc..
  3. User-defined variables:
1) Define variables: variables = values2Display all variables in the current shell:set
3Undo variable: unset variable4Declare static variables: readonly variables, note: cannot unsetCopy the code
  1. Rules for defining variables
1) Variable names can consist of letters, numbers, and underscores, but cannot start with a number.2) No Spaces on either side of the equal sign3Variable names are generally capitalizedCopy the code
  1. Assign the return value of a command to a variable (important)
1A= 'ls-la' backquotes, run the command inside, and return the result to variable A2A=$(ls -la) is equivalent to A backquoteCopy the code
  1. Basic syntax for setting environment variables:
exportVariable name = Variable value (Output shell variables as environment variables) source configuration file (make the modified configurations take effect immediately) echo $Variable name (query environment variables)Copy the code

2. Detailed operation

Check the values of the environment variables HOME and PATH:

root@ubuntu:/home/peng# echo $HOME
/root
root@ubuntu:/home/peng# echo $PATH
/usr/local/sbin:/usr/local/ bin: / usr/sbin, / usr/bin, / sbin, / bin: / usr/games: / home/peng/toolchain/GCC - 4.6.4 / bin: / home/peng/toolchain/arm - cortex_a8 / binCopy the code

View environment variables in Windows

View all paths in the environment variable PATH

Example for installing script paths

Method 1: Modify environment variables: Add the specified software installation directory in PATH.

root@ubuntu:/home/peng/yikou# pwd
/home/peng/yikou
root@ubuntu:/home/peng/yikou# ls
a.sh
root@ubuntu:/home/peng/yikou# sh a.sh
Sun May  2 17:00:14 PDT 2021
Hello world
root@ubuntu:/home/peng/yikou# export PATH=$PATH:/home/peng/yikou/
root@ubuntu:/home/peng/yikou# echo $PATH
/usr/local/sbin:/usr/local/ bin: / usr/sbin, / usr/bin, / sbin, / bin: / usr/games: / home/peng/toolchain/GCC - 4.6.4 / bin: / home/peng/toolchain/arm - cortex_a8 / bin: /home/peng/yikou/ root@ubuntu:/home/peng/yikou# a.shBash :/home/peng/yikou /a.sh: permission insufficient root@ubuntu:/home/peng/yikou# chmod 777 a.sh 
root@ubuntu:/home/peng/yikou# a.sh 
Sun May  2 17:01:34 PDT 2021
Hello world
Copy the code

Method 2: Modify the environment variable configuration file to make the modified environment variable take effect permanently

vim /etc/bash.bashrc
Copy the code

source.bash.rc to make the configuration file take effect againCopy the code

Close the terminal, open it and re-enter: a. Shi can still be executed.

root@ubuntu:/home/peng/# a.sh 
Sun May  2 17:10:00 PDT 2021
Hello world
Copy the code

5. Mathematical operations

1

Syntax used by operators:

Expr operator mapping tableOperator meaning

2. Detailed operation

  1. Compare the size, can only be compared to integers, need to add a space, Linux reserved keywords to escape
root@ubuntu:/home/peng/yikou# num1=30
root@ubuntu:/home/peng/yikou# num2=50
root@ubuntu:/home/peng/yikou# expr $num1 \> $num2
0
Copy the code

Check whether the previous command is successfully executed:Returns 0 success, other failures

  1. Less than, less than or equal to, greater than or equal to
expr $num1 \< $num2
expr $num1 \<= $num2
expr $num1 \>= $num2
Copy the code

  1. Operations add, subtract, multiply and divide
Add num1 = #17
num2=5Expr $num1 + $num2 + $num2 = 'expr $num1 + $num2' echo $num3 expr $num1 - $num2 # multiply expr $num1 \* $num2 expr $num1 / $num2 expr $num1 % $num2Copy the code

Matters needing attention:

The value of two parentheses must be assigned, otherwise an error will be reported

Script and user interaction

Manipulate command line arguments

1. Read parameters

The bash shell uses positional parameters to store all parameters of command line input, including the program name. 0 indicates the program name, 0 indicates the program name, 0 indicates the program name, 1 indicates the first parameter, 2 indicates the second parameter,… , 2 indicates the second parameter… , 2 indicates the second parameter… , 9 represents the ninth parameter. If the number of parameters is more than nine, the variables must be expressed as follows: 10, {10}, 10, {11}…

#! /bin/bash# author: A mouthful of Linux
for((count = 1; count <= $1; count++))
do
    echo The number is $count.
done
Copy the code

Execution Result:

Modify the script as follows:

echo The $1.$2.$3.$4
Copy the code

The result is as follows:

2 Read the program name

The first thing that comes to mind is to use 0, but 0, but 0, but 0 to get the file name including the./ and the path prefix, as follows:

echo The command entered is: $0The command entered is entered./14.sh
Copy the code

If you want to get only the file name without a./, you can use the basename command:

name=`basename $0'echo The command entered is: $name'14.sh
Copy the code

3 Special Variables

$#Represents the number of command line arguments:Copy the code
#! /bin/bash# author: A mouthful of Linux
params=$# echo The number of params is: $params
for((i = 1; i <= params; i++))
do
    echo The param is: $i
done
Copy the code

The execution result

If you want to get all the arguments, of course you can iterate through them one by one using # and loops. You can also use the following two special variables: * stores all command line arguments as a whole, while $@ stores all command line arguments separated by Spaces as follows:

#! /bin/bash# author: A mouthful of Linux
count=1
for param in "$*"
do
    echo "\$* parameter $count = $param"
    count=$[ $count + 1 ]
done

count=1
for param in "$@"
do
    echo "\$@ parameter $count = $param"
    count=$[ $count + 1 ]
done
Copy the code

4 Basic read

The read command takes input data from a keyboard or file descriptor and stores it in a specified variable.

Options:

-p: prompt for specifying the read value. -t: specifies the wait time (s) for the read value. If the read value is not entered within the specified time, the read value will not be waited. -n: Sets the number of allowed charactersCopy the code

parameter

Variable: The name of the variable that specifies the read valueCopy the code

Operation,

  1. Case 1
#! /bin/bash# author: A mouthful of Linux
# testing the read option

read -p "Please enter your name: " name
echo "Hello $name."
Copy the code

The execution result

  1. Case 2

With the read command, input data can be stored in as many variables as necessary. If fewer variables are specified, the last variable will contain all the remaining input, as shown below:

#! /bin/bash
# author: A mouthful of Linux
# testing the read option

read -p "Enter the values: " val1 val2 val3
echo "$val1"
echo "$val2"
echo "$val3"
Copy the code

Execution Result:

  1. Comprehensive example

Prompt the user for a positive integer num, then calculate 1+2+3+… + num value; You must check whether num is a positive integer. If not, enter again

Ideas:

  • The value of expr and an integer is $? To determine whether it is an integer

  • Expr $num1 > 0

#! /bin/bash
# author: A mouthful of Linux
while true
do
        read -p "please input a positive number: " num
        Check if the number is an integer
        expr $num + 1 &> /dev/null
        if [ $? -eq 0 ];then
                If the integer is greater than 0, return 1
                if [ `expr $num \> 0` -eq 1 ];then
                        #echo "yes,positive number"
                        # $sum is not assigned and defaults to 0
                        for i in `seq 0 $num`
                        do
                                sum=`expr $sum + $i`
                        done
                        echo "1 + 2 + 3 +... +$num = $sum"
                        Exit to perform calculations
                        exit
                fi
        fi
        echo "error,input enlegal"
        continue
done
Copy the code

Testing:

Relational operators

Sometimes we need to compare the size of two numbers, so we use relational operators. Relational operators only support numeric operations, not character operations.

1

The Shell language supports the following relational operators:

-eq: checks whether two numbers are equaltrue. -ne: checks whether two numbers are unequaltrue. -gt: Checks whether the number on the left is greater than that on the righttrue. -lt: Checks whether the number on the left is smaller than the number on the righttrue. -ge: checks whether the number on the left is greater than or equal to that on the righttrue. -le: checks whether the number on the left is less than or equal to that on the righttrue.Copy the code

2. Detailed operation

#! /bin/bash# author: A mouthful of Linux

a=10
b=20

if [ $a -gt $b ]
then
   echo "a great than b"
else
   echo "a not great than b"
fi
Copy the code

Execution result! As follows:

String operators

1

= Compare two strings for equality! -z checks if the string length is zero. -n checks if the string length is non-zeroCopy the code

2. Detailed operation

#! /bin/bash
# author: A mouthful of Linux

### This script is primarily used for string operators
if[!The $1 ]
then
    echo "The first parameter is null."
    echo "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *"
    echo "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *"
    echo "************** Execute the use case in the format of sh$0Variable 1 variable 2***************
    echo "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *"
    echo "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *"
    break
else
    if[!$2 ]
    then
        echo "The second parameter is null"
        echo "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *"
        echo "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *"
        echo "************** Execute the use case in the format of sh$0Variable 1 variable 2***************
        echo "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *"
        echo "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *"
        break
    else
        Check whether two strings are equal;
        if [ The $1 = $2 ]
        then
            echo "This is the first judgment statement."
            echo "Variable one equals variable two."
        else
            echo "This is the first judgment statement."
            echo "Variable 1 is not equal to variable 2."
        fi
        ###2, check if two strings are not equal;
        if [ The $1! =$2 ]
        then
            echo "This is the second judgment statement."
            echo "Variable 1 is not equal to variable 2."
        else
            echo "This is the second judgment statement."
            echo "Convenience quantity one is equal to variable two."
        fi
        Check if the string length is 0
        if [ -z The $1 ]
        then
            echo "This is the third judgment statement."
            echo "Variable 1 is a string of length 0"
        else
            echo "This is the third judgment statement."
            echo The $1
        fi
        Check if the string length is not 0
        if [ -n $2 ]
        then
            echo "This is the fourth judgment statement."
            echo "Variable 2 has a non-zero string length"
            echo $2
        else
            echo "This is the fourth judgment statement."
            echo "Variable 2 is a string of length 0"
        fi
        Check if the string is not empty
        if [ The $1 ]
        then
            echo "This is the fifth judgment statement."
            echo "Variable 1 is not empty"
        else
            echo "This is the fifth judgment statement."
            echo "Variable 1 is empty"
        fi
    fi
fi
Copy the code

Test results:

Examples of common operations on shell files and directories

  1. Extract the directory and filename of the path

Extraction directory:

dirname $path
Copy the code

Extract file name:

basename $path
Copy the code
  1. Batch renames files with Spaces
function processFilePathWithSpace(){ 
 find The $1 -name "* *" | while read line
 do 
 newFile=`echo $line | sed 's/[ ][ ]*/_/g'`
 mv "$line" $newFile
logInfo "mv $line $newFile $?"
 done
} 
Copy the code
  1. Iterate over the contents of the file
cat /tmp/text.txt | while read line
do
	echo $line
done
Copy the code
  1. If the file does not exist, create a file
[ -f $logFile ] || touch $logFile
Copy the code
  1. Recursively traverse the directory
function getFile() {for file in `ls The $1`
	do
		element=The $1"/"$file
    	if [ -d $element ]
		then 
        	getFile $element
    	else
        	echo $element
    	fi  
	done
}
Copy the code
  1. Clear file contents
cat /dev/null > $filePath
Copy the code