如何在shell编程中从键盘输入数据

Aru*_*lam 6 unix bash shell

如何在shell编程中输入键盘数据?

一些类似于c中的scanf的命令

Pas*_*TIN 16

你可以使用" read":

$ cat ./test.sh
#!/bin/sh
echo -n "enter the value : "
read my_var

echo "The value is : $my_var"
Run Code Online (Sandbox Code Playgroud)

并且,执行脚本:

$ sh ./test.sh
enter the value : 145
The value is : 145
Run Code Online (Sandbox Code Playgroud)

  • Bash:"read"有一些有用的选项,例如-p来指定提示:read -p'输入一个值:'my_var请参阅"help read"了解更多信息. (5认同)