练习1 a.,提示用户输入一个用户名; b, 显示一个菜单给用户,形如: U|u show UID G|g show GID S|s show SHELL Q|q quit c,提醒用户给的是一个非上述所提示的选项,则提醒用户给出的选项错误,并请其重新选择后执行;
[root@localhost mscripts]# cat lx36.sh
#!/bin/bash read -p "Please give a username:" USERS cat << EOF U|u show UID G|g show GID S|s show SHELL Q|q quit EOF read -p "Please give a option:" CHOICE while :; do case $CHOICE in u|U) id -u $USERS break ;; g|G) id -g $USERS break ;; s|S) A=cat /etc/passwd | grep "$USERS" | awk -F: '{printf $7}'
echo "$A" break ;; q|Q) echo "quit.." exit 7 ;; *) read -p "Mistake! Please give a option again:" CHOICE esac done
练习2 1,判断一个指定的bash 脚本是否有语法错误;如果有错误,则提醒用户键入Q或者q无视错误并退出;其它任何键可以通过vim打开这个指定的脚本; 2,如果用户通过vim打开编辑后保存退出时仍然有错误,则重复第1步中的内容;否则,就正常关闭退出; [root@VM_0_6_centos script]# cat lx37.sh #!/bin/bash read -p "Please follow a file:" FILE while bash -n $FILE &> /dev/null; do read -p "Please input choice.q|Q:" CHOICE case $CHOICE in q|Q) echo "quit..." exit 0 ;; *) vim + $FILE ;; esac read -p "Please input choice again:" CHOICE done
原文链接:https://www.f2er.com/centos/374952.html