2

I've been working on our intro scripting assignment, and am having issues calling functions within the script. I am in the second portion of the assignment, and I am just testing to make sure what I have is (hopefully) going to work. I have gathered some directories, and ask a yes or no question. When I get a 'y', I wrote a little function that I call, and when I get a 'n' I have another function, both simple echoes. What is the issue?

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_two $@

part_five(){
    echo -n "working yes";
}

part_six(){
    echo -n "working no";
}

Any help would be greatly appreciated, as always.

1
  • 1
    We can't tell you the issue until you tell us the unwanted behavior you are experiencing Commented Dec 3, 2010 at 7:53

2 Answers 2

10

Much like in C a function must be defined before it's used. In your code snippet you are calling part_two (which is calling part_five and part_six) before declaring the two functions.

Have you tried moving their definitions to the start of the script?

EDIT:

In most cases, the best way to deal with this in Bash is to simply define all functions at the start of the script before executing any actual commands. The order of the definitions does not really matter - the shell only looks up a function when it's about to use it - so generally there are no dependency issues etc. that you may have to think about.

EDIT 2:

There are cases where you may not be able to just define a function at the start of the script. A common case is when you use conditional constructs to dynamically select or modify the declaration of a function e..g.:

if [[ "$1" = 0 ]]; then
    function show() {
        echo Zero
    }
else
    function show() {
        echo Not-zero
    }
fi

In these cases you have to make sure that each function call happens after that function (and any others that it calls) is declared.

EDIT 3:

In bash a function declaration is actually the function foo() { ... } block where you define its implementation - and yes, the function keyword is not strictly necessary. There are no function prototypes as in C - they would not make sense anyway because shell scripts are generally parsed as they are executed. Newer Bash version do read a script at once, but they mostly check for syntax errors and not for logical errors such as this one.

BTW the official term is "function declaration", but even the Bash info page uses "declaration" and "definition" interchangeably.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. We touched on functions in class, but only for moments, was not aware of their need to be defined.
@mrwienerdog: In other words, move this line to the bottom: part_two $@.
This answer saved me 10 years later. Every tutorial suggested the syntax was function_name () { ... } or function function_name { ... }, but I was getting "Unexpected line ending" as soon as I made a function. However, with function function_name () { ...} it works flawlessly. Can't believe it honestly, such a simple fix
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.