一、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