Introduction to Shell Scripting
What is a Shell script? (There are similar types of files on windows, generally called batch files)
-
A program file composed of shell commands according to a certain syntax
What is the use of shell script?
Batch files/integration commands
-
software start
-
performance monitoring
-
log analysis
...
The essence of the Shell command (view the type command)
Built-in commands (already loaded into memory at startup, no need to reload from hard disk)
gec@ubuntu:~$ type cd
cd 是 shell 内建
gec@ubuntu:~$ type pwd
pwd 是 shell 内建
External commands (implemented by other external programs)
gec@ubuntu:~$ type ifconfig
ifconfig 是 /sbin/ifconfig
ifconfig corresponds to an external application, which actually corresponds to the ifconfig program under /sbin, and an application becomes
When a Shell command needs to be executed, a PATH environment variable is set. This path environment variable records the directory where an application corresponding to the external command is located. The current PATH can be printed out by the following command
gec@ubuntu:~$ echo $PATH
/home/gec/bin:/home/gec/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/local/arm/5.4.0/usr/bin
When inputting a command, it will go to the memory to match the built-in command first. If it cannot find it, the shell will go to these directories to find the corresponding application one by one. If it finds it, it will create a new process and execute it in the process. This new app.
Example:
gec@ubuntu:~$ sudo vi hello.c
gec@ubuntu:~$ gcc hello.c -o hello
gec@ubuntu:~$ ./hello
hello world
gec@ubuntu:~$ echo $PATH
/home/gec/bin:/home/gec/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/local/arm/5.4.0/usr/bin
gec@ubuntu:~$ sudo mv hello /usr/bin/
gec@ubuntu:~$ hello
hello world
By using GCC to compile the file and move hello.c
the generated executable file to the directory, which is in the environment variable of the system , hello becomes an external command similar to ifconfighello
/usr/bin/
$PATH
hello.c
#include <stdio.h>
int main()
{
printf("hello world\r\n");
return 0;
}
The application becomes an external command to the shell
compiled language
Is Shell scripting language the same as C language?
-
Compiled vs Interpreted : C language is a compiled language that requires a compiler to convert the source code into machine code, and then execute the generated executable file. The Shell script language is an interpreted language that interprets and executes scripts line by line without compiling and generating executable files.
-
Language usage : C language is usually used for system-level programming and low-level development, and can directly operate memory and hardware resources, so it is more suitable for developing applications with high performance requirements and more hardware control requirements. The Shell script language is mainly used for automation tasks, system management and script programming, and is more suitable for processing file operations, running system commands and writing simple scripts.
-
Grammatical features : C language has relatively strict grammatical rules, including concepts such as strongly typed variable declarations, complex control structures, and pointers. The syntax of the Shell script language is relatively simple and flexible, and supports direct calling of system commands and data processing using pipelines.
What are the commonly used Shell interpreters?
gec@ubuntu:~$ cat /etc/shells
# /etc/shells: valid login shells
/bin/sh
/bin/dash
/bin/bash
/bin/rbash
gec@ubuntu:~$ ls /bin/*sh
/bin/bash /bin/dash /bin/rbash /bin/sh /bin/static-sh
first shell script
helloworld
Edit, save, change permissions, run/debug
gec@ubuntu:~$ sudo vi hello.sh
gec@ubuntu:~$ sudo chmod 777 hello.sh
gec@ubuntu:~$ sudo vi hello.sh
gec@ubuntu:~$ ./hello.sh
hello world
Shell startup method
-
when the program executes
-
Specifies the interpreter to run
-
source and .
gec@ubuntu:~$ ./hello.sh
hello world
gec@ubuntu:~$ /bin/bash hello.sh
hello world
gec@ubuntu:~$ source hello.sh
hello world
gec@ubuntu:~$ . hello.sh
hello world
Shell script syntax explanation
define variable
-
variable=value
applies to no spaces -
variable='value'
will not be dereferenced, it will be printed intact -
variable="value"
will dereferencegec@ubuntu:~$ sudo vi test.sh gec@ubuntu:~$ ./test.sh 123 5674aaa gec@ubuntu:~$ sudo vi test.sh gec@ubuntu:~$ ./test.sh ${var}aaa
use variables
-
$variable
-
${variable}
-
1 #! /bin/bash 2 3 var="123 5674" 4 var1='${var}aaa' 5 echo "$var1"
Don't forget the {} of the variable to drink the variable value
Assign the result of the command to a variable
-
variable=`command`
-
variable=$(command)
Both of these can assign the result of the command to the variable on the left
delete variable
unset
#! /bin/bash
var="123 5674"
var1='${var}aaa'
unset var
echo "$var"
special variable
variable | meaning |
---|---|
$0 | The filename of the current script. |
$n(n≥1) | Arguments passed to the script or function. n is a number indicating the number of parameters. For example, the first parameter is $1 and the second parameter is $2. |
$# | The number of arguments passed to the script or function. |
$* | All parameters passed to the script or function. |
$@ | All parameters passed to the script or function. " " $@ works slightly differently from $* when enclosed in double quotes . |
$? | Exit status of last command or get function return value. |
$$ | The current shell process ID. For shell scripts, this is the process ID where the scripts reside. |
1 #! /bin/bash
2
3 var="123 5674"
4 var1='${var}aaa'
5
6 unset var
7 echo "$var"
8 echo "$0"
9 echo "$1"
10 echo "$2"
11 echo "$#"
12 echo "$*"
13 echo "$$"
14
15
16
17 exit 123
~
gec@ubuntu:~$ ./test.sh 10 11 13 14
./test.sh
10
11
4
10 11 13 14
10870
string concatenation
side by side
Read data entered from the keyboard
read is somewhat similar to scanf
1 #! /bin/bash
2
3
4 read -p "input a:" a
5 read -p "input b:" b
6 var="123 5674"
7 var1='${var}aaa'
8
9 #unset var
10 #echo "$var"
11 #echo "$0"
12 #echo "$1"
13 #echo "$2"
14 #echo "$#"
15 echo "$*"
16 #echo "$$"
17
18 echo "${a}"
19 echo "${b}"
20
21
22
23 exit 123
~
结果
gec@ubuntu:~$ ./test.sh
input a:111
input b:222
111
222
exit the current process
exit
Perform mathematical operations on integers
(())
logical and/or
command1 && command2
command1 || command2
When &&, the former is false and the latter will not be executed; when ||, the former is true and the latter will not be executed
Check if a condition is true
test expression和[ expression ]
options | effect |
---|---|
-eq | Check if values are equal |
- is | Check if values are not equal |
-gt | Determine whether the value is greater than |
-lt | Determine whether the value is less than |
-ge | Determine whether the value is greater than or equal to |
- the | Determine whether the value is less than or equal to |
-z str | Determine whether the string str is empty |
-n str | Determines whether the string str is non-empty |
= and == | Check if the strings str are equal |
-d filename | Determine whether the file exists and whether it is a directory file. |
-f filename | Determine whether the file exists, and whether it is an ordinary file. |
pipeline
command1 | command2
if statement
if condition then statement(s) fi
if else statement
if condition then statement1 else statement2 fi
if elif else statement
if condition1 then statement1 elif condition2 then statement2
…… else statementn fi
case in statement
case expression in pattern1) statement1 ;; pattern2) statement2 ;; pattern3) statement3 ;; …… *) statementn esac
for in loop
for variable in value_list do statements done
value_list
-
directly give the specific value
-
give a range of values
-
Execution result of the command
-
Using shell wildcards
-
use special variables
while loop
while condition do statements done
function
function name() { statements [return value] }