将一个脚本保存为带有扩展名的文件,如 test1.sh,
内容,例如:
#/bin/bash
echo -n "enter login name:"
read name
echo -n "enter password:"
read pwd
if [ $name = "abc" -a $pwd = "123" ] # 模拟登陆输入,需要输入 abc/123
then
echo -e "the login name and password are right! \n" # echo -e " xxxxx" 打印换行
else
echo -e "the input is wrong. \n"
fi
在终端上输入: chmod +x test1.sh,赋予可执行权限。
在终端上输入: ./test1.sh 或者 sh test1.sh 就能运行成功。
另外,如果脚本文件没有扩展名的话,在终端上输入: ./test1 或者 test1 脚本都能执行成功。