Explanation
Running your provided script file (here named question.sh) with at least one parameter, e.g. ./question abc, then answering the question, results in either message on stderr:
- when answering
y: question.sh: line 15: part_five: command not found
- when providing
n: question.sh: line 12: part_six: command not found
This is because (roughly speaking) Bash executes "commands" as soon as they are encountered. So in your script, as soon as Bash reaches those lines 15 or 12, Bash has not processed the declarations for shell functions part_five or part_six; their declarations begin at lines 22 and 26, respectively.
Bash will go through a couple of "search strategies" for finding a "command" by name, ultimatively trying to find an executable by that name available on $PATH.
As there are (most likely) no executables by the name part_five or part_six, Bash didn't find something to execute, resulting in the "command not found" stderr message (and returning to your script with exit status 127).
As a comparison, on the interactive Bash prompt, trying to execute part_five or part_six will (most likely) give the same failure message and exit status 127:
$ part_five
bash: part_five: command not found
$ echo $?
127
Solution
One solution is to move the shell function declarations for part_five or part_six before the part_two $@ command; this allows Bash to see and "learn" the declarations for shell functions part_five or part_six before their execution:
part_two(){
answer=""
for value in "$@";do
echo "$value"
while [ "$answer" != "y" -a "$answer" != "n" ]
do
echo -n "Would you like to save the results to a file? (y/n): "
read answer
done
if [ "$answer" = "n" ]
then
part_six
elif [ "$answer" = "y" ]
then
part_five
fi
done
}
part_five(){
echo -n "working yes";
}
part_six(){
echo -n "working no";
}
part_two $@
Coincidentally, this follows a best practice from Google's Bash Shell Style Guide – wrapping the "main" part of a script file in a dedicated main function, then declaring other functions, then ending the script with main "$@":
#!/bin/bash
set -e
# main function idiom
# see https://google.github.io/styleguide/shellguide.html#main
main() {
echo "before"
local exit_status
after_main_function_declared_function
# execution of above 'after_main_function_declared_function' will succeed
exit_status="$?"
echo "after, exit status=${exit_status}"
}
after_main_function_declared_function() {
echo "this comes from function 'after_main_function_declared_function'"
}
main "$@"
Especially for longer script files, this main function idiom puts the main script task at the beginning of the file, and places the "finer details" in reusable functions below it – just as it is done in other programming languages.