bash 使用默认值读取bash中的变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2642585/
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-09 19:11:07  来源:igfitidea点击:

Read a variable in bash with a default value

bashshell

提问by Ricardo Marimon

I need to read a value from the terminal in a bash script. I would like to be able to provide a default value that the user can change.

我需要在 bash 脚本中从终端读取一个值。我希望能够提供用户可以更改的默认值。

# Please enter your name: Ricardo^

In this script the prompt is "Please enter your name: " the default value is "Ricardo" and the cursor would be after the default value. Is there a way to do this in a bash script?

在此脚本中,提示是“请输入您的姓名:”,默认值为“Ricardo”,光标将位于默认值之后。有没有办法在 bash 脚本中做到这一点?

回答by ghostdog74

you can use parameter expansioneg

您可以使用参数扩展,例如

read -p "Enter your name [Richard]: " name
name=${name:-Richard}
echo $name

Including the default value in the prompt between brackets is a fairly common convention

在括号之间的提示中包含默认值是一个相当普遍的约定

回答by Jadav Bheda

read -e -p "Enter Your Name:" -i "Ricardo" NAME

echo $NAME

回答by Paused until further notice.

In Bash 4:

在 Bash 4 中:

name="Ricardo"
read -e -i "$name" -p "Please enter your name: " input
name="${input:-$name}"

This displays the name after the prompt like this:

这会在提示后显示名称,如下所示:

Please enter your name: Ricardo

with the cursor at the end of the name and allows the user to edit it. The last line is optional and forces the name to be the original default if the user erases the input or default (submitting a null).

将光标放在名称的末尾并允许用户对其进行编辑。最后一行是可选的,如果用户删除输入或默认值(提交空值),则强制名称为原始默认值。

回答by manish_s

Code:

代码:

IN_PATH_DEFAULT="/tmp/input.txt"
read -p "Please enter IN_PATH [$IN_PATH_DEFAULT]: " IN_PATH
IN_PATH="${IN_PATH:-$IN_PATH_DEFAULT}"

OUT_PATH_DEFAULT="/tmp/output.txt"
read -p "Please enter OUT_PATH [$OUT_PATH_DEFAULT]: " OUT_PATH
OUT_PATH="${OUT_PATH:-$OUT_PATH_DEFAULT}"

echo "Input: $IN_PATH Output: $OUT_PATH"

Sample run:

示例运行:

Please enter IN_PATH [/tmp/input.txt]: 
Please enter OUT_PATH [/tmp/output.txt]: ~/out.txt
Input: /tmp/input.txt Output: ~/out.txt

回答by sibaz

I found this question, looking for a way to present something like:

我发现了这个问题,正在寻找一种呈现以下内容的方法:

Something interesting happened.  Proceed [Y/n/q]:

Using the above examples I deduced this:-

使用上面的例子我推断出:-

echo -n "Something interesting happened.  "
DEFAULT="y"
read -e -p "Proceed [Y/n/q]:" PROCEED
# adopt the default, if 'enter' given
PROCEED="${PROCEED:-${DEFAULT}}"
# change to lower case to simplify following if
PROCEED="${PROCEED,,}"
# condition for specific letter
if [ "${PROCEED}" == "q" ] ; then
  echo "Quitting"
  exit
# condition for non specific letter (ie anything other than q/y)
# if you want to have the active 'y' code in the last section
elif [ "${PROCEED}" != "y" ] ; then
  echo "Not Proceeding"
else
  echo "Proceeding"
  # do proceeding code in here
fi

Hope that helps someone to not have to think out the logic, if they encounter the same problem

希望可以帮助某人不必考虑逻辑,如果他们遇到同样的问题

回答by bukzor

I've just used this pattern, which I prefer:

我刚刚使用了这种模式,我更喜欢:

read name || name='(nobody)'

回答by Jonathan Leffler

name=Ricardo
echo "Please enter your name: $name \c"
read newname
[ -n "$newname" ] && name=$newname

Set the default; print it; read a new value; if there is a new value, use it in place of the default. There is (or was) some variations between shells and systems on how to suppress a newline at the end of a prompt. The '\c' notation seems to work on MacOS X 10.6.3 with a 3.x bash, and works on most variants of Unix derived from System V, using Bourne or Korn shells.

设置默认值;打印出来;读取新值;如果有新值,请使用它代替默认值。关于如何在提示末尾取消换行,shell 和系统之间存在(或曾经)一些差异。'\c' 符号似乎适用于带有 3.x bash 的 MacOS X 10.6.3,并且适用于从 System V 派生的大多数 Unix 变体,使用 Bourne 或 Korn shell。

Also note that the user would probably not realize what is going on behind the scenes; their new data would be entered after the name already on the screen. It might be better to format it:

另请注意,用户可能不会意识到幕后发生的事情;他们的新数据将在屏幕上已有的名称之后输入。格式化它可能会更好:

echo "Please enter your name ($name): \c"

回答by kokane

#Script for calculating various values in MB
echo "Please enter some input: "
read input_variable
echo $input_variable | awk '{ foo =  / 1024 / 1024 ; print foo "MB" }'

回答by speefak

The -e and -t parameter does not work together. i tried some expressions and the result was the following code snippet :

-e 和 -t 参数不能一起使用。我尝试了一些表达式,结果是以下代码片段:

QMESSAGE="SHOULD I DO YES OR NO"
YMESSAGE="I DO"
NMESSAGE="I DO NOT"
FMESSAGE="PLEASE ENTER Y or N"
COUNTDOWN=2
DEFAULTVALUE=n
#----------------------------------------------------------------#
function REQUEST ()
{
read -n1 -t$COUNTDOWN -p "$QMESSAGE ? Y/N " INPUT
    INPUT=${INPUT:-$DEFAULTVALUE}
    if  [ "$INPUT" = "y" -o "$INPUT" = "Y" ] ;then
        echo -e "\n$YMESSAGE\n"
        #COMMANDEXECUTION
    elif    [ "$INPUT" = "n" -o "$INPUT" = "N" ] ;then
        echo -e "\n$NMESSAGE\n"
        #COMMANDEXECUTION
    else
        echo -e "\n$FMESSAGE\n"
    REQUEST
    fi
}
REQUEST