1. 写个从用户接受文件名的shell脚本。如果文件是普通文件,则显示以下信息:
————is an ordinary file-display?
如果答案是‘y’,而且文件是有可读权限的,显示文件,不
然,则显示如下信息并退出:
sorry _______has no read permission.
如果参数是个目录,在用户确认了权限之后显示那个目录
里的文件名。
如果参数不是普通或者目录文件,显示出错信息并退出。
1 echo “enter a file or directory name:”
2 read fname
3 if test -f ${fname}
4 then echo “${fname} is an ordinary file-diaplay?”
5 read ans
6 if test $ans = 'y'
7 then
8 if test -r ${fname}
9 then cat ${fname}
10 else echo “sorry,${fname} has no read permission”
11 fi
12 fi
13 elif test -d ${fname}
14 then echo “${fname} is a directory file – display?”
15 read ans
16 if test $ans = 'y'
17 then
18 if test -r ${fname}
19 then /bin/ls -l ${fname}
20 else echo “sorry,${fname} has no read permission”
21 fi
22 fi
23 else echo “${fname} is neither an ordinary file nor a directory”
24 fi
25
2.写出shell脚本接受两个参数,开始时间和结束时间。然后显示所有在这段时间里登录的用户名。
1 if test -z "$2"
2 then
3 echo "you need to supply the start time and time as parameters"
4 echo "USAGE: $0<start_time><End_Time>"
5 echo "the time should be in hh:mm format –24 hour day"
6 exit
7 fi
8 hour_after=`echo $1|cut -d: -f1`
9 min_after=`echo $1 | cut -d: -f2`
10 total_after=`expr $hour_after \* 60`
11 total_after=`expr $total_after + $min_after`
12 hour_before=`echo $2 | cut -d: -f1`
13 min_before=`echo $2 | cut -d: -f2`
14 total_before=`expr $hour_after \* 60`
15 total_before=`expr $total_before + $min_before`
16 echo "the following users have logged in between $1 and $2"
17 echo "-----------------------------------------------------------"
18 for user in `who | tr -s ' ' | cut -d ' ' -f1,5 | tr ' ' ':' `
19 do
20 hour=`echo $user | cut -d: -f2`
21 min=`echo $user | cut -d: -f3`
22 total=`expr $hour \* 60`
23 total=`expr $total + $min`
24 echo "total=$total total_after=$total_after before=$total_before"
25 if [ $total -ge $total_after -a $total -le $total_before ]
26 then
27 username=`echo $user | cut -d: -f1`
28 echo "${username}"
29 fi
30 done
31
未完待续
转载于:https://blog.51cto.com/littershare/1193044