4、写一个脚本
(1) 能接受四个参数:start, stop, restart, status
start: 输出“starting 脚本名 finished.”
…
(2) 其它任意参数,均报错退出;
#!bin/bash read -p "Enter a option: " option prog=$(basename $0) case "$option" in start) echo "start $prog finished" ;; stop) echo "stop $prog finished" ;; restart) echo "restart $prog finished" ;; status) echo "status $prog" ;; *) echo "wrong" exit 3 ;; esac
5、写一个脚本,判断给定的用户是否登录了当前系统;
(1) 如果登录了,则显示用户登录,脚本终止;
(2) 每3秒钟,查看一次用户是否登录;
#!/bin/bash # read -p "Enter a user name: " username while true; do if who | grep "^$username" &> /dev/null; then break fi sleep 3 done echo "$username logged on." >> /tmp/user.log
6、写一个脚本,显示用户选定要查看的信息;
cpu) display cpu info
mem) display memory info
disk) display disk info
quit) quit
非此四项选择,则提示错误,并要求用户重新选择,只到其给出正确的选择为止;
#!/bin/bash # cat << EOF cpu) show cpu information; mem) show memory information; disk) show disk information; quit) quit ============================ EOF read -p "Enter a option: " option while [ "$option" != 'cpu' -a "$option" != 'mem' -a "$option" != 'disk' -a "$option" != 'quit' ]; do read -p "Wrong option, Enter again: " option done if [ "$option" == 'cpu' ]; then lscpu elif [ "$option" == 'mem' ]; then cat /proc/meminfo elif [ "$option" == 'disk' ]; then fdisk -l else echo "Quit" exit 0 fi
7、写一个脚本
(1) 用函数实现返回一个用户的UID和SHELL;用户名通过参数传递而来;
(2) 提示用户:输入一个用户名或输入“quit”退出;
当输入的是用户名,则调用函数显示用户信息;当用户输入quit,则退出脚本;
进一步地:显示键入的用户相关信息后,再次提醒输出用户名或quit
#!/bin/bash # function showinfo { if id $name &> /dev/null;then cat /etc/passwd | grep -E '^('$name')\>' |awk -F: '{print $3,$NF}' read -p "Enter a username or quit:" name else read -p "wrong argument,plsease enter a username or quit:" name fi } read -p "Enter a username or quit:" name while [ "$name" != "quit" ];do showinfo $name done
原创文章,作者:mississippi,如若转载,请注明出处:http://www.178linux.com/45232