一、Shell 脚本编写
1、提示用户输入一个字符串,如果是 hello,打出 yes,并每秒输出 "hello,world",否则就输出 no,实现如下:
#!/bin/bash
#Filename:hello.sh
echo "Please input 'hello'"
read -p "> " str // 得到键盘输入
echo $str
if [ "$str" == hello ]
then
echo "yes"
while true
do
echo "hello, world"
sleep 1 // 睡眠1秒
done
else
echo "no"
fi
2、实现一个 find.sh,运行 ./find.sh /tmp/test.txt ,当 /tmp/test.txt 为文件且存在时输出 yes, 否则输出 no,实现如下:
#!/bin/bash
#Filename:find.sh
args1=$1 ( $0 为要执行的文件路径,$1 为输入的第一个参数,以此类推)
if [ -e "$args1" ] // 如果该变量代表的值是一个文件
then
echo "yes"
else
echo "no"
fi

本文介绍了如何在Linux环境下编写Shell脚本,包括接收用户输入并判断条件、检查文件存在性、实现ping命令控制以及设置脚本开机自启动的方法。示例脚本包括hello.sh、find.sh、ping.sh,详细解释了每个脚本的功能和实现逻辑。
最低0.47元/天 解锁文章
29万+

被折叠的 条评论
为什么被折叠?



