Home arrow Shell Programming arrow Write a procedure in UNIX Shell Scripting
Write a procedure in UNIX Shell Scripting PDF
Saturday, 23 January 2010
To create a procedure (function) in a shell script use the following example:

Example of a procedure / function in UNIX Shell Scripting
  1. #!/bin/sh
  2.  
  3. my_option="option"
  4.  
  5. my_function()
  6. {
  7. param=$1
  8. echo "now executing custom function with parameter: "$param
  9. }
  10.  
  11. my_function $my_option


See example 2 for a procedure without parameters:

Example 2: a procedure without parameters
  1. #!/bin/sh
  2.  
  3. my_function()
  4. {
  5.   echo "now executing custom function"
  6. }
  7.  
  8. my_function

 

 In next example we've defined multiple procedures:

Example 3: multiple prodcedures
#!/bin/sh

func_a ( ) {
    echo "calling func_a"
}

func_b ( ) {
    echo "calling func_b"
}

func_c ( ) {
    echo "calling func_c"
}

set -e

echo "start"
func_a
func_b
func_c
echo "done."


Last Updated ( Friday, 18 May 2012 )
 
< Prev   Next >

Other BSD Systems

OpenBSD

Misc

Solaris

Polls

Best BSD firewall?