carynbear
carynbear

Reputation: 11

Bash Script: want to call a function that reads input

I'm writing a script that calls on a function that reads for input in multiple lines. I want to pass parameters into the read, but I don't know if I can or how to.

aka how to get enter-grades to take my values as input instead of waiting for input at the prompts?

Inside my bash script

...
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

outside my bash script

#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]

Upvotes: 1

Views: 98

Answers (1)

glenn jackman
glenn jackman

Reputation: 246797

Assuming that enter-grades does not read directly from the terminal, just supply the information on that program's standard input:

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

then, group your echo commands together, and pass all that into the program:

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

or, succinctly

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

Quote all your variables.

Or, with a here-doc

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

Upvotes: 2

Related Questions