Guest User

Untitled

a guest
Nov 15th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. # Bash Script Example
  2. The following blog post shows a sample bash script covering some inportant aspects in writing a bash script.
  3.  
  4. ## Important aspects covered in this script:
  5. - Validate if input arguments are passed
  6. - Parse positional arguments
  7. - Handle parameter values with white space
  8. - Validating the arguments (using regex)
  9. - While loop to iterate through varargs
  10. - Usage of unit functions
  11. - Usage of function that takes inputs and return value
  12. - Using -e option to fail the script on command execution errors
  13. - Using -x option to help debugging the script execution
  14. - Exit the script with success or error status
  15. - Redirect error messages to error console
  16.  
  17. ## Executing the script:
  18.  
  19. ### Method 1 (in it's own bash shell)
  20. The script can be executed as
  21.  
  22. bash calculate.sh --operation add --lhs 2 --rhs 3
  23.  
  24. This is the simplest way to execute the script because:
  25. - The script runs in its own shell
  26. - Doesn't read variables or environment variables from the main shell
  27. - Doesn't leave traces of variables or changes to environment variables after script completion
  28. - Needs only read permission to execute the script
  29.  
  30. ### Method 2 (in the main shell where it is invoked)
  31. Another way of executing the script is
  32.  
  33. ./calculate.sh --operation add --lhs 2 --rhs 3
  34.  
  35. The difference from the first style is:
  36. - The script runs in main shell where it is invoked
  37. - Shares variables or environment variables from the main shell
  38. - Leave traces of variables or changes to environment variables after script completion
  39. - Needs executable permission to execute the script (chmod u+x calculate.sh)
Add Comment
Please, Sign In to add comment