Bash Programming

  • View
    1.790

  • Download
    3

Embed Size (px)

Text of Bash Programming

  • 1. Bash Programming Andrew Vandever RHC{T,E,I,X} [email_address] http://avcomp.net

2. Bash Programming Andrew Vandever Technical Training Resources Scripting Basics Expansion Loops Conditional Statements I/O Redirection User Input System V Init Scripts 3. Scripting Basics

  • Bourne Again Shell

4. Posix-Compliant 5. Interactive Shell Script 6. Automation, Repetition 7. NOT exhaustive 8. NOT covering external programs 9. Check out guides on tldp.org 10. Scripting Basics

  • Magic - #!/bin/bash

11. Add -x to debug 12. Comments - # 13. ; = EOL 14. escapes EOL 15. chmod +x 16. echo $PATH 17. Expansion

  • VARIABLE=value
  • All-caps not needed, just recommended

echo ${VARIABLE}

  • {} not always needed, but stops problems

18. $ is necessary declare -i VARIABLE=value

  • Specifies variable is an integer

19. Expansion

  • Math
  • $[]

20. $(()) Curly-brace expansion

  • {items,in,list}toexpand

21. {a..z}range 22. Expansion

  • Command Substitution
  • $(command)

23. `command` Tilde

  • ~ = your home directory

24. ~user = user's home directory Wildcard globs

  • man 7 glob

25. Expansion

  • Prevention
  • escapes a single special character

26. escapes all but $, ` and 27. '' escapes all special characters 28. Loops

  • for VAR in items in list; do commands; done

29. exit status - $? 30. test - [ condition ] 31. while [ test ]; do commands; done 32. until [ test ]; do commands; done 33. Conditional Statements

  • Operators
  • &&

34. || if [ test ]; then commands 35. elif [ test ]; then commands 36. else commands 37. fi 38. Conditional Statements

  • case VAR in

39. option)

  • commands

40. ;; *)

  • commands

41. ;; esac 42. I/O Redirection

  • Channel 2 STDERR
  • 2>

43. 2>> Channel 1 - STDOUT

  • >

44. >> 45. | 46. I/O Redirection

  • Combining 1 and 2
  • &>

47. &>> 48. 2>&1 49. >&2 Channel 0 STDIN