【发布时间】:2015-10-02 03:21:20
【问题描述】:
我正在编写一个脚本,该脚本调用一个读取多行输入的函数。我想将参数传递给读取,但我不知道我是否可以或如何。
又名如何让 enter-grades 将我的值作为输入而不是等待提示输入?
在我的 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]
【问题讨论】:
-
...我没有
enter-grades的定义 -
您在上面的 sn-p 中有一个错误,shellcheck.net 将捕获(
login赋值)和一个未定义的变量(至少在 sn-p 中)。这里的问题是如何让enter-grades将您的值作为输入而不是等待提示输入? -
是的,这就是问题
-
顺便说一下,
enter-grades不是有效的 POSIX 函数名称,因为破折号是不合法的(规则与变量名称相同,也不允许破折号)。从当前版本开始,Bash 将在使用显式function关键字时允许此定义——但该行为未记录在案,因此在未来版本中可能会在不通知的情况下消失。如果您想与其他 shell 兼容,最好这样定义:enter_grades() {-- 也没有function关键字。