作业:
练习1:在终端输入一个用户名
定义有参又返函数,返回用户在/etc/passwd文件中的行号
参数:用户名
返回:行号
#!/bin/bash
2
3 #2 解析器
4 read -p "请输入一个用户名" username
5
6 function Zrr()
7 {
8 #$(())不能用
9 line=`grep -n $username /etc/passwd | cut -d ":" -f 1`
10
11 }
12
13 Zrr $username
14 echo 行号是=$line
15
![](https://img-blog.csdnimg.cn/direct/3d9d5c823340443d940b8c70db032fec.png)
定义有参又返函数,返回用户在.etc.passwd文件中的解析器
参数:用户名
返回:解析器
#!/bin/bash
2
3 #2 解析器
4 read -p "请输入一个用户名" username
5
6 function Zrr()
7 {
8 #$(())不能用
9 jiexiqi=`grep -n $username /etc/passwd | cut -d "/" -f 6`
10
11 }
12
13 Zrr $username
14 echo 解析器是=$jiexiqi
15
![](https://img-blog.csdnimg.cn/direct/f9fbb0640597466da0238fda0d3e45a3.png)
定义有参又返函数,返回用户的uid和gid
参数:用户名
返回:uid gid
#!/bin/bash
2
3 read -p "请输入一个用户名" username
4
5 function Zrr()
6 {
7 #$(())不能用
8 uid=`grep -n $username /etc/passwd | cut -d ":" -f 4`
9 gid=`grep -n $username /etc/passwd | cut -d ":" -f 5`
10 }
11
12 Zrr $username
13 echo gid是=$gid
14 echo uid是=$uid
15
![](https://img-blog.csdnimg.cn/direct/5723f1abc0394c3bb87b99248c589e6f.png)
练习2:定义数组存储软件名(oneko bastet frozen-bubble)使用select..in循环数组,实现页面的交互
使用case in 判断软件是否存在(执行),正常运行则不下载,运行失败则下载并运行arar
ps查看进程
#!/bin/bash
2
3
4 arr=("oneko" "bastet" "frozen-bubble")
5
6 select varname in ${arr[*]}
7 do
8 case varname in
9 oneko)
10 if [ $? -ne 0 ]
11 then
12 echo 未安装oneko
13 sudo apt-get install oneko
14 ./oneko
15 else
16 echo 已安装
17 ./oneko
18 fi
19 ;;
20
21 basket)
22 if [ rpm -q basket | $? -ne 0 ]
23 then
24 echo 未安装oneko
25 sudo apt-get install basket
26 ./basket
27 else
28 echo 已安装
29 ./basket
30 fi
31 ;;
32
33
34 frozen-bubble)
35 if [ rpm -q frozen-bubble | $? -ne 0 ]
36 then
37 echo 未安装oneko
38 sudo apt-get install frozen-bubble
39 ./frozen-bubble
40 else
41 echo 已安装
42 ./frozen-bubble
43 fi
44 ;;
45
46
47
48
49
50 esac
51 done
练习3:输入数组元素,计算数组的最大值
1 #!/bin/bash
2
3
4 read -p "请输入数组" -a arr
5
6 max=${arr[0]}
7
8 for ((i=0;i<${#arr[*]};i++))
9 do
10 if [ $max -lt ${arr[$i]} ]
11 then
12 max=${arr[i]}
13 fi
14 done
15
16 echo $max
![](https://img-blog.csdnimg.cn/direct/1c0f6739e5c04be0b8babf07dc1ee93d.png)
练习4:打印图形
****
***
**
*
![](https://img-blog.csdnimg.cn/direct/cc55b0049a124b69aa490d516355eb28.png)
1 #!/bin/bash
2
3
4 read -p "输入n" n
5
6 for((i=1;i<=n;i++))
7 do
8 for((j=1;j<=i-1;j++))
9 do
10 echo -n " "
11 done
12 for((k=1;k<n+1-i;k++))
13 do
14 echo -n "*"
15 done
16 echo ""
17 done
![](https://img-blog.csdnimg.cn/direct/f695bc7094674513ae1989a5f6751777.png)
练习5:输入一个文件名,
判断是否在当前目录下存在,
如果是一个目录,则直接输出是目录
如果是一个链接文件则输出文件的详细信息
如果存在则判断是否是一个普通文件,如果是普通文件则判断是否具备
可读可写可执行权限,如果具备权限,写入hello,不具备权限,则添加读写执行权限,写入hello
read -p "文件名" file
5 if [ -e ./$file ]
6 then
7 if [ -d ./$file ]
8 then
9 echo 目录
10 elif [ -L ./$file ]
11 then
12 ls -lh $file
13 elif [ -f ./$file ]
14 then
15 echo 普通文件
16 if [ -r ./$file -a -w ./$file -a -x ./$file ]
17 then
18 echo hello >> $file
19 else
20 chmod 777 $file
21 echo hello >> $file
22 fi
23 fi
24 fi
![](https://img-blog.csdnimg.cn/direct/1385d00b7fd64254a07b49c66401a9cc.png)
![](https://img-blog.csdnimg.cn/direct/50f061fe43a446d0aadff252d5200582.png)
![](https://img-blog.csdnimg.cn/direct/e3f321a0aefc449398cc2bde52b2012d.png)
思维导图
![](https://img-blog.csdnimg.cn/direct/e4d838eb4cef4c7a8853d27442ad0129.png)