Linux bash shell 脚本提示用户输入并存储在变量中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15751855/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 05:03:45  来源:igfitidea点击:

Linux bash shell script prompt user input and store in variable

bashvariablesuser-input

提问by michael

How do I collect user input and store in variables to use in parameters for jar file execution?

如何收集用户输入并存储在变量中以用于 jar 文件执行的参数?

Tried using read -p and bash's select command from following link but get errors: How do I prompt for Yes/No/Cancel input in a Linux shell script?here is what I have so far:

尝试从以下链接使用 read -p 和 bash 的 select 命令,但出现错误:如何在 Linux shell 脚本中提示是/否/取消输入?这是我到目前为止所拥有的:

#!/bin/bash

echo "Hello "$USER"."
echo "Enter System :"
read name
echo "Enter Format (JPG/PDF):"
read format 

java -jar ~/folder/myjar.jar -input ~/scripts/file -output$format ~/output/$name.$format

This partially works in that it produces output file but the name variable is lost, format variable is generated in string ok. Running this also generates the following errors: ": command not found" under hello user, " ': not a valid identifierame" after entering system name

这部分起作用,因为它生成输出文件但名称变量丢失,格式变量在字符串中生成。运行这个也会产生以下错误:“: command not found”在hello user下,“': not a valid identifierame”在输入系统名称后

回答by michael

Looks like you may have carriage return / windows line terminations, try to run

看起来你可能有回车/windows 行终止,尝试运行

dos2unix input.txt

or

或者

sed 's/^M$//' input.txt > output.txt

or

或者

tr -d '\r' < input.txt > output.txt