shell的应用练习
2013-05-04 16:38
225 查看
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
未完待续
————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
未完待续
相关文章推荐
- adb shell am start -d 启动应用之uri被*吃了
- linux的shell循环的应用
- 【Shell剧本练习】得出的结论是当前用户
- Linux的shell脚本grep、sed、awk练习和diff、patch、cut、sort介绍
- Shell 代码简单应用
- 5个Shell脚本编程入门练习例子
- shell脚本练习(12.12)
- 数据结构 练习21-trie的原理分析和应用
- Shell脚本应用(五)if语句
- 管理文件及正则表达式的应用练习
- 练习---shell脚本查看系统所有进程
- shell工作应用总结
- Shell应用:批量将文件编码由gbk转utf-8
- android 应用练习-----GoogleWeather
- shell基础实例应用
- shell脚本小练习--域名解析
- eclipse 用adb shell ps 命令检测某个应用的资源占用
- shell脚本实例---学习的捷径就是练习
- 网络编程应用:基于TCP协议【实现文件上传】--练习
- Shell简易学习练习