shell练习

判断用户输入是否是整数

[root@mapogo    Thu Jun 23 18:06:39   
 /tist_shell]# cat zhengshu.sh 
#!/bin/bash
read -p "请输入数字:" num1

if [[ $num1 =~ ^[0-9]+$ ]]
then
        echo "您输入的是整数:$num1"
else 
        echo "您输入的不是整数,请重新输入"

fi

限制程序的参数输入个数

只能输入两位
[root@mapogo    Thu Jun 23 21:09:37   
 /tist_shell]# cat canshu.sh 
#!/bin/bash

p=$1
d=$2
s=$3
if [ $# -ne  2 ]
then
        echo "只能输入两位参数 "
else 
        echo -e "参数1:$1\n参数2:$2"
fi

输入小于两位
[root@mapogo    Thu Jun 23 21:17:42   
 /tist_shell]# cat canshu.sh 
#!/bin/bash

p=$1
d=$2
s=$3
if [ $# -le  2 ]
then
        echo -e "参数1:$1\n参数2:$2"
else 
        echo "只能输入小于等于两位参数 "
fi

只能大于3位
[root@mapogo    Thu Jun 23 21:23:36   
 /tist_shell]# cat canshu.sh 
#!/bin/bash

p=$1
d=$2
s=$3
if [ $# -ge  3 ]
then
        echo -e "参数1:$1\n参数2:$2\n参数3:$3"
else 
        echo "只能输入大于等于两位参数 "
fi

6、开发登录程序小脚本。

[root@mapogo    Thu Jun 23 22:20:46   
 /tist_shell]# cat dl.sh 
#!/bin/bash
read -p "输入用户名:" dd
read -p "输入密码:" pwd

if [[ $dd == "afei" ]]  &&  [  $pwd == "111111" ]
then
     echo "欢迎使用"
else  
     echo "用户名有误"
fi

[root@mapogo    Thu Jun 23 23:18:06   
 /tist_shell]# cat ssh.sh 
#!/bin/bash
read -p "输入用户名:" dd
read -p "输入密码:" pwd

if [[ $dd == $(cat /etc/passwd | awk -F ':' '{print $1}' | grep $dd) ]]  &&  [  $pwd == "111111" ]
then
     echo "欢迎使用"
else  
     echo "用户名有误"
fi

posted @ 2022-08-03 20:55  张开嘴  阅读(37)  评论(0编辑  收藏  举报