eg1:
#!/bin/bash
echo -n "login:"
read name
echo -n "password:"
read password
if [ $name = "admin" -a $password = "123123" ]
then
echo "the host and password is right!"
else
echo "input is erro"
fi
该例子就是一个输入用户名和密码,查看是否匹配的例子。
遇到的问题:if和【】之间没有留空格,会报语法错误,同时括号内也要注意留空格,不然即使输入正确也会显示输入错误。
2:比较两个数的大小
#!/bin/bash
echo "Please enter two numbers:"
read a
read b
if test $a -eq $b
then echo "a equals b"
elif test $a -gt $b
then echo "a greather than b"
else echo "a less than b"
fi
3.查找root目录下是否存在该目录
#!/bin/bash
echo "Please enter your filename"
read a
if test -e/root/$a
then echo "exist the file"
else echo "not exist the file"
fi
4.查看用户是否在运行
#!/bin/bash
echo "Please enter a user"
read a
b=$(whoami)
if test $a = $b
then echo "the user is running"
else echo "the user is not running"
fi
注意事项:变量赋值时不能用空格,即b=$(whoami)等号左右不能有空格。
最后
以上就是故意奇迹最近收集整理的关于shell编程小例子及遇到的问题的全部内容,更多相关shell编程小例子及遇到内容请搜索靠谱客的其他文章。
发表评论 取消回复