今天看了一天的数学,把《程序员的数学》第一本又过了一遍,下午又把第二本看了100多页,晚上实在看不下去了,就翻出以前没看完的Linux Shell编程
但是发现是在暑假前看的了,现在已经忘得差不多了,结果又几乎是从头开始看了,不过幸好还记得一些,前面的看起来还是挺快的。
晚上主要是看了下Shell的程序控制结构,跟C差不多的结构,并且bash还提供C风格的程序控制结构,比如if elf,for,while,case等等。
比如如果想比较一个变量和一个常量的大小的判断选择结构
#!/bin/bash
num=10
if (($num<20));then
echo $num is less than 20
elif (($num>20));then
echo $num is large than 20
else
echo $num is equal to 20
fi
输出结果:10 is less than 20
循环控制结构比如用for打印0-9
#!/bin/bash
for((i=0;i<10;i++))
do
echo $i
done
输出结果:
0
1
2
3
4
5
6
7
8
9
同理如果用while来做
#!/bin/bash
i=0
while (($i<10))
do
echo $i
i=$[$i+1]
done
输出结果:
0
1
2
3
4
5
6
7
8
9
bash也可以获取命令行参数
#!/bin/bash
echo $0 $1 $2 $3
输入:
sh test.sh
输出结果:
test.sh 0 1 2
在bash中$0就是运行的shell脚本名,$i-$n就是命令行参数
bash读取用户输入
#!/bin/bash
read i
echo $i
输入:
sh test.sh
hello
输出结果:
hello
给提示的用户输入
#!/bin/bash
read -p "请输入内容" str
echo $str
输入:
sh test.sh
请输入内容hello
输出结果:
hello
隐藏用户输入
#!/bin/bash
read -s -p "请输入内容:" str
echo $str
输入:
sh test.sh
请输入内容:
输出结果
hello