Shell programming
Shell script operator
Calculate 4×5÷6
Method 1:
Use $(())
A=$(((4*5)/6))
echo A=$A
Method 2:
Use $[]
B=$[(4*5)/6]
echo B=$B
Method 3:
Use expr
and need to be separated by spaces
Use backticks for assignment·
C=`expr 4 \* 5 / 6 `
echo C=$C
operation result:
bash mathrl
A=3
B=3
C=3
Variables in shell scripts
Create, output, delete variables
#!/bin/bash
#声明
#创建一个变量
#中间不能使用空格
A=100
#输出变量,使用$
echo $A
echo A=$A
echo "A=$A"
#删除变量
unset A
echo A
operation result
100
A=100
A=100
static variable
#创建静态变量
readonly B=2
#输出
echo $B
#删除变量
unset B
operation result
2
bash: unset: B:无法取消设定: 只读 variable
Static variables cannot be deleted
System variables
Variables that have been set in the system
echo $HOME
echo $USER
echo $HOME
#还有很多
You can useset
to view system variables
Environment variables
Variables (global variables) that can be used in every shell script
var1.sh
#定义一个环境变量
export EX_VAR=10
Use in another script
#加载变量
source var1.sh
#输出变量
echo $EX_VAR
run
10
Assign command to variable
A=$(ls)
echo $A
operation result
var1.sh var2.sh var.sh
Positional parameter variable
$*: Output all parameters (output as a whole)
$@: Output all parameters (output individually)
$#: Number of output parameters
#!/bin/bash
#输出参数
echo 0=$0 1=$1 2=$2 3=$3
#输出所有参数 方法一
echo 所有参数为$*
#输出所有参数 方法二
echo 所有参数为$@
#输出参数个数
echo $#
run
#传入参数运行,参数之间用空格分开
bash var2.sh 10 20 30
#结果
0=var2.sh 1=10 2=20 3=30
所有参数为10 20 30
所有参数为10 20 30
3
Predefined variables
$$: Current process PID
$!: Output the process number of the last background process
$?: Output the status of the last executed command, 0 is normal
#!/bin/bash
#输出当前进程的PID
echo 当前进程的PID为$$
#后台运行程序
gedit var.sh &
#输出当前最后一个后台的进程号
echo 最后一个后台进程号$!
#输出最后一个执行命令的状态,为0正常
echo 状态为$?
operation result
当前进程的PID为2119
最后一个后台进程号2705
状态为0
Shell script if statement
if [ 条件 ] #中括号里面一定要空格
then 命令
fi
if [ 条件 ] #中括号里面一定要空格
then 命令
elif [ 条件 ]
then 命令
...
else 命令
fi
[]Test condition parameters:
Integer judgment
parameter | Function |
---|---|
-eq | equal |
- is | not equal to |
-lt | less than |
- the | less than or equal to |
-gt | more than the |
-ge | greater or equal to |
String judgment
parameter | Function |
---|---|
= | equal |
!= | not equal to |
str1 < str2 | less than |
str1 >str2 | more than the |
str | True if not empty |
- in str | True if the length is greater than 0 |
- with p | length equals 0 is true |
Greater than less than is sorted in lexicographic order
File judgment
parameter | Function |
---|---|
-r | readable |
-In | writable |
-x | Executable |
-f | The file exists and exists |
-d | file as directory |
logic operation
parameter | Function |
---|---|
! | No |
-a | and |
-O | or |
Case 1: Judging student grades, <60 failed, >60 and <80 passed, >80 excellent, and other grades were wrong.
#!/bin/bash
if [ $1 -lt 60 -a $1 -ge 0 ]
then
echo "不及格"
elif [ $1 -lt 80 ]
then
echo "及格"
elif [ $1 -le 100 ]
then
echo "优秀"
else
echo "成绩错误"
fi
operation result
bash if1 0
不及格
bash if1 100
优秀
bash if1 120
成绩错误
Case 2: Determine file permissions, writable, readable, executable, does not exist. The output file does not exist:
#!/bin/bash
if [ -f $1 ]
then
echo -n "$1文件"
if [ -r $1 ]
then
echo -n "可读"
fi
if [ -w $1 ]
then
echo -n "可写"
fi
if [ -x $1 ]
then
echo -n "可执行"
fi
else
echo "文件不存在"
fi
echo
operation result
bash if2 txt #运行
文件不存在 #结果
touch txt #运行
bash if2 txt #运行
txt文件可读可写 #结果
Shell script case statement
The format of the case statement:
case 字符串 in
模式字符串1) 命令 ;;
模式字符串2) 命令 ;;
模式字符串3) 命令 ;;
模式字符串4) 命令 ;;
*)命令 ;;
esac
If the string is equal to the pattern string 1-4, it will be executed. If neither is satisfied, the command of ) will be executed. *
Case: Write a shell to control the file. Enter 1 to output the file content, enter 2 to delete the file, enter 3 to change the file name, and other output errors.
The procedure is as follows:
#!/bin/bash
case $1 in
1) cat $2 ;;
2) rm $2 ;;
3) mv $2 $3 ;;
*) echo "指令错误"
esac
Going as follows:
qx@qx-VirtualBox:~/桌面/Class/ifelse$ bash case 3 txt atxt
qx@qx-VirtualBox:~/桌面/Class/ifelse$ ls
atxt case if1 if2
qx@qx-VirtualBox:~/桌面/Class/ifelse$ bash case 1 atxt
abcdefg
qx@qx-VirtualBox:~/桌面/Class/ifelse$ bash case 2 atxt
qx@qx-VirtualBox:~/桌面/Class/ifelse$ ls
case if1 if2
qx@qx-VirtualBox:~/桌面/Class/ifelse$ bash case 4 atxt
指令错误
Shell script for statement
Formal first:
for 变量 in "值表"
do
命令
done
Case 2:
for (( e1;e2;e3 ))
do
命令
done
Similar to C language, such asfor (( i=0; i<100; i++ ))
Case: Sum all the numbers entered
Use method one to write:
#!/bin/bash
sum=0
for i in " $@ "
do
sum=$[$sum+$i]
done
echo $sum
Run result:
bash -x for 1 2 3 4 5
+ sum=0
+ for i in " $@ "
+ sum=1
+ for i in " $@ "
+ sum=3
+ for i in " $@ "
+ sum=6
+ for i in " $@ "
+ sum=10
+ for i in " $@ "
+ sum=15
+ echo 15
15
Use method two to write:
#!/bin/bash
num=$#
sum=0
for(( i=1; i<=num; i++ ))
do
sum=$[$sum+$i]
done
echo $sum
Run result:
bash -x for2 1 2 3 4 5
+ num=5
+ sum=0
+ (( i=1 ))
+ (( i<=num ))
+ sum=1
+ (( i++ ))
+ (( i<=num ))
+ sum=3
+ (( i++ ))
+ (( i<=num ))
+ sum=6
+ (( i++ ))
+ (( i<=num ))
+ sum=10
+ (( i++ ))
+ (( i<=num ))
+ sum=15
+ (( i++ ))
+ (( i<=num ))
+ echo 15
15
Shell script while statement
Format:
while [ 条件 ]
do
指令
done
Note: there need to be spaces between while and square brackets
Case: Sum the input data
The procedure is as follows:
#!/bin/bash
sum=0
num=0
while [ $# -ne 0 -a $num -lt $# ]
do
num=$[$num+1]
sum=$[$sum+$num]
done
echo sum=$sum
Run as follows:
bash -x while 1 2 3 4 5
+ sum=0
+ num=0
+ '[' 5 -ne 0 -a 0 -lt 5 ']'
+ num=1
+ sum=1
+ '[' 5 -ne 0 -a 1 -lt 5 ']'
+ num=2
+ sum=3
+ '[' 5 -ne 0 -a 2 -lt 5 ']'
+ num=3
+ sum=6
+ '[' 5 -ne 0 -a 3 -lt 5 ']'
+ num=4
+ sum=10
+ '[' 5 -ne 0 -a 4 -lt 5 ']'
+ num=5
+ sum=15
+ '[' 5 -ne 0 -a 5 -lt 5 ']'
+ echo sum=15
sum=15