Shell Program Design (2) Shell syntax Variables
In shell, variables do not need to be declared before they are used. By default, variables are stored as strings, even if they are assigned numerical values. Shell and some tool programs convert numeric strings into corresponding values as needed to operate on them. Linux is case sensitive.
In shell, we can add a $ symbol before the variable name to access its content. Whenever we want to get the content of a variable, we need to add a $ character before it. When assigning values to a variable, we only need to use the variable name.
If the string contains spaces, it must be enclosed in quotation marks. Note that there cannot be spaces on both sides of the equal sign.
You can assign a value to a variable using the read command. This command requires a parameter, that is, the variable name of the data to be read into the user, and then it will wait for the user to input data. Generally, when you press the Enter key, the read command ends.
1. Use quotation marks
If a parameter contains one or more blank characters (such as spaces, tabs, or line breaks), quotation marks must be placed on the parameter. The behavior of variables such as $ myvar in quotation marks depends on the type of quotation marks you use. If you put a variable with the $ character in double quotation marks, the program will replace the variable with its value after executing this line; if you put it in single quotation marks, there will be no replacement. You can also cancel its special meaning by one \ character on the machine before the $ character.
Strings are usually placed in double quotation marks to prevent them from being separated by blank characters.
#! /Bin/bash
Myvar = "Hi there"
Echo $ myvar
Echo "$ myvar"
Echo '$ myvar'
Echo \ $ myvar
Echo Enter some text
Read myvar
Echo '$ myvar' now equals $ myvar
Exit 0
The output result is as follows:
Hi there
Hi there
$ Myvar
$ Myvar
Enter some text
HelloWorld
$ Myvar now equals Hello World
2. Environment Variables
When a shell script program starts running, some variables are initialized according to the Environment setting values. These variables are usually named in upper case to distinguish the variables defined in the script program. The latter is typically named in lower case letters.
# HOME main directory of the current user
# PATH: A list of directories separated by colons used to search for commands
# PS1 command prompt, usually a $ character, but in bash, you can use more complex values. # PS2 second-level prompt, used to prompt subsequent input, usually> character
# IFS input domain Separator
$0 SHELL script name
$ # Number of parameters passed to the script
$ Process ID of the shell script
3. parameter variables
$1, $2... Script program parameters
$ * List all parameters in a list. Each parameter is separated by the first character of the environment variable IFS.
$ @ It is a kind of exquisite transformation of $ *. If IFS is not used, the parameter values will not be combined when IFS is empty.
The following example shows the difference between $ * and $ @.
Chen123 @ ubuntu :~ /C ++ $ IFS =''
Chen123 @ ubuntu :~ /C ++ $ set foo bar bam
Chen123 @ ubuntu :~ /C ++ $ echo "$ @"
Foo bar bam
Chen123 @ ubuntu :~ /C ++ $ echo "$ *"
Foobarbam
Chen123 @ ubuntu :~ /C ++ $ unset IFS
Chen123 @ ubuntu :~ /C ++ $ echo "$ *"
Foo bar bam
Chen123 @ ubuntu :~ /C ++ $
Experiment: parameters and Environment Variables
Script program:
#! /Bin/sh
Salutation = "Hello"
Echo $ salutation
Echo "The program $0 is nowrunning"
Echo "The second parameter was $2"
Echo "The first parameter was $1"
Echo "The parameter list was $ *"
Echo "The user's home directory is $ HOME"
Echo "Please enter a newgreeting"
Read salutation
Echo $ salutation
Echo "The script is now complete"
Exit 0
Output result:
Hello
The program./hird is now running
The second parameter was
The first parameter was
The parameter list was
The user's home directory is/home/chen123
Please enter a new greeting
SIre
SIre
The script is now complete
Condition test or [command
To enhance readability, we end with a [Symbol] When we use it.
Let's take the simplest condition as an example to introduce the usage of the test command: Check whether a file exists. The command used to implement this operation is test-f <filename>, So we write the following code in the script program:
If test-f fred. c
Then
...
Fi
Alternatively, you can use [as follows:
If [-f fred. c]
The
...
Fi
Note that a space must be set between the [symbol and the condition to be checked.
The test command can use three types of conditions: String comparison, arithmetic comparison, and file-related conditional testing.
Example:
#! /Bin/sh
If [-f/bin/bash]
Then
Echo "file/bin/bash exits"
Fi
If [-d/bin/bash]
Then
Echo "/bin/bash is a directory"
Else
Echo "/bin/bash is NOT a directory"
Fi
Output:
File/bin/bash exits
/Bin/bash is NOT a directory
Control structure if statement:
If condition
Then
Statements
Else
Statements
Fi
Elif statement
If condition
Then
Statements
Elif condition
Statements
Else
Statements
Fi
For statement
For variable in values
Do
Statements
Done
Experiment: Use a fixed string for Loop
For variable in values
Do
Statements
Done
Output:
Bar
Fud
43
Experiment: for Loop expanded with wildcard characters
#! /Bin/sh
For file in $ (ls f *. sh );
Do
Lpr $ file
Done
Exit 0
This example demonstrates the usage of $ (command. The parameter table of the for command comes from the output result of the Command included in $.
While statement
While condition do
Statements
Done
Until statement
Until condition
Do
Statement
Done
Case statement
Case variable in
Pattern [| pattern]…) Statements ;;
Pattern [| pattern]…) Statements ;;
Esac
Command list
AND list
Statement & statement2 & statements &&...
OR list
Statement |...
Statement Block
If you want to use multiple statements in some scenarios where only one statement is allowed, you can enclose them in brackets {} to construct a statement block.