Bash脚本:想要调用读取输入的函数

时间:2015-07-13 18:14:33

标签: bash unix arguments

我正在编写一个脚本,该脚本调用一个函数来读取多行输入。我想将参数传递给阅读,但我不知道我是否可以或如何使用。

又如何让输入等级将我的值作为输入而不是在提示处等待输入?

在我的bash脚本中

...
login="studentName"
echo "enter score:"
read score 
echo "comments:"
read comments
enter-grades $hw #---> calls another function (dont know definition)
#
# i want to pass parameters into enter-grades for each read
echo "$login" #---> input to enter-grade's first read
echo "$score $comments" #---> input to enter-grade's second read
echo "." #---> input to enter-grade's third read
在我的bash脚本之外

#calling enter-grades
> enter-grades hw2
Entering grades for assignment hw2.
Reading previous scores for hw2...
Done.
Enter grades one at a time.  End with a login of '.'
Login: [READS INPUT HERE]
Grade and comments: [READS INPUT HERE]
Login: [READS INPUT HERE]

1 个答案:

答案 0 :(得分:1)

假设enter-grades没有直接从终端读取,只需提供有关该程序标准输入的信息:

login="studentName"
read -p "enter score: " score 
read -p "comments: " comments

然后,将echo命令组合在一起,并将所有这些命令传递给程序:

{
    echo "$login"
    echo "$score $comments"
    echo "."
} | enter-grades "$hw"

或者,简洁

printf "%s\n" "$login" "$score $comments" "." | enter-grades "$hw"

引用所有您的变量。

或者,使用here-doc

enter-grades "$hw" <<END
$login
$score $comments
.
END