当前位置:网站首页>Shell script Basics
Shell script Basics
2022-06-12 21:22:00 【mb61037a3723f67】
Catalog
- 2.1:shell Prompt ( understand ):
- 2.2: shell Variable :
- 2.3: shell Special variables :
- 2.4: Replace :
- 2.5: character string :
- 2.6: Array :
- 2.7: Operator :
- 3.1: Enhanced Edition echo---printf
- 3.2: test command :
- 3.3: Output , Input redirection
- Reference address :http://c.biancheng.net/cpp/view/6995.html
One :shell Basic grammar
1: The foundation of the script
- 1: shell Script naming conventions : .sh ending .
- 2: shell The script must initially define the interpretation environment : #!/usr/bin/bash
- 3: shell Script is not allowed to write Chinese .
- 4: Script description : author , Creation time , edition , Script description .
#!/usr/bin/bash
#Author: liangShan
#Created Time:2021/08/30
#Release: 1.0
#Script Description: nginx install script
- 1.
- 2.
- 3.
- 4.
- 5.
- 5: Script composition :
- Explain the environment : #!/usr/bin/env
- annotated :
- Execute code :
- 6: Script run :
- 1: Add execution permission :
chmod +x Script name
./ Script
- 1.
- 2.
- 2: The interpreter runs directly :
bash Script - 7: shell Special symbols in :

- 8: Common ones shell :
- cat / etc/shells : see shell The order of :
- The common one is : sh, bash, dash
[email protected]:~$ cat /etc/shells
# /etc/shells: valid login shells
/bin/sh
/bin/bash
/usr/bin/bash
/bin/rbash
/usr/bin/rbash
/bin/dash
/usr/bin/dash
/usr/bin/tmux
/usr/bin/screen
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- View the default shell:
renshan[email protected]:~$ echo $SHELL
/bin/bash
- 1.
- 2.
2: shell command :
2.1:shell Prompt ( understand ):
- shell The prompt format in is through : PS1, and PS2 Two variables to null .
- for example : The first one below is PS1 Format , The second is PS2 Format .
[email protected]:~$ echo "liangshan"
liangshan
[email protected]:~$ echo "
> liang
> shan
> "
liang
shan
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
2.2: shell Variable :
- shell There is only one type of variable in : character string .
- Definition of variables :
Variable name = value
Variable name =' value '
Variable name =" value "
- 1.
- 2.
- 3.
- 1: There must be no spaces on either side .
- 2: Variable name : Numbers , Letter , Underline makes up , Letter , Start with an underline .
- Use of variables : Recommended {}
${ Variable }
$ Variable
- 1.
- 2.
- The difference between single quotation mark and double quotation mark :
- Single quotation marks cannot explain variables
- Case study :
#!/bin/bash
# Author: renshanwen
user="liangshan"
full_name=' I am a :${user}'
full_name2=" I am a :${user}"
echo $full_name
echo $full_name2
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
-
- The result of the command is assigned to a variable :
- 1.
- 2.
- 3.
- 4.
- Variable =$( command )
- A read-only variable :
- readonly Variable name
- Delete variables :
- unset Variable name
2.3: shell Special variables :
Variable | meaning |
$0 | The name of the current script . |
$n | Parameters passed to the script : $1 That's the first parameter . |
$# | The number of parameters passed to the script . |
$* | All parameters passed to the script . |
All parameters passed to the script . It is slightly different when it is enclosed in double quotation marks . | |
$? | Exit status of last command / The return value of the function . General order 0 It means success ,1 It means failure .s |
$$ | At present shell The process of ID |
- Case study :
[email protected]:~$ ./test.sh aaa bbb
File Name: ./test.sh
First Parameter: aaa
Second Parameter: bbb
Quoted Values: aaa bbb
Quoted Values: aaa bbb
Total Number of Parmaeters: 2
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
* #!/bin/bash
# Author: renshanwen
echo "File Name: $0"
echo "First Parameter: $1"
echo "Second Parameter: $2"
echo "Quoted Values: [email protected]"
echo "Quoted Values: $*"
echo "Total Number of Parmaeters: $#"
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
-
$* and [email protected] The difference between :
#!/bin/bash
echo "\$*=" $*
echo "\"\$*\"=" "$*"
echo "\[email protected]=" [email protected]
echo "\"\[email protected]\"=" "[email protected]"
echo "print each param from \$*"
for var in $*
do
echo "$var"
done
echo "print each param from \[email protected]"
for var in [email protected]
do
echo "$var"
done
echo "print each param from \"\$*\""
for var in "$*"
do
echo "$var"
done
echo "print each param from \"\[email protected]\""
for var in "[email protected]"
do
echo "$var"
done
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
$*= a b c d
"$*"= a b c d
[email protected]= a b c d
"[email protected]"= a b c d
print each param from $*
a
b
c
d
print each param from [email protected]
a
b
c
d
print each param from "$*"
a b c d
print each param from "[email protected]"
a
b
c
d
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
-
$* When surrounded by double quotes , The printed parameters are a whole . however [email protected] It will print out a single parameter .
2.4: Replace :
#!/bin/bash
# Author: renshanwen
a=10
echo -e "Value is $a \n"
- 1.
- 2.
- 3.
- 4.
- Meaning of escape character

