|
156 | 156 | | > | Output redirect | |
157 | 157 | | / | Pathname directory separator | |
158 | 158 | | ? | Single character wildcard | |
| 159 | + |
| 160 | + |
| 161 | +## Understanding Command Line Parsing |
| 162 | + |
| 163 | + + When a command is interpreted by the shell, the shell interprets all special characters |
| 164 | + + This process is known as ***command line parsing*** |
| 165 | + + Commands themselves may interpret parts of the command line as well |
| 166 | + + To ensure that a special character is interpreted by the command and not by the shell, use quoting |
| 167 | + |
| 168 | +## Quoting |
| 169 | + |
| 170 | + + Quoting is used to treat special characters literally |
| 171 | + + If a string of characters is surrounded with single quotation marks, all characters are stripped of the special meaning they may have |
| 172 | + + Imagine **echo 2*3 > 5**, whic would be interpreted |
| 173 | + + Or imagine **find .-name '*.doc'** which ensures that **find** interprets *.doc and not the shell |
| 174 | + + Double quotes are weak quotes and treat only some special characters as special |
| 175 | + + A backslash can be used to escape the one following character |
| 176 | + |
| 177 | +## Using Double Quotes |
| 178 | + |
| 179 | + + Double quotes ignore pipe characters, aliases, tilde substitution, wildcard expression, and splitting into words using delimiters |
| 180 | + + Double quotes do allow parameter substitution, command substitution, and arithmetic expression evaluation |
| 181 | + + best practice: use single quotes, unless you specifically need parameter, command, or arithmetic substitution |
| 182 | + |
| 183 | +## Handling Script Arguments |
| 184 | + |
| 185 | + + Any argument that was used when starting a script, can be dealt with from within the script |
| 186 | + + Use $1, $2 and so on to refer to the first, the second, etc. argument |
| 187 | + + **$0** refers to the name of the script itself |
| 188 | + + Use **${nn}** or **shift** to refer to arguments beyond 9 |
| 189 | + + Arguments that are stored in $1 etc. are read only and cannot be changed from within the script |
| 190 | + |
| 191 | +> Try : [simplearg](https://github.com/OddExtension5/Linux-Guide/blob/master/bash%20shell/code/simplearg.sh) |
| 192 | +
|
| 193 | +Run Commands : |
| 194 | + |
| 195 | + + chmod + simplearg.sh |
| 196 | + + ./simplearg sushil sneha |
| 197 | + + ./simplearg sushil sneha debagni |
| 198 | + + ./simplearg sushil sneha debagni aakansha |
| 199 | + |
| 200 | +## Handling Argumets the Smart Way |
| 201 | + |
| 202 | + + The previous example works only if the amount of argumets is known on berforehand |
| 203 | + + If this is not the case, use **for** to evaluate all possible arguments |
| 204 | + + Use **$@** to refer to all arguments provided, where all arguments can be treated one by one |
| 205 | + + Use **$#** to count the amount of argumets provided |
| 206 | + + Use **$*** if you need a single string that contains all arguments (use in specific cases only) |
| 207 | + |
| 208 | +> Try : [nicearg](https://github.com/OddExtension5/Linux-Guide/blob/master/bash%20shell/code/nicearg.sh) |
| 209 | +
|
| 210 | +Run commands: |
| 211 | + |
| 212 | + + chmod +x nicearg |
| 213 | + + ./nicearg a b c |
| 214 | + |
| 215 | +Output: |
| 216 | + |
| 217 | + + $* gives a b c |
| 218 | + + $# gives 3 |
| 219 | + + $@ gives a b c |
| 220 | + + $0 is path/nicearg |
| 221 | + + a b c |
| 222 | + + a |
| 223 | + + b |
| 224 | + + c |
| 225 | + |
| 226 | +Testing script: **bash -x filename** |
| 227 | + |
| 228 | +## Using Shift |
| 229 | + |
| 230 | + + Shift removes the first argument from a list so that the second argument gets stored in $1 |
| 231 | + + Shift is useful in older shell versions that do not understand ${10} etc. |
| 232 | + |
| 233 | +> Try: [shift_arg.sh](https://github.com/OddExtension5/Linux-Guide/blob/master/bash%20shell/code/shift_arg.sh) <br> |
| 234 | +> Try: [shift_arg2.sh](https://github.com/OddExtension5/Linux-Guide/blob/master/bash%20shell/code/shift_arg2.sh) |
| 235 | +
|
| 236 | +## Understanding Command Substitution |
| 237 | + |
| 238 | + + Command susbtitution allows using the result of a command in a script |
| 239 | + + Useful to provide ultimate flexibility |
| 240 | + + Two allowd syntaxes: |
| 241 | + + **`command`** (deprecated) |
| 242 | + + **$(command)** (preferred) |
| 243 | + + **ls -l $(which passwd)** |
| 244 | + |
| 245 | +## Using String Verification |
| 246 | + |
| 247 | + + When working with arguments and input, it is useful to be able to verify availability and correct use of a string |
| 248 | + + Use **test -z** to check if a string is empty |
| 249 | + + **test -z $1 && exit 1** |
| 250 | + + Use **[[...]]** to check for specific patterns |
| 251 | + + **[[ $1=='[a-z]*' ]] || echo $1 does not start with a letter** |
| 252 | + |
| 253 | + |
| 254 | +# Using Here Documents |
| 255 | + |
| 256 | + + In a here document, I/O redirection is used to feed a command list into an interactive program or command, such as for instance **ftp** or **cat** |
| 257 | + + Use it in scripts to replace echo for long texts that need to be displayed |
| 258 | + + Use it if in a script a command is called that opens its own prompt, such as an FTP client interface |
| 259 | + |
| 260 | +> Try: [heredoc1.sh](https://github.com/OddExtension5/Linux-Guide/blob/master/bash%20shell/code/heredoc1.sh) |
0 commit comments