1、编写一个脚本,由用户输入姓名,年龄,邮箱。例:姓名:laowang 年龄:30 邮箱:[email protected] # 如果年龄不输入,默认为20岁。

脚本要求 1、只能用root执行 2、脚本只能放在/opt目录下才能被执行 3、当用户输入的年龄大于18岁,输出欢迎光临信息,否则输出,请尽快退出。

欢迎光临信息:

姓名:laowang

年龄:30

邮箱用户名:12345
#!/usr/bin/bash
if [[ $UID -ne 0 ]];then
echo “请用root用户执行”
#!/usr/bin/bash
if [[ UIDne0]];thenecho"root"exit1fidir=UID -ne 0 ]];then echo "请用root用户执行" exit 1 fi dir=(cd $(dirname $0 ) ; pwd)
if [ dir!=/opt];thenecho"/opt/"exit1fireadp":"namereadp":"agereadp":"mailname=dir != '/opt' ];then echo "请在/opt/下执行" exit 1 fi read -p "请输入姓名:" name read -p "请输入年龄:" age read -p "请输入邮箱:" mail name={name:=‘匿名’ }
if [[ ! $age -gt 0 ]]; then
age=20
fi

mail=echo $mail | grep '@'
if [[ mail=""]];thenmail=""elsemail=mail = "" ]]; then mail="邮箱地址不合法" else mail={mail%@*}
fi
xinxi="
姓名:KaTeX parse error: Undefined control sequence: \n at position 6: name \̲n̲ ̲年龄:age \n
邮箱:$mail \n
"
if [[ agelt18]];thenecho"退"elseechoe"age -lt 18 ]]; then echo "请尽快退出" else echo -e "xinxi"
fi
入门shell脚本之小试牛刀2
入门shell脚本之小试牛刀2

相关文章: