[ Simple use of shell script creation ]
All of the following operations are based on an already-established 1.sh
script
Variable declaration
The declaration of Shell variables is similar to the declaration of variables in Python, ie
变量名=值
a=123
a=123
b=456
c="hello world !"
It is worth noting that
- No spaces before and after the equal sign
- Variable naming rules, to be more standardized, can not be underlined, what starts with a question mark
- The default type of the variable is a string , even if a = 123, a is actually
“123”
, as for how to add and subtract, detailed below
Variable use
When using a variable, you cannot directly type the variable name, because we can think of the script as typing on the command line. We directly type an a. How does the system know whether to type the a command or call the a variable?
Variable references need to be prepended with a $
dollar sign to tell the system that this is a variable. In addition, if you use ${变量名}
parentheses, the name of the variable will be further refined and less prone to errors, but this bracket is not necessary
Write a simple script to verify:
#!/bin/bash
a=123
b=456
c="hello world !"
echo $a+$b
echo $c
It can be seen that a + b does not produce an addition operation, because the default variable type is a string , and our + sign is also a string, which is similar to the replacement of macro definition in C ++ . . .
Formatted output
Sometimes we want to output a description string in front of the variable. For example “这个字符串的值是: abc”
, abc is the value of a variable. At this time, when thinking of printf , you can use a method similar to printf. You can also remove the quotation marks . Definition replacement
echo "说明语句 $变量名"
echo 说明语句 $变量名
Run this script
#!/bin/bash
a=123
b=456
c="Hello World"
echo "a+b=$a+$b"
echo a+b=$a+$b
echo c is :$c
echo "c is :$c"
Addition, subtraction, multiplication and division of variables
expr statement
Use expr This is a statement to declare an expression, use the expr note 反引号
enclosed, and ~
is a key, press the shift is ~
, it does not follow that 反引号
, under the ESC
`expr $变量a 运算符 $变量b`
Modify the script just now
#!/bin/bash
a=123
b=456
c=`expr $a + $b`
echo $a+$b
echo $c
Verifying the above statement again, you can see that the first line is direct $a+$b
, which is equivalent to a simple replacement of the macro definition string. The second line uses the expr statement to really add and subtract. It is worth noting that the operator must have Space
Other operators
Add, subtract, multiply and divide
#!/bin/bash
a=123
b=10
echo a + b = `expr $a + $b`
echo a - b = `expr $a - $b`
echo a \* b = `expr $a \* $b`
echo a / b = `expr $a / $b`
echo *
Escape of multiplication
It is noteworthy that, the multiplication needed \*
to escape a direct hit *
, through all the files in the current folder name , and java is import java.xxx.*;
similar to the following example demonstrates the use of \*
importance to escape
Relationship judgment
[ $变量1 == $变量2 ] :变量1是否 [等于] 变量2, 返回true or false
[ $变量1 != $变量2 ] :变量1是否 [不等于] 变量2, 返回true or false
[ $变量1 -eq $变量2 ] :变量1是否 [等于] 变量2, 返回true or false
[ $变量1 -ne $变量2 ] :变量1是否 [不等于] 变量2, 返回true or false
[ $变量1 -gt $变量2 ] :变量1是否 [大于] 变量2, 返回true or false
[ $变量1 -lt $变量2 ] :变量1是否 [小于] 变量2, 返回true or false
[ $变量1 -ge $变量2 ] :变量1是否 [大于等于] 变量2, 返回true or false
[ $变量1 -le $变量2 ] :变量1是否 [小于等于] 变量2, 返回true or false
解释:
eq : equal 等于
ne : not equal 不等于
gt : greater than 大于
lt : less than 小于
ge : greater equal 大于等于
le : less equal 小于等于
if judgment statement
The if statement is similar to most of the statements, except that the end of the} becomes fi, which can be understood as final, the meaning of the end, remember to add fi at the end
if 条件语句
then
语句1
语句2
...
语句n
fi
You can also add an else statement
if 条件语句
then
语句1
语句2
...
语句n
else
语句1
语句2
...
语句n
fi
Demo
Execute the following script and produce the following output
#!/bin/bash
a=123
b=124
if [ $a == $b ]
then
echo a=b
echo a=b hhhhh
else
echo a!=b
echo a!=b AAhhhh
fi
Another form
It is worth noting that it is surrounded by two brackets , and then judged like a general programming language, that is, you can directly write the greater than and less than symbols
Executing the following script will produce the following output
#!/bin/bash
a=123
b=124
if (($a < $b))
then
echo a=b
echo a=b hhhhh
else
echo a!=b
echo a!=b AAhhhh
fi
loop statement
while statement
Similar to if, the ending} is replaced with done
while 条件
do
语句1
语句2
...
语句n
done
Remember that you just created 5 txt, we delete them in a loop, print the files in the current directory while deleting, execute the following script, and produce the following output
#!/bin/bash
ls
i=1
while (($i <= 5))
do
rm $i.txt
i=`expr $i + 1`
ls
done
About self-increasing, other detailed methods
i=`expr $i + 1`
let i+=1
let "i++"
((i++))
for statement
Enumerate iteration variables
By enumerating v1, v2, to achieve loop
for 迭代变量 in v1 v2 v3 ... vn
do
语句
...
语句
done
#!/bin/bash
for i in 1 2 3
do
echo $i
let "i++"
done
for + seq sequence to achieve loop
seq statement: (don't forget the backticks)
`seq 首项 公差 末项`
Execute the following script and produce the following output
#!/bin/bash
for i in `seq 0 2 8`
do
echo $i
done