- Command substitution :
#!/bin/bash
# Author: renshanwen
DATA=`date`
echo "Data is $DATA"
USERS=`who | wc -l`
echo "Logged in user $USERS"
UP=`date ; uptime`
echo "UP time is $UP"
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- Command substitution means Shell You can execute the order first , Save the output temporarily , Output... In the right place .
- Variable substitution :

2.5: character string :
- Single quotation marks
- Double quotes :
- Double quotes can have variables , Escape characters can also appear .
- String splicing :
#!/bin/bash
# Author: renshanwen
my_name="liangshan"
my_full_name=" name : "$my_name"!"
echo $my_full_name
Running results :
[email protected]:~$ ./test.sh
name : liangshan!
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
#!/bin/bash
# Author: renshanwen
my_name="liangshan"
my_full_name=" name : ${my_name}!"
echo $my_full_name
[email protected]:~$ ./test.sh
name : liangshan!
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- You can use quotation marks , You can also use ${}
- Get string length :
#!/bin/bash
# Author: renshanwen
my_name="liangshan"
my_full_name=" Name length : ${#my_name}!"
echo $my_full_name
Running results :
[email protected]:~$ ./test.sh
Name length : 9!
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- ${# Variable name }
- Extract substring :
#!/bin/bash
# Author: renshanwen
my_name="liangshan"
echo ${my_name:0:5}
# Output : liang
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- ${ Variable name : Start subscript : Number }
2.6: Array :
- 1: To define an array :
- array_name=(value1 value2), Note that there is no comma between variables .
- array_name[0]=value1
- 2: Read array :
- ${ Array name [index]}
- Get all the elements in the array : ${ Array name [*]}
- 3: Gets the length of the array :
- ${# Array name [*]}
#!/bin/bash
# Author: renshanwen
my_array=("1" "2" "3")
echo " The first element of the array is : ${my_array[0]}"
echo " All elements of the array are : "${my_array[*]}
echo " The length of the array is :${#my_array[*]}"
[email protected]:~$ ./test.sh
The first element of the array is : 1
All elements of the array are : 1 2 3
The length of the array is :3
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
2.7: Operator :
- Arithmetic operator :
- We use it a lot expr Do arithmetic operations :
- Space between expression and operator .
- The complete expression uses `` contain .
#!/bin/bash
# Author: renshanwen
val=`expr 2 + 2`
echo " The result is :$val"
- 1.
- 2.
- 3.
- 4.
-
List of arithmetic operators :

- Relational operator :

- Relational operators only support : Numbers , String not supported .
- Boolean operator :

- String operators :

Two : shell Branch loop
2.1: Conditional branch :
- 1: The simplest if sentence : a It's not equal to b The output .
#!/bin/bash
# Author: renshanwen
a=10
b=20
if [ $a != $b ]
then
echo "a is not equal to b"
fi
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 2: if else sentence :
#!/bin/bash
# Author: renshanwen
a=10
b=10
if [ $a != $b ]
then
echo "a is not equal to b"
else
echo "a is equal to b"
fi
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 3: Multi branch selection structure :
#!/bin/bash
# Author: renshanwen
a=10
if [ $a -lt 10 ]
then
echo "a Less than 10"
elif [ $a -lt 20 ]
then
echo "a Less than 20, Greater than or equal to 10"
else
echo "a Greater than 20"
fi
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
-
- 4: And test Use with commands :
#!/bin/bash
# Author: renshanwen
a=$[10*20]
b=$[20*10]
if test $[a] -eq $[b]
then
echo " Both equal "
else
echo " The two are not equal "
fi
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
2.2: loop :
- 1: Use while Loop printing 1 To 100
#!/bin/bash
# Author: renshanwen
COUNTER=0
while [ $COUNTER -lt 100 ]
do
COUNTER=`expr $COUNTER + 1`
echo $COUNTER
done
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 2: Use while Loop reading variable information :
#!/bin/bash
# Author: renshanwen
while read FILM
do
echo " The input is :$FILM"
done
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- Keyboard input information , Save in variables : FILM in .
- Press Ctrl + D End of cycle .
- 3: until loop :
- Not until a certain condition is established .
- Case study : Print 0 To 10
#!/bin/bash
# Author: renshanwen
COUNT=0
until [ $COUNT -gt 10 ]
do
echo $COUNT
COUNT=`expr $COUNT + 1`
done
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 4: for loop :
#!/bin/bash
# Author: renshanwen
for loop in 1 2 3 4 5
do
echo "loop The value of is $loop"
done
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 5: Out of the loop :
- break
- comtinue
3、 ... and : Enhanced output , test , Output and input redirection
3.1: Enhanced Edition echo—printf
- And C The language format is the same .
- 1.
- 2.
- 3.
3.2: test command :
- Check whether a certain condition is true .
- Case a : Test whether the two numbers are equal ( The judgment is the value ):
num1=100
num2=100
if test $[num1] -eq $[num2]
then
echo 'The two numbers are equal!'
else
echo 'The two numbers are not equal!'
fi
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
-
- Case 2 : Test whether two characters are equal .
num1=100
num2=100
if test num1=num2
then
echo 'The two strings are equal!'
else
echo 'The two strings are not equal!'
fi
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
3.3: Output , Input redirection
- Output of command , Input redirection :
-
>: Cover the output -
>>: Additional output -
<: Override input
Four : function
- 1: Must be defined before using .
- 2: shell The return value of a function can only be an integer , Generally speaking 0 It means success , Other indication of failure .
- 3: If you need to return other types , You need to define a variable first , The script can access this variable when necessary .
- 4: Case a : Define a function , And then call .
#!/bin/bash
# Author: renshanwen
Hello () {
echo " Ha ha ha "
}
Hello
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
-
- 2: Case 2 : Define a function with a return value , And then call .
#!/bin/bash
# Author: renshanwen
funcWithReturn (){
read aNum
read anotherNum
return $(($aNum + $anotherNum))
}
funcWithReturn
res=$?
echo $res
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
-
- Be careful : Cannot return a string , Need to use $() Convert to variable .
- $? You can retrieve the return value of the last command .
- 3: Nested use of functions :
#!/bin/bash
# Author: renshanwen
func_one (){
echo " The first function "
func_two
}
func_two (){
echo " The second function "
}
func_one
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 4: Function to get parameters :
func_one (){
echo " The first parameter is $1"
echo " The second parameter is $2"
}
func_one aaa bbb
[email protected]:~$ ./test.sh
The first parameter is aaa
The second parameter is bbb
[email protected]:~$
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 1.
边栏推荐
- 重排数列练习题
- Preliminary understanding of regular expressions (regex)
- leetcode:210. 课程表 II
- Access control system based on RFID
- Summary of machine learning materials
- Can flush open an account? Can you directly open the security of securities companies on the app
- Delphi XE7的蓝牙 Bluetooth
- 金融信创爆发年!袋鼠云数栈DTinsight全线产品通过信通院信创专项测试
- #886 Possible Bipartition
- Main stages of garbage collection in ZGC
猜你喜欢
随机推荐
Foreign brands become a thing of the past? What are the key words of the TV industry in 2022?
机器学习资料汇总
指针与数组&指针与const&结构体与const
ATOI super resolution
Vs2017 environmental issues
The service did not report any errors MySQL
CUDA out of memory
Do we media video, and share the necessary app for friendly new media operation
How to improve communication efficiency during home office | community essay solicitation
#113 Path Sum II
China hydraulic cylinder linear position sensor market trend report, technical dynamic innovation and market forecast
Junda technology is applicable to "kestar" intelligent precision air conditioning network monitoring
Data visualization - Calendar chart
初步了解认识正则表达式(Regex)
在同花顺开户安全么,买股票怎么网上开户
#141 Linked List Cycle
zgc的垃圾收集的主要阶段
How do testers plan for their future? To achieve 25K in 2 years?
shell语言
Yanghui triangle code implementation








