exploring-bash.bash 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #!/bin/bash
  2. function hello () {
  3. pattern1='[hell]*o'
  4. pattern2='g[o]*dbye'
  5. if [[ $1 == $pattern1 ]] ; then
  6. echo "$1 to you to!" ;
  7. elif [[ $1 == $pattern2 ]] ; then
  8. echo "well $1 to you too." ;
  9. else echo "what?" ;
  10. fi
  11. }
  12. # this one is not working like the one above it is...
  13. function otherHello () {
  14. pattern1='[hell]*o'
  15. pattern2='g[o]*dbye'
  16. case "$1" in
  17. $pattern1 | $pattern2 | $1 | "hello") echo "well $1 to you too." ;;
  18. *) echo "you did not say hello or goodbye" ;;
  19. esac
  20. echo "$1"
  21. }
  22. echo "enter your first name."
  23. read NAME
  24. # this is supposed to tell you that you have a good name if you only use letters and no spaces or numbers...
  25. # but if you enter "snttnh nhnthuo nhtht" It says that is a good name. weird.
  26. if [[ $NAME == [a-zA-Z]* ]] ; then
  27. echo "That looks like a good name";
  28. else echo "That does not look like a good name";
  29. fi
  30. echo "insert your age"
  31. read AGE
  32. PATTERN='[0-9]*'
  33. if [[ $AGE == $PATTERN ]] ; then
  34. if (($AGE < 5)) ; then
  35. echo "Are you old enough to play on a computer?";
  36. elif (($AGE >= 5 && $AGE < 25)) ; then
  37. echo "right on man!";
  38. elif (($AGE >= 25 && $AGE < 60)) ; then
  39. echo "looks like you are approaching adulthood";
  40. elif (($AGE >= 60 && $AGE < 100)) ; then
  41. echo "You might be very wise.";
  42. elif [ $AGE -gt 100 ] ; then
  43. echo "how are you still alive?";
  44. else echo "something weird happened";
  45. fi
  46. else echo "you were supposed to enter a number you silly goose.";
  47. fi
  48. if ! [ 5 -gt 6 ] ; then
  49. echo "5 is not > 6 ";
  50. fi
  51. # this creates a coprocess that is named thisProcess.
  52. coproc thisProcess for (( number=5 ; number<10 ; number++ )) ;
  53. do echo "You will never see this message." ;
  54. done
  55. echo "say hello or goodbye"
  56. read helloOrGoodbye
  57. hello $helloOrGoodbye
  58. otherHello $otherHello
  59. echo "Your first argument is $1"
  60. echo "Your arguments were $@"
  61. case "$#" in
  62. 0) echo "You had 0 arguments" ;;
  63. 1) echo "You have 1 argument" ;;
  64. *) echo "you had more than 1 argument" ;;
  65. esac
  66. echo "The had $# arguments"
  67. echo "This shell's process id is $$"
  68. echo "The most recent background job was $!"
  69. echo "The last argument of the previous command was: $_"
  70. array=(0 1 2 3 4 5 6 a b c d)
  71. echo "The 7th element and on of the array is ${array[@]:7}"
  72. echo "The 7th element of the array is ${array[@]:7:1}"
  73. array2=(10 9 8 7 6 5 4 3 2 1)
  74. echo ${!array*}
  75. echo ${!array@}
  76. echo "array2's indices are ${!array2[@]}"
  77. echo "array2's indices are ${!array2[*]}"
  78. string="Hello"
  79. echo "The length of string is ${#string}"