Understanding shell Scripts
The shell script works in two ways:
Interactive: Enter a command that the shell interprets and executes a command
Batch: Write many commands to the shell script, and the shell executes all commands at once
Shell a script is a program that combines various commands into logical statements
To view all the shell interpreters available in the system:
[Email protected] ~]# Cat/etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/usr/bin/sh
/usr/bin/bash
/usr/sbin/nologin
/bin/tcsh
/bin/csh
[Email protected] ~]#
To view the current shell interpreter:
[Email protected] ~]# echo "$SHELL"
/bin/bash
[Email protected] ~]#
Write a simple shell script
Shell scripts should include:
Script Declaration, comment information, executable statement
Script Declaration (#! ): Tell the system which shell to use to explain
Comment Information (#): Introduction to Features
Executable statement: Execute a specific command
Instance:
[Email protected] sh]# vim example.sh
#!/bin/bash
#For Example by Msl23
# blog:www.cnblogs.com/msl23
Pwd
Ll-a
There are three ways to execute shell scripts:
Script file path:./example.sh
SH script file path: SH example.sh
Source script file path: source example.sh
The Sh and source methods can execute the script directly, but the method of accessing the script path directly
The Execute permission is required to execute the script
[Email protected] sh]#./example.sh
-bash:./example.sh:permission denied
[Email protected] sh]# chmod u+x example.sh #给属主用户加执行的权限
[Email protected] sh]#./example.sh
/root/lab1/sh
Receive User's parameters
The following are the shell predefined variables that have been defined
$ |
Current Execution Shell program name of the script |
$1-9,${10},${11} ... |
Positional Variables for parameters |
$# |
How many parameters are there? |
$* |
Values for all variables |
$? |
whether the previous command succeeded, succeeded 0 , failure not 0 |
Instance:
[Email protected] sh]# vim example_1.sh
#!/bin/bash
#Test parameter passing
#Author: Msl23 blog:www.cnblogs.cn/msl23
echo " this shell the script name is: $ "
echo " there are $# . parameters, respectively: $* "
echo " First parameter: $ $, third parameter: $ "
[Email protected] sh]# chmod u+x example_1.sh
[Email protected] sh]#./example_1.sh One, three four five six
The shell script program name is:./example_1.sh
There are a total of 6 parameters, namely: One twothree four five six
First parameter: One , third parameter:three
linux[Basic]-15-Simple shell script [01]