This is the 25th day of my participation in the August Challenge
A lifelong learner, practitioner and sharer committed to the path of technology, a busy and occasionally lazy original blogger, an occasionally boring and occasionally humorous teenager.
Welcome to dig friends wechat search “Jie Ge’s IT journey” attention!
The original link: Shell programming | script parameters and the interaction and common problems
Shell programming – script parameters and interaction and common problems
When executing a script, it is often necessary to pass parameters to the script and generate data or perform specific logic based on the input parameter values.
1.1 Passing Parameters to the script
Shell scripts can be executed with parameters, and there are variables in the Shell script to refer to. The names of these variables are unusual: 0, 1, 2, 3… It’s called the position variable.
Positional variables start with 0, where 0 is reserved for the name of the actual script, 1 corresponds to the first parameter of the script program, and so on. As with any variable, the value of a positional variable can be referenced in the Shell with the “$” symbol.
[root@localhost 20190105]# vi paral.sh #! Echo 'The 1th parameter is '$1 # echo 'The 2th parameter is '$1 # Echo 'The 4th parameter is '$4 # Echo 'The 5th parameter echo 'The 4th parameter is '$4 # echo 'The 5th parameter echo 'The 4th parameter is '$4 # echo 'The 5th parameter Echo 'The 6th parameter is '$6 '. Echo 'The 6th parameter is '$7 ' Echo 'The 9th parameter is '$9 [root@localhost 20190105]#./paral.sh Ni hao, Nice to meet you! The script name is ./paral.sh The 1th parameter is Ni The 2th parameter is hao The 3th parameter is , The 4th parameter is Nice The 5th parameter is to The 6th parameter is meet The 7th parameter is you The 8th parameter is ! 9th parameter is // null value [root@localhost 20190105]#Copy the code
1.2 User Interaction
Using the read command, you can read data from the keyboard and assign it to a specified variable to interact with the user’s data in a Shell script.
The format of the read command
Read variable 1 [variable 2...]Copy the code
The read command reads the values of multiple variables from the keyboard. Data is separated by Spaces or tabs.
If the number of variables is the same as the number of input data, the corresponding values are assigned accordingly;
If the number of variables is greater than the number of input data, the assignment is made from left to right; If there is no data, the corresponding variable is null;
If the number of variables is less than the number of data entered, it is assigned from left to right, and the last variable is assigned all the remaining data.
The read command reads the data entered on the keyboard and saves it in a variable, displaying the value of the variable on the screen, and terminates the program when the user enters exit.
[root@localhost 20190105]# vi read1.sh #! Input2 = "# input3=" # input4= "# input4=" # input4= The variable value is empty #until loop, Until ["$input1" = exit] do echo 'Please input the values:' # Read input1 input2 If ["$input1"!= exit] then echo 'input1: Echo 'input2: '$input2: 'echo 'input3: '$input3:' echo 'input4: Else echo 'exit the script.' fi done [root@localhost 20190105]# Chmod +x read1.sh [root@localhost 20190105]#./read1.sh Please input the values: How do you do // Input1: How input2: do input3: You input4: do Please input the values: Welcome to Beijing Welcome input2: to input3: Beijing input4: Please input the values: let's go // The number of entered data is less than the number of variables input1: let's input2: go input3: input4: Please input the values: Nice to meet you,too! Input1: Nice input2: to input3: meet input4: you,too! Please input the values: // End the program exit exit the script. [root@localhost 20190105]#Copy the code
The running results can be seen as follows:
- When the number of variables is greater than the number of input data, the value of the variable corresponding to no data is null.
- When the number of variables is less than the number of data entered, the last variable is assigned all the remaining data;
1.3 Special Variables
Special variables and description
The variable name | instructions |
---|---|
$# | The number of arguments passed to the script |
$* | The values of all parameters passed to the script |
$@ | The same as $* |
$$ | Specifies the process id for executing the script |
$! | The process number of the last process to run in the background |
$- | Displays the current options used by the Shell |
$? | Displays the exit status of the command. 0: correct, 1: wrong. |
[root@localhost 20190105]# vi vall.sh #! /bin/bash echo 'The value of $# is: '$# echo 'The value of $* is: '$* echo 'The value of $* is: '$* Echo 'The value of $$is: echo 'The value of $$is: echo 'The value of $! is: '$! / / output $! Echo 'The value of $-is: '$- echo 'The value of $? is: '$? / / output $? [root@localhost 20190105]#./vall.sh how do you do The value of $# is: 4 The value of $@ is: how do you do The value of $@ is: How do you do The value of $@ is: How do you do 9040 The value of $! Is: // Output The value of $-is: hB // output The value of $? Is: 0 // Prints the value of the 0 variable [root@localhost 20190105]#Copy the code
1.4 Common Problems in Shell Programming
1.4.1 How Do I Mask the Command Output
Linux creates a device file /dev/null by default, and all output to the device is masked. You can mask the output of the command by redirecting it to the device /dev/null.
The command > / dev/nullCopy the code
Mask command error output
2 > command/dev/nullCopy the code
Mask normal and error output of commands
Command > /dev/null 2> /dev/nullCopy the code
For example, if you want to use the grep command in the Shell code to check whether a certain keyword exists in the file, you want to display the output of the grep command.
if grep jack /etc/passwd > /dev/null
then
echo "jack found"
fi
Copy the code
If the /etc/passwd file contains jack keyword information, jack found is displayed, but the result of grep command is not displayed.
1.4.2 How to Divide a Command into Multiple Lines
The Linux Shell script is very powerful, which allows users to combine multiple commands through pipes. However, the command written in a single line of Shell script is too long and difficult to read. To make the structure of the script clearer, you can divide a single line of Shell script into multiple lines.
Use two pipe characters to combine the ps, grep, and awk commands.
[root@localhost ~]# ps -ef | grep sshd | awk '{print $2}'
4478
12821
22028
Copy the code
Combining multiple commands in a single line of code is difficult to read. The Shell provides a special character, “\,” that lets you write one line of code into multiple lines.
[root@localhost ~]# ps -ef | \
> grep ssh | \
> awk '{print $2}'
4478
12821
23375
[root@localhost ~]#
Copy the code
Recommended reading
Super hardcore! 11 very useful Python and Shell scripts to use examples!
7 very useful Shell take to use script examples!
Shell programming – condition test | base paper
Shell programming – control structure | foundation
In this paper, to the end.
Original is not easy, if you think this article is useful to you, please kindly like, comment or forward this article, because this will be my power to output more high-quality articles, thank you!
By the way, please give me some free attention! In case you get lost and don’t find me next time.
See you next time!