bashscript.txt 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. shebang:
  2. #!/bin/bash
  3. #!/usr/bin/env bash - highly prefered
  4. Bash is basically a glue to the C programs.
  5. Bash is case sensitive.
  6. 1. Variables
  7. var1="good"
  8. echo $var1
  9. when you do variable assignment in bash no space around in eqaul sign.
  10. var2 = "bad"
  11. echo $var2
  12. 2. Command substitution
  13. built-in bash commands assignment to a variable:
  14. my_dir=$(pwd)
  15. my_dir=`pwd`
  16. 3. Arrays
  17. arr=(hello universe)
  18. echo ${arr[0]}
  19. echo ${arr[1]}
  20. hello
  21. universe
  22. arr="hello universe"
  23. echo ${arr[0]}
  24. echo ${arr[1]}
  25. hello universe
  26. count number of elements in an array
  27. arr=(hello universe)
  28. echo ${#arr[*]}
  29. 2
  30. 4. Quoting
  31. space is a delimiter in bash
  32. in bash single quote is a literal string - i.e. prints exactly the string contents
  33. echo "$(pwd)"
  34. /home/user
  35. echo '$(pwd)'
  36. $pwd
  37. 5. Math
  38. result=$((4 + 6))
  39. echo $result
  40. 10
  41. echo "$result"
  42. 10
  43. 6. Tests statements
  44. null string
  45. echo $([ -z "hi" ])
  46. echo $([ -z "" ])
  47. echo $([ ! -z "" )]
  48. execute echo command only if [ -z "" ] is true (&& - and, || - or)
  49. [ -z "" ] && echo "hi"
  50. execute both commands
  51. [ -z "" ]; echo "hi"
  52. 7. if statements
  53. if the string is null then echo "hi"
  54. if [ -z "hi" ]; then echo "hi"
  55. fi
  56. if [ -z "hi" ]
  57. then
  58. echo "hi"
  59. fi
  60. if [ -z "" ]; then echo "hi"
  61. fi
  62. if [ -z "" ] && [ 0=0 ]; then echo "hi"
  63. fi
  64. if [ -z "" ] || [ 0=0 ]; then echo "hi"
  65. fi
  66. don't do these: (not posix)
  67. [[ test1 && test2 ]]
  68. [[ test1 || test2 ]]
  69. 8. case statements
  70. filetype=".md"
  71. filetype=".tex"
  72. filetype=".rmd"
  73. filetype=".Rmd"
  74. filetype=".sh"
  75. case $filetype in
  76. *.md) echo "You chose markdown" ;;
  77. *.tex) echo "You chose a TeX file! | sed 's/!/, and LaTeX is best/g' ;;
  78. *.[rR]md) echo "You chose R Markdown" ;; # regex statement
  79. *) echo "You chose nothing, this is the default" ;;
  80. esac
  81. sed 's/!/, and LaTeX is best/g' # (s-substitute) ! (with) , and LaTex is best (g-globally)
  82. 9. Loops
  83. -le = less then equal to
  84. -ge = greater then equal to
  85. -lt = lesser then
  86. -gt = greater then
  87. while loops
  88. counter=1
  89. while [ $counter -le 10 ]
  90. do
  91. echo $counter
  92. ((counter++))
  93. done
  94. until loops
  95. counter=1
  96. until [ $counter -ge 10 ]
  97. do
  98. echo $counter
  99. ((counter++))
  100. done
  101. for loop
  102. names='raman lakshman'
  103. for name in $names
  104. do
  105. echo $name
  106. done
  107. 10. Braces expansion and ranges
  108. touch {file1,file2,file3}.txt
  109. touch file{1..10}.txt
  110. mkdir Directory{1..10}
  111. rm file{1..5}.txt
  112. rmdir Directory{1..5}
  113. for value in {1..5}
  114. do
  115. echo $value
  116. done
  117. 11. Globbing through wild cards
  118. rm file*.txt
  119. rm file{1,3,5}.txt
  120. distrotube did a great video on globbing
  121. 12. regex - regular expressions
  122. ^ - at the beginning of the line
  123. grep "^\- \[ \]*" ~/notes.txt
  124. 13. Functions
  125. function myFunc() {
  126. echo "this is bad practice"
  127. }
  128. myFunc() {
  129. echo "this is good practice"
  130. }
  131. myFunc
  132. 14. shellcheck
  133. Compare strings in Bash:
  134. string1 = string2 # use the = operator with the test [ command
  135. string1 == string2 # use the == operator with the [[ command for pattern matching
  136. string1 != string2 # inequality operator returns true if the operands are not equal
  137. string1 =~ regex # regex operator returns true if the left operand matches the
  138. extended regular expression on the right
  139. string1 > string2 # greater than operator returns true if the left operand is greater
  140. than the right sorted by lexicographical order
  141. string1 < string2 # less than operator returns true if the right operand is greater than
  142. the right sorted by lexicographical order
  143. -z string # true if the string length is zero
  144. -n string # true if the string length is non-zero
  145. echo * (alternative to ls command)
  146. echo *.jpg
  147. # print file size
  148. du -sh $(ls -l | tr -s ' ' | cut -d ' ' -f 9) | less