从读取命令获取值并分配给变量
Get value from Read command and assign to a variable
我有一个简单的脚本,如下所示:
#!/bin/bash
NUM=$(read -p "Number: ")
echo $NUM
当我从命令行 运行 执行此操作时,我似乎没有得到任何值
如何在脚本中获取需要 运行 的读取命令的结果,将其保存到一个变量,然后将该变量输出到屏幕上?
如果一切都失败了,请尝试文档。来自 read --help
:
Reads a single line from the standard input, or from file descriptor FD
if the -u option is supplied. The line is split into fields as with word
splitting, and the first word is assigned to the first NAME, the second
word to the second NAME, and so on, with any leftover words assigned to
the last NAME. Only the characters found in $IFS are recognized as word
delimiters.
If no NAMEs are supplied, the line read is stored in the REPLY variable.
所以:
read -p "Number: " NUM
echo $NUM
应该做你想做的。或者
read -p "Number: "
echo $REPLY
会做同样的事情,只是少打字。
我有一个简单的脚本,如下所示:
#!/bin/bash
NUM=$(read -p "Number: ")
echo $NUM
当我从命令行 运行 执行此操作时,我似乎没有得到任何值 如何在脚本中获取需要 运行 的读取命令的结果,将其保存到一个变量,然后将该变量输出到屏幕上?
如果一切都失败了,请尝试文档。来自 read --help
:
Reads a single line from the standard input, or from file descriptor FD if the -u option is supplied. The line is split into fields as with word splitting, and the first word is assigned to the first NAME, the second word to the second NAME, and so on, with any leftover words assigned to the last NAME. Only the characters found in $IFS are recognized as word delimiters. If no NAMEs are supplied, the line read is stored in the REPLY variable.
所以:
read -p "Number: " NUM
echo $NUM
应该做你想做的。或者
read -p "Number: "
echo $REPLY
会做同样的事情,只是少打字。