- 整理思维导图
- 统计家目录下.c文件的个数
ubuntu@ubuntu:~$ ls ~/*.c | wc -w
- 终端输入一个.sh文件,判断文件是否由可执行权限,如果有可执行权限运行脚本,没有可执行权限添加可执行权限后,再运行脚本
1 #!/bin/bash
2 read -p "请输入一个.sh文件" file
3 if [ -x "$file" ]
4 then
5 bash "$file"
6 else
7 chmod 775 "$file"
8 bash "$file"
9 fi - 写一个函数,输出当前用户uid和gid,并使用变量接收结果
1 #!/bin/bash
2 fun()
3 {
4 uid=`grep "^ubuntu" /etc/passwd -w | cut -d ":" -f 3`
5 gid=`grep "^ubuntu" /etc/passwd -w | cut -d ":" -f 4`
6 echo uid=$uid gid=$gid
7 }
8 id=`fun`
9 echo $id - 终端输入年月,判断该月有多少天,考虑闰平年的情况
1 #!/bin/bash
2 read -p "请输入年份:" year
3 read -p "请输入月份:" month
4 case $month in
5 1|3|5|7|8|10|12)
6 echo 有31天
7 ;;
8 4|6|9|11)
9 echo 有30天
10 ;;
11 2)
12 if [ $((year%4)) -eq 0 -a $((year%100)) -ne 0 -o $((year%400)) -eq 0 ]
13 then
14 echo 有29天
15 else
16 echo 有28天
17 fi
18 ;;
19 esac - 使用for循环,输出九九乘法表( printf "%d * %d = %d" $i $j $((i*j)) )
1 #!/bin/bash
2 for((i=1;i<=9;i++))
3 do
4 for((j=1;j<=i;j++))
5 do
6 printf "%d*%d=%-3d" $i $j $((i*j))
7 done
8 echo
9 done - 使用for循环,找到家目录下的所有.c文件,如果文件有内容编译该文件,如果文件中没有内容,删除文件
1 #!/bin/bash
2 for file in `ls ~/*.c`
3 do
4 if [ -s "$file" ]
5 then
6 gcc "$file"
7 else
8 rm "$file"
9 fi
10 done - 自己整理分文件编译,改之前写过的代码
6.26作业
最新推荐文章于 2024-07-14 14:55:20 发布