# Bash Script Beginner Note ## Command Check your terminal have bash or not ```bash bash version ``` Show file content ```bash cat <file> ``` Show file content reversely ```bash tac <file> ``` Show file content from head ```bash head <file> head -n <number_of_lines> <file> head -c <number_of_bytes> <file> ``` Show file content from tail ```bash tail <file> tail -n <number_of_lines> <file> tail -f <file> ``` Search bash command absolute path ```bash which bash ``` Change current directory to specific directory ```bash cd <directory> ``` ```bash ls ls -al ``` Add a new file in current directory ```bash touch <file> ``` Open file with vim editor ```bash vim <file> ``` ```bash! chmod +x <file or directory> chmod 755 <file or directory> ``` Output text ```bash echo "text" echo "text" > <file> echo $SHELL ``` ## Script Thanks to [@linuxhint](https://www.youtube.com/@linuxhint) for youtube bash script tutorial. Here are some basic bash script example to get concept from smooth way. ### 1. Hello Bash Script Output text ```bash #! /bin/bash echo "Hello Bash Script" ``` Store text in file ```bash #! /bin/bash echo "Hello Bash Script" > file.txt ``` ### 2. Redirect to file Replace file context with input text ```bash #! /bin/bash cat > file.txt ``` Append input text to file context ```bash #! /bin/bash cat >> file.txt ``` ### 3. Comment Single line comment ```bash #! /bin/bash # Comment ``` Multiple line comment ```bash #! /bin/bash : ' Comment1 Comment2' ``` ### 4. Conditional Statements | Abbr | Meaning | | -------- | -------- | | -eq | equal | | -ne | not equal | | -gt | greater than | | -ge | greater than or equal to | | -lt | less than | | -le | less than or equal to | | -a | and | | -o | or | Equal case `-eq` ```bash #! /bin/bash count=10 if [ $count -eq 10 ] then echo "Count is 10" else echo "Count is otherwise" fi ``` Compare case `>`, `<` ```bash #! /bin/bash count=10 if (( $count > 10 )) then echo "Count is greater than 10" elif (( $count >= 7 )) then echo "Count is greater than or equal to 7" else echo "Count is otherwise" fi ``` And case `&&`, `-a` ```bash #! /bin/bash count=10 if [ "$count" -gt 10 ] && [ "$count" -lt 40 ] # [[ "$count" -gt 10 && "$count" -lt 40 ]] # [ "$count" -gt 10 -a "$count" -lt 40 ] then echo "Count is correct" else echo "Count is not correct" fi ``` Or case `||`, `-o` ```bash #! /bin/bash count=10 if [ "$count" -gt 10 ] || [ "$count" -lt 40 ] # [[ "$count" -gt 10 || "$count" -lt 40 ]] # [ "$count" -gt 10 -o "$count" -lt 40 ] then echo "Count is correct" else echo "Count is not correct" fi ``` ### 5. Loops While loop ```bash #! /bin/bash count=1 while [ $count -lt 10 ] do echo "$count" count=$(( count+1 )) done ``` Until loop ```bash #! /bin/bash count=1 until [ $count -ge 10 ] do echo "$count" count=$(( count+1 )) done ``` For loop ```bash #! /bin/bash for i in 0 1 2 3 4 5 # for i in {0..5} # for i in {0..5..1} # for (( i=0; i<=5; i++ )) do echo $i done ``` Break ```bash #! /bin/bash for (( i=0; i<=5; i++ )) do if [ $i -eq 3 ] then break fi echo $i done ``` Continue ```bash #! /bin/bash for (( i=0; i<=5; i++ )) do if [ $i -eq 3 ] then continue fi echo $i done ``` ### 6. Script Input `$0` shows the file path related to current folder ```bash #! /bin/bash echo $0 $1 $2 $3 ``` Execute in terminal ```bash helloScript.sh a b c ``` Result ```bash helloScript.sh a b c ``` `args` are dynamic depends on what user inputs ```bash #! /bin/bash args=("$@") # echo ${args[0]} ${args[1]} ${args[2]} echo $@ # args value echo $# # args length ``` Execute in terminal ```bash helloScript.sh a b c ``` Result ```bash a b c 3 ``` ```bash #! /bin/bash while read line do echo "$line" done < "${1:-/dev/stdin}" ``` Execute in terminal ```bash helloScript.sh <file> ``` Result ```bash # Echo file content line by line ``` ### 7. Script Output ### 8. How to Send Output From One Script to Another Scrpt ### 9. String Processing ### 10. Numbers and Arithmetic ### 11. Declare Command ### 12. Array ### 13. Functions ### 14. Files and Directories ### 15. Send Email Via Script ### 16. Curl in Scripts ### 17. Professional Menus ### 18. Wait for filesystem events with inotify ### 19. Introduction to `grep` ### 20. Introduction to `awk` ### 21. Introduction to `sed` ## Reference - [Bash Scripting Full Course 3 Hours](https://www.youtube.com/watch?v=e7BufAVwDiM&ab_channel=linuxhint) - [Linux Chmod 755 命令:它有什么作用?](https://bbs.huaweicloud.com/blogs/300171) ###### tags: `Work` `Bash Script`