Linux中shell脚本案例(一)
Shell是系统的用户界面,提供了用户与内核进行交互操作的一种接口。它接收用户输入的命令并把它送入内核去执行。
下面是案例:
1、比较两个数大小
代码:
1 2 3 4 5 6 7 8 9 10 |
#!/bin/bash echo "please enter two number" read a read b if test $a -eq $b then echo "NO.1 = NO.2" elif test $a -gt $b then echo "NO.1 > NO.2" else echo "NO.1 < NO.2" fi |
结果:
1 2 3 4 |
please enter two number 3 5 NO.1 < NO.2 |
2、模拟linnux登录shell
代码:
1 2 3 4 5 6 7 8 9 10 |
#!/bin/bash echo -n "login:" read name echo -n "password:" read passwd echo $passwd if [ $name = 'abc' -a $passwd = 'abc' ];then echo 'the host and password is right!' else echo 'input is error!' fi |
结果:
1 2 3 4 |
login:abc password:abc abc the host and password is right! |
3、查找/root/目录下是否存在该文件
代码:
1 2 3 4 5 6 7 |
#!/bin/bash echo "enter a file name:" read a if test -e /root/$a then echo "the file is exist!" else echo "the file is not exist!" fi |
结果:
1 2 3 |
enter a file name: dd the file is not exist! |
4、for循环的使用
代码:
1 2 3 4 5 6 |
#/bin/bash clear for num in 1 2 3 4 5 6 7 8 9 10 do echo "$num" done |
结果:
1 2 3 4 5 6 7 8 9 10 |
1 2 3 4 5 6 7 8 9 10 |
5、命令行输入
代码:
1 2 3 4 5 6 7 8 |
#/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 |
结果:
1 2 3 |
Please enter a user: lll the user is not running. |
- 算法之冒泡排序
- Linux中shell脚本案例(二)