Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash脚本:要调用读取输入的函数吗_Bash_Unix_Arguments - Fatal编程技术网

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

Bash脚本:要调用读取输入的函数吗,bash,unix,arguments,Bash,Unix,Arguments,我正在编写一个脚本,它调用一个函数,该函数在多行中读取输入。我想将参数传递到读取中,但我不知道是否可以或如何传递 aka如何让enter grades将我的值作为输入,而不是在提示下等待输入 在我的bash脚本中 ... login="studentName" echo "enter score:" read score echo "comments:" read comments enter-grades $hw #---> calls another function (dont k

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

aka如何让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
不直接从终端读取,只需提供该程序标准输入的信息:

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"
引用所有变量

或者,和一个医生在一起

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

输入等级“$hw”请查看:。。。我没有
enter grades
的定义。您在上面的代码段中有一个错误,该错误将捕获(
login
赋值)和一个未定义的变量(至少在代码段中)。这里的问题是如何让
输入等级
将您的值作为输入,而不是在提示下等待输入?是的,这就是问题所在,
输入等级
不是有效的POSIX函数名,因为破折号不合法(规则与变量名相同,也不允许破折号)。从当前版本开始,Bash在使用显式的
函数
关键字时将允许此定义,但该行为没有文档记录,因此在未来的版本中可以不经通知而消失。如果您想与其他shell兼容,最好这样定义它:
enter_grades(){
--no
function
关键字。