快速分配变量'来自bash终端

时间:2016-05-05 05:30:02

标签: bash variables terminal

现在我的bash脚本中有以下简单变量

# Please set you name and surname as well as your login on CICADA cluster

prenom="gleb"
nom="novikov"

# Are you Lady or Sir ?
hah="Sir"

然后将在执行期间使用,在终端窗口中生成不同的有趣注释

echo "It's nice to meet you ${hah} ${nom} ${prenom}!"

现在我需要添加到我的脚本smth,它会在执行时问我,我的名字是什么? 我的姓是什么? 你是女士还是先生?

并将所有三个答案保存在变量$ {hah} $ {nom} $ {prenom}中,该变量仅在此终端会话期间使用!

感谢任何优雅和简单的解决方案!

1 个答案:

答案 0 :(得分:4)

你需要的是read内置的shell来读取输入文本并将它们存储到变量中。有关详细信息,请参阅help read

read -p "Enter your 1st name: " prenom
read -p "Enter your surname: " nom
read -p "Are you Lady or Sir? : " hah
echo "It's nice to meet you ${hah} ${nom} ${prenom}!"

输出:

$ ./script.bash 
Enter your 1st name: gleb
Enter your surname: novikov
Are you Lady or Sir? : Sir
It's nice to meet you Sir novikov gleb!
$