Saturday, November 30, 2019

[Unix / Linux Shell Tutorial ] Function

  1. #!/bin/bash
  2. # Basic function
  3. print_something () {
  4. echo Hello I am a function
  5. }
  6. print_something
  7. print_something


  1. ./function_example.sh
  2. Hello I am a function
  3. Hello I am a function


Passing Arguments

It is often the case that we would like the function to process some data for us. We may send data to the function in a similar way to passing command line arguments to a script. We supply the arguments directly after the function name. Within the function they are accessible as $1, $2, etc.
  1. #!/bin/bash
  2. # Passing arguments to a function
  3. print_something () {
  4. echo Hello $1
  5. }
  6. print_something Mars
  7. print_something Jupiter

  1. ./arguments_example.sh
  2. Hello Mars
  3. Hello Jupiter

Return Values

Most other programming languages have the concept of a return value for functions, a means for the function to send data back to the original calling location. Bash functions don't allow us to do this. They do however allow us to set a return status. Similar to how a program or command exits with an exit status which indicates whether it succeeded or not. We use the keyword return to indicate a return statu
  1. #!/bin/bash
  2. # Setting a return status for a function
  3. print_something () {
  4. echo Hello $1
  5. return 5
  6. }
  7. print_something Mars
  8. print_something Jupiter
  9. echo The previous function has a return value of $?

Line 11 - Remember that the variable $? contains the return status of the previously run command or function.
  1. ./return_status_example.sh
  2. Hello Mars
  3. Hello Jupiter
  4. The previous function has a return value of 5

Example2
  1. #!/bin/bash
  2. # Setting a return value to a function
  3. lines_in_file () {
  4. cat $1 | wc -l
  5. }
  6. num_lines=$( lines_in_file $1 )
  7. echo The file $1 has $num_lines lines in it.

Let's break it down:
  • Line 5 - This command will print the number of lines in the file referred to by $1.
  • Line 8 - We use command substitution to take what would normally be printed to the screen and assign it to the variable num_lines
  1. cat myfile.txt
  2. Tomato
  3. Lettuce
  4. Capsicum

  5. ./return_hack.sh myfile.txt
  6. The file myfile.txt has 3 lines in it.

Example3
  1. #!/bin/bash
  2. # Experimenting with variable scope
  3. var_change () {
  4. local var1='local 1'
  5. echo Inside function: var1 is $var1 : var2 is $var2
  6. var1='changed again'
  7. var2='2 changed again'
  8. }
  9. var1='global 1'
  10. var2='global 2'
  11. echo Before function call: var1 is $var1 : var2 is $var2
  12. var_change
  13. echo After function call: var1 is $var1 : var2 is $var2

  1. ./local_variables.sh
  2. Before function call: var1 is global 1 : var2 is global 2
  3. Inside function: var1 is local 1 : var2 is global 2
  4. After function call: var1 is global 1 : var2 is 2 changed again

No comments:

Post a Comment

Back to Top