您的位置:首页 > 运维架构 > Shell

SHELL编程几个小例子

2016-12-15 23:26 211 查看
1.比较两个数大小

#/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


输出结果:



3.查找/root/目录下是否存在该文件

#/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


输出结果:



4.for循环的使用

#/bin/bash
clear
for num in 1 2 3 4 5 6 7 8 9 10
do
echo "$num"
done


输出结果:



5.命令行输入

#/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


输出结果:



6.从0.sh中读取内容并打印

#/bin/bash
while read line
do
echo $line
done < 0.sh


输出结果:



7.打印当前用户

#/bin/bash
echo "Current User is :"
echo $(ps | grep "$$" | awk '{print $2}')


输出结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: