shell编程——Shell读取用户输入

该博客介绍了如何在Shell脚本中使用read命令来读取用户的输入,包括普通输入、密码输入以及带有提示的输入。还展示了如何检查输入的路径是否存在,并基于用户输入的路径安装nginx的示例。此外,还提到了read命令的返回值以及使用read-p和read-s选项来增强用户交互体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

读取用户输入

选择性输入,例如yes or no

密码输入,不适合回显

read命令读取用户输入,输入的内容放到变量里

read name

echo "read name, name is $name"

read age

echo "read age, age is $age"

read命令的返回值,只有输入ctrl+d才会返回非0

read name

echo $?

read -p选项可提示用户输入啥内容

read -p "Please input your name: " name

echo "read name, name is $name"

read -p "Please input your age: " age

echo "read age, age is $age"

read -s选项输入密码

read -s -p "Please input your password: " mypwd

echo "read pwd, pwd is $mypwd"

更改nginx安装脚本,支持输入安装目录

read -p "Please input you installpath: " installpath

if [ -e "$installpath" ];then

  echo "nginx is installed. exit script"

  exit 1

fi

nginxdownloadurl="http://nginx.org/download/nginx-1.12.2.tar.gz"

yum -y install wget gcc pcre pcre-devel zlib zlib-devel

cd /usr/local/src/

wget "$nginxdownloadurl"

tar -zxvf nginx-1.12.2.tar.gz

cd nginx-1.12.2

./configure --prefix=$installpath && make && make install

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DB实践

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值