一、创建名为CollectFirstLine的shell脚本,能够将当前文件夹下所有可读文件的第一行内容写入指定的文件(要求文件名在运行时输入)。
1 #!/bin/bash
2 IFS=$'\n'
3 for file in ./text/*
4 do
5 if [ -r "$file" ]
6 then
7 for states in $(head -1 $file)
8 do
9 echo "$states" >> a.txt
10 done
11 fi
12 done
二、创建名为Calculator的shell脚本,能够对两个整数进行加、减、乘、求商、求余五种运算,要求运算类别必须以命令行选项-a、-s、-m、-c、-r的方式指定,两个整数作为命令行参数输入。
1 #!/bin/bash
2 IFS=$'\n'
3 param1="$2"
4 param2="$3"
5 result=""
6 while [ -n "$1" ]
7 do
8 case "$1" in
9 -a) result=$[$param1 + $param2]
10 echo "The add result is $result";;
11 -s) result=$[$param1 - $param2]
12 echo "The sub result is $result";;
13 -m) result=$[$param1 * $param2]
14 echo "The multi result is $result";;
15 -c) result =$[$param1 / $param2]
16 echo "The c result is $result";;
17 -r) result=$[$par1 % $param2]
18 echo "The rest result is $result";;
19 esac
20 break
21 done