├── image └── bashscript.jpg ├── Basic-Script-Bulding ├── part03-Using-Variables.sh ├── part17-Basic-Reading.sh ├── part04-Math-Example.sh ├── part18-Basic-Reading-2.sh ├── part01-Using-Multiple-Commands.sh ├── part10-Reading-values-in-list.sh ├── part02-Display-Messages.sh ├── part11-Reading-values-from-command.sh ├── part07-Using-Double-Brackets.sh ├── part16-Testing-Parameters.sh ├── part06-Double-Parentheses-Command-Symbols.sh ├── part15-The-Break-Command.sh ├── part09-For-Command.sh ├── part14-The-while-Command.sh ├── part12-Reading-directory-using-wildcards.sh ├── part05-if-then-Statement.sh ├── part08-Case-Command.sh ├── part19-Pass-Arguments-To-Scripts.sh └── part13-The-C-Style-for-Command.sh ├── BasicCheatSheet.sh └── README.md /image/bashscript.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devopshobbies/bash-script-templates/HEAD/image/bashscript.jpg -------------------------------------------------------------------------------- /Basic-Script-Bulding/part03-Using-Variables.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # display user information from the system 3 | 4 | echo "User info for userid: $USER" 5 | echo UID: $UID 6 | echo HOME: $HOME 7 | -------------------------------------------------------------------------------- /Basic-Script-Bulding/part17-Basic-Reading.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # testing the read command 4 | 5 | echo -n "Enter your name: " 6 | read name 7 | echo "Hello $name, welcome to my program." 8 | -------------------------------------------------------------------------------- /Basic-Script-Bulding/part04-Math-Example.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # An example of a simple division 3 | 4 | var1=10 5 | var2=20 6 | var3=$(( $var2 / $var1 )) 7 | echo "'$var2' divided by '$var1' results in '$var3'" 8 | -------------------------------------------------------------------------------- /Basic-Script-Bulding/part18-Basic-Reading-2.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # testing the read -p option 4 | 5 | read -p "Please enter your age:" age 6 | days=$[ $age * 365 ] 7 | echo "That makes you over $days days old!" 8 | -------------------------------------------------------------------------------- /Basic-Script-Bulding/part01-Using-Multiple-Commands.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # This script displays the date and who’s logged on 4 | date 5 | cal 6 | who 7 | 8 | #After 9 | #chmod +x filename 10 | #./filename 11 | -------------------------------------------------------------------------------- /Basic-Script-Bulding/part10-Reading-values-in-list.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # basic for command 4 | 5 | for test in Alabama Alaska Arizona Arkansas California Colorado 6 | do 7 | echo The next state is $test 8 | done 9 | -------------------------------------------------------------------------------- /Basic-Script-Bulding/part02-Display-Messages.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # This script displays the date and who’s logged on 4 | echo The time and date are: 5 | date 6 | echo "Let’s see who’s logged into the system:" 7 | who 8 | -------------------------------------------------------------------------------- /Basic-Script-Bulding/part11-Reading-values-from-command.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # reading values from a file 4 | 5 | file="states" 6 | for state in `cat $file` 7 | do 8 | echo "Visit beautiful $state" 9 | Done 10 | -------------------------------------------------------------------------------- /Basic-Script-Bulding/part07-Using-Double-Brackets.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | if [[ $USER == alex ]] 4 | then 5 | echo "Hello $USER" 6 | else 7 | echo "Sorry, I don’t know you" 8 | fi 9 | 10 | # for Answer: 11 | # useradd alex 12 | 13 | -------------------------------------------------------------------------------- /Basic-Script-Bulding/part16-Testing-Parameters.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # testing parameters before use 4 | 5 | if [ -n "$1" ] 6 | then 7 | echo Hello $1, glad to meet you :) 8 | else 9 | echo "Sorry, you didn’t identify yourself" 10 | fi 11 | -------------------------------------------------------------------------------- /Basic-Script-Bulding/part06-Double-Parentheses-Command-Symbols.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # using double parenthesis 4 | 5 | val1=10 6 | 7 | if (( val1 ** 2 > 90 )); then 8 | (( val2 = val1 ** 2 )) 9 | echo "The square of '$val1' is '$val2'" 10 | fi 11 | -------------------------------------------------------------------------------- /Basic-Script-Bulding/part15-The-Break-Command.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # breaking out of a for loop 4 | 5 | for var1 in 1 2 3 4 5 6 7 8 9 10 6 | do 7 | if [ $var1 -eq 5 ] 8 | then 9 | break 10 | fi 11 | echo "Iteration number: $var1" 12 | done 13 | echo "The for loop is completed" 14 | -------------------------------------------------------------------------------- /Basic-Script-Bulding/part09-For-Command.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # For Command Statement : 4 | # for var in list 5 | # do 6 | # commands 7 | # done 8 | 9 | # basic for command 10 | 11 | for test in Alabama Alaska Arizona Arkansas California Colorado 12 | do 13 | echo The next state is $test 14 | done 15 | -------------------------------------------------------------------------------- /Basic-Script-Bulding/part14-The-while-Command.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # The While Command Statement : 3 | 4 | # while test command 5 | # do 6 | # other commands 7 | # done 8 | 9 | # while command test : 10 | 11 | var1=10 12 | while [ $var1 -gt 0 ] 13 | do 14 | echo $var1 15 | var1=$[ $var1 - 1 ] 16 | done 17 | -------------------------------------------------------------------------------- /Basic-Script-Bulding/part12-Reading-directory-using-wildcards.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # iterate through all the files in a directory 4 | 5 | for file in /home/rich/* 6 | do 7 | if [ -d "$file" ] 8 | then 9 | echo "$file is a directory" 10 | elif [ -f "$file" ] 11 | Then 12 | echo "$file is a file" 13 | fi 14 | done 15 | 16 | # For Answer : 17 | # useradd rich 18 | # In /home/rich/ : 19 | # touch test-file 20 | # mkdir test-directory 21 | -------------------------------------------------------------------------------- /Basic-Script-Bulding/part05-if-then-Statement.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # if-then-Statement: 4 | # if command 5 | # then 6 | # commands 7 | # fi 8 | 9 | # testing multiple commands in the then section 10 | 11 | testuser=rich 12 | if grep $testuser /etc/passwd 13 | then 14 | echo The bash files for user $testuser are: 15 | ls -a /home/$testuser 16 | fi 17 | 18 | 19 | 20 | # To Answer : 21 | # useradd rich 22 | # passwd rich 23 | # change to root and : 24 | # cd /home/rich 25 | # touch test.txt 26 | # Run the Bashscript file 27 | 28 | -------------------------------------------------------------------------------- /Basic-Script-Bulding/part08-Case-Command.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Case Command Statement : 4 | # case variable in 5 | # pattern1 | pattern2) commands1;; 6 | # pattern3) commands2;; 7 | # *) default commands;; 8 | # esac 9 | 10 | # using the case command 11 | 12 | case $USER in 13 | root | alex) 14 | echo "Welcome, $USER" 15 | echo "Please enjoy your visit";; 16 | rich) 17 | echo "Special testing account";; 18 | jessica) 19 | echo "Don’t forget to log off when you’re done";; 20 | *) 21 | echo "Sorry, you’re not allowed here";; 22 | esac 23 | -------------------------------------------------------------------------------- /Basic-Script-Bulding/part19-Pass-Arguments-To-Scripts.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Arguments passed to script from the command line, can be accessed using the "$" notation. 3 | 4 | # The arguments "Arg1" and "Arg2" are passed to this script by the following notation: 5 | # sh part19-Pass-Argument-To-Script.sh Arg1 Arg2 6 | # They can be refrenced in the script as $1 and $2. Please note that $0 is reserved for the name of the script. 7 | # This notaion supports up to 9 inputs. If you want to go beyond that limit you have to put {} around the number like ${10}. 8 | 9 | 10 | #======= Examples ========= 11 | 12 | # sh part19-Pass-Argument-To-Script 13 | echo $0 # output : part19-Pass-Argument-To-Script.sh 14 | 15 | 16 | 17 | # sh part19-Pass-Argument-To-Script.sh Arg1 Arg2 18 | echo $1 # output : Arg1 19 | echo $2 # output : Arg2 20 | -------------------------------------------------------------------------------- /Basic-Script-Bulding/part13-The-C-Style-for-Command.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # The C-Style for Command Statement : 4 | # for (( variable assignment ; condition ; iteration process )) 5 | 6 | # testing the C-style for loop 7 | 8 | for (( i=1; i ‹= 10; i++ )) 9 | do 10 | echo "The next number is $i" 11 | done 12 | 13 | # An example of for loops (C-Style) 14 | 15 | let TMP=0 16 | 17 | echo -n "Enter Number between (5 to 9): " 18 | read TMP 19 | 20 | if ! [ $TMP -ge 5 -a $TMP -le 9 ]; then 21 | echo "pelese enter number between 5 and 9, Try Again." 22 | exit 1 23 | fi 24 | 25 | 26 | clear 27 | 28 | for ((i = 1; i <= TMP; i++)); do 29 | 30 | for ((s = TMP; s >= i; s--)); do 31 | echo -n " " 32 | done 33 | 34 | for ((j = 1; j <= i; j++)); do 35 | echo -n " ." 36 | done 37 | 38 | echo "" 39 | 40 | done 41 | 42 | ################### Second stage ###################### 43 | 44 | for ((i = TMP; i >= 1; i--)); do 45 | 46 | for ((s = i; s <= TMP; s++)); do 47 | echo -n " " 48 | done 49 | 50 | for ((j = 1; j <= i; j++)); do 51 | echo -n " ." 52 | done 53 | 54 | echo "" 55 | 56 | done 57 | -------------------------------------------------------------------------------- /BasicCheatSheet.sh: -------------------------------------------------------------------------------- 1 | ### Function ### 2 | fun_name ( ) { 3 | cmd1 4 | cmd2 5 | ... 6 | } 7 | 8 | # and the single line version to create method 9 | fun_name () { commands; } 10 | 11 | ## Second format to create method ## 12 | function fun_name { 13 | cmd1 14 | cmd2 15 | ... 16 | } 17 | 18 | # and the single line 19 | function fun_name { commands; } 20 | 21 | 22 | ### Using brackets ### 23 | sum=$[ $var1 + $var2 ] 24 | 25 | 26 | ### Using bc ### 27 | variable=`bc << EOF 28 | options 29 | statements 30 | expressions 31 | EOF 32 | ` 33 | 34 | 35 | ### if-then Statement ### 36 | if command 37 | then 38 | commands 39 | fi 40 | 41 | 42 | ### if-then-else Statement ### 43 | if command 44 | then 45 | commands 46 | else 47 | commands 48 | fi 49 | 50 | 51 | ### Nesting ifs ### 52 | if command1 53 | then 54 | command set 1 55 | elif command2 56 | then 57 | command set 2 58 | elif command3 59 | then 60 | command set 3 61 | elif command4 62 | then 63 | command set 4 64 | fi 65 | 66 | 67 | ### The case Command ### 68 | case variable in 69 | pattern1 | pattern2) commands1;; 70 | pattern3) commands2;; 71 | *) default commands;; 72 | esac 73 | 74 | 75 | ### The for Command ### 76 | for var in list 77 | do 78 | commands 79 | done 80 | 81 | 82 | ### The C-Style for Command ### 83 | for (( variable assignment ; condition ; iteration process )) 84 | do 85 | commands 86 | done 87 | 88 | 89 | ### The while Command ### 90 | while test command 91 | do 92 | other commands 93 | done 94 | 95 | 96 | ### The until Command ### 97 | until test commands 98 | do 99 | other commands 100 | done 101 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # bash-script-tutorial 2 | An Overview of Shells\ 3 | A shell is a fundamental and important part of your Linux computing environment. Shells are user programs not unlike other text-based programs and utilities. They offer a rich customizable interface to your system. Some of the main items provided by your shell are:\ 4 | • An interactive textual user interface to the operating system.\ 5 | • An operating environment.\ 6 | • A facility for launching and managing commands and programs.\ 7 | • A programming language. 8 | 9 |
10 |
11 |