声明变量并赋值
变量名=字符串/数字
例:
引用时加$符号定义变量是不加
Int1 XXX int2
XXX
-eq 等于
-ge 大于等于
-le 小于等于
-gt 大于
-it小于
-ne 不等于
绿色为可执行文件
root下新建目录work,进入,在终端中打开
1、ls
2、aa=123
3、echo $aa
4、aa=abc
5、echo $aa
6、vi shell_001.shell
#!/bin/bash
num=120
str=”happy new year.”
echo $num
echo $str
保存退出
7、li –l
8、chmod 755 shell_001.shell
9、ls –l
10、ls
11、运行./shell_001.shell
12、结果为:120 happy new year
13、修改:echo “your age is :”$sum
echo “zhufu :”$str
14、shell_002.shell
#!/bin/bash
a=20
b=30
if [ $a –gt $b ]
then
echo “The Max Num is:”$a
else
echo “The Max Num is:”$b
fi
echo “Task is Finished.”
保存
15、ls
16、chmod 755 shell_002.shell
17、./shell_002.shell
运行结果:The Max Num is:30
Task is Finished。
18、在work目录下创建目录data
在目录data下新建data01.txt 到data09.txt
19、创建shell_003.shell
#!/bin/bash
str=”data01.txt”
if [ -f $str ]
then
echo “The File is Exist.” >> re.txt//重定向到文件re.tet中else
echo “The File is not Exist.” >> re.txt
fi
echo “Task is Finished.”
此时运行结果:The File is not Exist.
Task is Finished
在第二行后加cd /root/work/data
此时运行结果:The File is Exist
Task is Finished
重定向时,叠加用>>不追加用>,不追加“>>”可重复写,一句一句往上写,不追加即新内容覆盖原内容
20、P319 while循环要考
21、新建shell_004.shell
#!/bin/bash
n=100
i=1
sum=0
while [ $i –le $n ] //i<=n时
do
((sum=$sum+$i))
((i=$i+1))
done
echo “The Sum is:”$sum >> sum.txt
运行结果:The Sum is:5050
#!/bin/bash
i=1
fn=””
cd /root/work/data
while [ $i –le 20 ] //i<=20时
do
if [ $i –lt 10 ] //or [ $i –le 9 ]
then
fn=”data0”$i”.txt”
else
fn=”data”$i”.txt”
fi
if [ -f $fn ]
then
echo “提交终端编号:”$i >> co.txt else
echo “未提交终端编号:”$i >> noco.txt fi
((i=$i+1))
done
运行结果: