| | 15 | File Color : Executable scripts appear in a different colour from rest of the files and folders. |
| | 16 | |
| | 17 | Let's start to have some experience on scripting |
| | 18 | |
| | 19 | ===== Simple Script for newbies ===== |
| | 20 | |
| | 21 | First we have to create a text file |
| | 22 | |
| | 23 | `$cat > hello.sh` |
| | 24 | |
| | 25 | And create the content in it |
| | 26 | |
| | 27 | #!/bin/sh |
| | 28 | echo 'Hello World' |
| | 29 | |
| | 30 | `#!/bin/sh` is "how to run" the program. |
| | 31 | `echo 'Hello World'` is "what to do" in the script |
| | 32 | |
| | 33 | Then make the file executable |
| | 34 | |
| | 35 | `$ chmod +x hello.sh` |
| | 36 | |
| | 37 | And run the executable file |
| | 38 | |
| | 39 | `$./hello.sh` |
| | 40 | |
| | 41 | Output: |
| | 42 | |
| | 43 | Hello world |
| | 44 | |
| | 45 | Make yourself familiar with `man man` and `man apropos` on the shell. It will need for your self-tutoring. |
| | 46 | |
| | 47 | ==== Let's make this little advanced ==== |
| | 48 | |
| | 49 | ===== How to define variables ===== |
| | 50 | |
| | 51 | Variables can be defined using the syntax ` variable_name=value`. To get the value of the variable, add `$` before the variable |
| | 52 | |
| | 53 | #!/bin/bash |
| | 54 | # A simple variable example |
| | 55 | greeting=Hello |
| | 56 | name=world |
| | 57 | echo $greeting $name |
| | 58 | |
| | 59 | ===== Arithmetic Expressions ===== |
| | 60 | |
| | 61 | These are the operstors supported by bash for mathematical calculations. |
| | 62 | |
| | 63 | + addition |
| | 64 | - subtraction |
| | 65 | * multiplication |
| | 66 | / division |
| | 67 | ** exponentiation |
| | 68 | % modulus |
| | 69 | |
| | 70 | Numerical expressions can also be calculated and stored in a variable using the syntax below: |
| | 71 | |
| | 72 | `value=$((expression))` |
| | 73 | |
| | 74 | #!/bin/bash |
| | 75 | |
| | 76 | var=$((3+9)) |
| | 77 | echo $var |
| | 78 | |
| | 79 | ===== How to read user input ===== |
| | 80 | |
| | 81 | Sometimes user input are needed to perform relevant operations. |
| | 82 | |
| | 83 | In bash, user inputs are taken using the` read` command. |
| | 84 | |
| | 85 | `read variable_name` |
| | 86 | |
| | 87 | To prompt the user with a custom message, use the `-p` flag. |
| | 88 | |
| | 89 | `read -p "Enter your age" variable_name` |
| | 90 | |
| | 91 | ===== Numeric Comparison logical operators ===== |
| | 92 | |
| | 93 | Comparison is used to check if statements evaluate to true or false. |
| | 94 | |
| | 95 | Equality num1 -eq num2 is num1 equal to num2 |
| | 96 | Greater than equal to num1 -ge num2 is num1 greater than equal to num2 |
| | 97 | Greater than num1 -gt num2 is num1 greater than num2 |
| | 98 | Less than equal to num1 -le num2 is num1 less than equal to num2 |
| | 99 | Less than num1 -lt num2 is num1 less than num2 |
| | 100 | Not Equal to num1 -ne num2 is num1 not equal to num2 |
| | 101 | |
| | 102 | Syntax: |
| | 103 | |
| | 104 | if [ conditions ] |
| | 105 | then |
| | 106 | commands |
| | 107 | fi |
| | 108 | |
| | 109 | ===== Conditional Statements (Decision Making) ===== |
| | 110 | |
| | 111 | Conditions are expressions that evaluate to a boolean expression (true or false). To check conditions, we can use `if`,` if-else`,` if-elif-else` and nested conditionals. |
| | 112 | |
| | 113 | ===== Looping and skipping ===== |
| | 114 | |
| | 115 | For loops allow you to execute statements a specific number of times. |
| | 116 | |
| | 117 | Looping with numbers: |
| | 118 | |
| | 119 | #!/bin/bash |
| | 120 | |
| | 121 | for i in {1..5} |
| | 122 | do |
| | 123 | echo $i |
| | 124 | done |
| | 125 | |
| | 126 | Looping with strings: |
| | 127 | |
| | 128 | #!/bin/bash |
| | 129 | |
| | 130 | for X in cyan magenta yellow |
| | 131 | do |
| | 132 | echo $X |
| | 133 | done |
| | 134 | |
| | 135 | ====== While loop ====== |
| | 136 | |
| | 137 | While loops check for a condition and loop until the condition remains true. We need to provide a counter statement that increments the counter to control loop execution. |
| | 138 | |
| | 139 | #!/bin/bash |
| | 140 | i=1 |
| | 141 | while [[ $i -le 10 ]] ; do |
| | 142 | echo "$i" |
| | 143 | (( i += 1 )) |
| | 144 | done |
| | 145 | |
| | 146 | Reading files: |
| | 147 | |
| | 148 | Suppose we have a file sample_file.txt as shown below: |
| | 149 | |
| | 150 | #!/bin/bash |
| | 151 | |
| | 152 | LINE=1 |
| | 153 | |
| | 154 | while read -r CURRENT_LINE |
| | 155 | do |
| | 156 | echo "$LINE: $CURRENT_LINE" |
| | 157 | ((LINE++)) |
| | 158 | done < "sample_file.txt" |
| | 159 | |
| | 160 | |
| | 161 | |