我需要在bash脚本中从终端读取一个值.我希望能够提供用户可以更改的默认值.
# Please enter your name: Ricardo^
Run Code Online (Sandbox Code Playgroud)
在此脚本中,提示符为"请输入您的姓名:",默认值为"Ricardo",光标将在默认值之后.有没有办法在bash脚本中执行此操作?
我有一个shell脚本,我想在脚本执行时提示用户输入一个对话框.
示例(脚本启动后):
"Enter the files you would like to install : "
user input : spreadsheet json diffTool
where $1 = spreadsheet, $2 = json, $3 = diffTool
Run Code Online (Sandbox Code Playgroud)
然后遍历每个用户输入并执行类似的操作
for var in "$@"
do
echo "input is : $var"
done
Run Code Online (Sandbox Code Playgroud)
我将如何在我的shell脚本中执行此操作?
先感谢您
我的应用程序安装程序使用标准的开放式 DMG,拖动到“应用程序”进行安装,但我想更新$PATH以便我的应用程序可以从命令行使用。
我认为这样做的正确方法是调用第一次我的应用程序运行时创建一个文件,一个脚本myapp在/etc/paths.d与文本/Applications/myapp/bin跟着一个换行符(ASCII 13):
rm /etc/paths.d/myapp
echo "/Applications/myapp/bin" > /etc/paths.d/myapp
Run Code Online (Sandbox Code Playgroud)
目前我收到错误;
rm: /etc/paths.d/myapp: No such file or directory
./myapp.sh: line 2: /etc/paths.d/myapp: Permission denied
Run Code Online (Sandbox Code Playgroud)
我需要触发用户输入管理员密码的请求,但我不确定如何以这种方式清楚地告知用户我对他们的系统进行了哪些更改以及原因。(我可以将它添加到手册中,但谁会阅读)
有什么建议?
PS 我需要在 linux(希望类似)和 Windows 上做同样的事情,但是如果我可以对 MacOS 进行排序,希望我知道从哪里开始。
我是这样想的:
read -p "Make this rsync backup session a dry run [Y/n]? " -i '--dry-run' dry_run
echo "$dry_run"
Run Code Online (Sandbox Code Playgroud)
...如果我只是点击回复提示,则会输出--dry-run为其“默认值” 。Enter但是,事实并非如此。它输出一个换行符。
其目的是什么-i以及如何运作?
从help read:
-i text use TEXT as the initial text for Readline
Run Code Online (Sandbox Code Playgroud)
对于任何想了解我在哪里学习如何向用户发出 bash 提示的人:How do I read user input into a variable in Bash?