├── Hackerrank ├── 011-cut-#1.sh ├── 012-cut-#2.sh ├── 013-cut-#3.sh ├── 014-cut-#4.sh ├── 016-cut-#6.sh ├── 027-tr-command-#3.sh ├── 039-paste-#1.sh ├── 040-paste-#2.sh ├── 041-paste-#3.sh ├── 042-paste-#4.sh ├── 017-cut-#7.sh ├── 018-cut-#8.sh ├── 031-sort-command-#4.sh ├── 001-lets-echo.sh ├── 015-cut-#5.sh ├── 019-cut-#9.sh ├── 026-tr-command-#2.sh ├── 028-sort-command-#1.sh ├── 030-sort-command-#3.sh ├── 029-sort-command-#2.sh ├── 023-tail-of-a-text-file-#1.sh ├── 033-sort-command-#6.sh ├── 020-head-of-a-text-file-#1.sh ├── 021-head-of-a-text-file-#2.sh ├── 024-tail-of-a-text-file-#2.sh ├── 034-sort-command-#7.sh ├── 035-uniq-command-#1.sh ├── 032-sort-command-#5.sh ├── 038-uniq-command-#4.sh ├── 037-uniq-command-#3.sh ├── 036-uniq-command-#2.sh ├── 003-a-personalized-echo.sh ├── 022-the-middle-of-a-text-file.sh ├── 044-slice-an-array.sh ├── let's-echo.md ├── 043-read-in-an-array.sh ├── 004-looping-with-numbers.sh ├── 002-looping-and-skipping.sh ├── 009-arithmetic-operations.sh ├── 025-tr-command-#1.sh ├── a-personalized-echo.md ├── 046-filter-an-array-with-patterns.sh ├── 048-dispaly-an-element-of-an-array.sh ├── looping-with-number.md ├── looping-and-skipping.md ├── 047-count-the-number-of-elements-in-an-array.sh ├── 045-concatenate-an-array.sh ├── 005-the-world-of-numbers.sh ├── the-world-of-numbers.md ├── 010-compute-the-average.sh ├── 006-comparing-numbers.sh ├── compute-the-average.md ├── getting-started-with-conditionals.md ├── 007-getting-started-with-conditionals.sh ├── arithmetic-operation.md ├── comparing-numbers.md ├── 008-more-on-conditionals.sh └── more-on-conditionals.md └── README.md /Hackerrank/011-cut-#1.sh: -------------------------------------------------------------------------------- 1 | # Cut 1 : https://www.hackerrank.com/challenges/text-processing-cut-1 2 | 3 | cut -c 3 4 | -------------------------------------------------------------------------------- /Hackerrank/012-cut-#2.sh: -------------------------------------------------------------------------------- 1 | # Cut 2: https://www.hackerrank.com/challenges/text-processing-cut-2 2 | 3 | cut -c 2,7 4 | -------------------------------------------------------------------------------- /Hackerrank/013-cut-#3.sh: -------------------------------------------------------------------------------- 1 | # Cut 3: https://www.hackerrank.com/challenges/text-processing-cut-3 2 | 3 | cut -c 2-7 4 | -------------------------------------------------------------------------------- /Hackerrank/014-cut-#4.sh: -------------------------------------------------------------------------------- 1 | # Cut 4 : https://www.hackerrank.com/challenges/text-processing-cut-4 2 | 3 | cut -c 1-4 4 | -------------------------------------------------------------------------------- /Hackerrank/016-cut-#6.sh: -------------------------------------------------------------------------------- 1 | # Cut 6: https://www.hackerrank.com/challenges/text-processing-cut-6 2 | 3 | cut -c 13- 4 | -------------------------------------------------------------------------------- /Hackerrank/027-tr-command-#3.sh: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/text-processing-tr-3/ 2 | 3 | tr -s " " 4 | -------------------------------------------------------------------------------- /Hackerrank/039-paste-#1.sh: -------------------------------------------------------------------------------- 1 | # Paste 1 : https://www.hackerrank.com/challenges/paste-1/ 2 | 3 | paste -s -d ';' 4 | -------------------------------------------------------------------------------- /Hackerrank/040-paste-#2.sh: -------------------------------------------------------------------------------- 1 | # Paste 2: https://www.hackerrank.com/challenges/paste-2/ 2 | 3 | paste - - - -d ';' 4 | -------------------------------------------------------------------------------- /Hackerrank/041-paste-#3.sh: -------------------------------------------------------------------------------- 1 | # Paste 3: https://www.hackerrank.com/challenges/paste-3/ 2 | 3 | paste -s -d $'\t' 4 | -------------------------------------------------------------------------------- /Hackerrank/042-paste-#4.sh: -------------------------------------------------------------------------------- 1 | # Paste 4: https://www.hackerrank.com/challenges/paste-4/ 2 | 3 | paste - - - -d $'\t' 4 | -------------------------------------------------------------------------------- /Hackerrank/017-cut-#7.sh: -------------------------------------------------------------------------------- 1 | # Cut 7: https://www.hackerrank.com/challenges/text-processing-cut-7 2 | 3 | cut -d ' ' -f 4 4 | -------------------------------------------------------------------------------- /Hackerrank/018-cut-#8.sh: -------------------------------------------------------------------------------- 1 | # Cut 8: https://www.hackerrank.com/challenges/text-processing-cut-8 2 | 3 | cut -d ' ' -f -3 4 | -------------------------------------------------------------------------------- /Hackerrank/031-sort-command-#4.sh: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/text-processing-sort-4/ 2 | 3 | sort -n -r 4 | -------------------------------------------------------------------------------- /Hackerrank/001-lets-echo.sh: -------------------------------------------------------------------------------- 1 | # Link: https://www.hackerrank.com/challenges/bash-tutorials-lets-echo 2 | 3 | echo "HELLO"; 4 | -------------------------------------------------------------------------------- /Hackerrank/015-cut-#5.sh: -------------------------------------------------------------------------------- 1 | # Cut 5: https://www.hackerrank.com/challenges/text-processing-cut-5 2 | 3 | cut -d $'\t' -f 1-3 4 | -------------------------------------------------------------------------------- /Hackerrank/019-cut-#9.sh: -------------------------------------------------------------------------------- 1 | # Cut 9: https://www.hackerrank.com/challenges/text-processing-cut-9 2 | 3 | cut -d $'\t' -f 2- 4 | -------------------------------------------------------------------------------- /Hackerrank/026-tr-command-#2.sh: -------------------------------------------------------------------------------- 1 | # Tr command 2: https://www.hackerrank.com/challenges/text-processing-tr-2/ 2 | 3 | tr -d a-z 4 | -------------------------------------------------------------------------------- /Hackerrank/028-sort-command-#1.sh: -------------------------------------------------------------------------------- 1 | # Sort Command 1: https://www.hackerrank.com/challenges/text-processing-sort-1/ 2 | 3 | sort 4 | -------------------------------------------------------------------------------- /Hackerrank/030-sort-command-#3.sh: -------------------------------------------------------------------------------- 1 | # Sort Command 3: https://www.hackerrank.com/challenges/text-processing-sort-3 2 | 3 | sort -n 4 | -------------------------------------------------------------------------------- /Hackerrank/029-sort-command-#2.sh: -------------------------------------------------------------------------------- 1 | # Sort Command 2: https://www.hackerrank.com/challenges/text-processing-sort-2/ 2 | 3 | sort -r 4 | -------------------------------------------------------------------------------- /Hackerrank/023-tail-of-a-text-file-#1.sh: -------------------------------------------------------------------------------- 1 | # Tail of a text file: https://www.hackerrank.com/challenges/text-processing-tail-1 2 | 3 | tail -n 20 4 | -------------------------------------------------------------------------------- /Hackerrank/033-sort-command-#6.sh: -------------------------------------------------------------------------------- 1 | # Sort Command 6: https://www.hackerrank.com/challenges/text-processing-sort-6 2 | 3 | sort -t $'\t' -n -k 2 4 | -------------------------------------------------------------------------------- /Hackerrank/020-head-of-a-text-file-#1.sh: -------------------------------------------------------------------------------- 1 | # Head of a text file #1: https://www.hackerrank.com/challenges/text-processing-head-1/ 2 | 3 | head -n 20 4 | -------------------------------------------------------------------------------- /Hackerrank/021-head-of-a-text-file-#2.sh: -------------------------------------------------------------------------------- 1 | # Head of a text file 2 : https://www.hackerrank.com/challenges/text-processing-head-2 2 | 3 | head -c 20 4 | -------------------------------------------------------------------------------- /Hackerrank/024-tail-of-a-text-file-#2.sh: -------------------------------------------------------------------------------- 1 | # Tail of a text file #2: https://www.hackerrank.com/challenges/text-processing-tail-2/ 2 | 3 | tail -c 20 4 | -------------------------------------------------------------------------------- /Hackerrank/034-sort-command-#7.sh: -------------------------------------------------------------------------------- 1 | # Sort Command 7: https://www.hackerrank.com/challenges/text-processing-sort-7/ 2 | 3 | sort -t '|' -k 2 -r -n 4 | -------------------------------------------------------------------------------- /Hackerrank/035-uniq-command-#1.sh: -------------------------------------------------------------------------------- 1 | # Uniq Command 1: https://www.hackerrank.com/challenges/text-processing-in-linux-the-uniq-command-1/ 2 | 3 | uniq 4 | -------------------------------------------------------------------------------- /Hackerrank/032-sort-command-#5.sh: -------------------------------------------------------------------------------- 1 | # Sort Command 5 : https://www.hackerrank.com/challenges/text-processing-sort-5/ 2 | 3 | sort -t $'\t' -k 2,2 -r -n 4 | -------------------------------------------------------------------------------- /Hackerrank/038-uniq-command-#4.sh: -------------------------------------------------------------------------------- 1 | # Uniq Command 4: https://www.hackerrank.com/challenges/text-processing-in-linux-the-uniq-command-4/ 2 | 3 | uniq -u 4 | -------------------------------------------------------------------------------- /Hackerrank/037-uniq-command-#3.sh: -------------------------------------------------------------------------------- 1 | # Uniq command 3: https://www.hackerrank.com/challenges/text-processing-in-linux-the-uniq-command-3/ 2 | 3 | uniq -i -c | tr -s ' ' | cut -c 2- 4 | -------------------------------------------------------------------------------- /Hackerrank/036-uniq-command-#2.sh: -------------------------------------------------------------------------------- 1 | # Uniq Command 2: https://www.hackerrank.com/challenges/text-processing-in-linux-the-uniq-command-2/ 2 | 3 | uniq -c | tr -s ' ' | cut -d ' ' -f 2- 4 | -------------------------------------------------------------------------------- /Hackerrank/003-a-personalized-echo.sh: -------------------------------------------------------------------------------- 1 | # A personalized echo : https://www.hackerrank.com/challenges/bash-tutorials---a-personalized-echo 2 | 3 | read -s name; 4 | echo "Welcome $name"; 5 | -------------------------------------------------------------------------------- /Hackerrank/022-the-middle-of-a-text-file.sh: -------------------------------------------------------------------------------- 1 | # The Middle of a text file: https://www.hackerrank.com/challenges/text-processing-in-linux---the-middle-of-a-text-file/ 2 | 3 | sed -n '12,22p' 4 | -------------------------------------------------------------------------------- /Hackerrank/044-slice-an-array.sh: -------------------------------------------------------------------------------- 1 | # Slice an array: https://www.hackerrank.com/challenges/bash-tutorials-slice-an-array/problem?isFullScreen=true 2 | 3 | readarray -t arr; 4 | echo ${arr[@]:3:5} 5 | -------------------------------------------------------------------------------- /Hackerrank/let's-echo.md: -------------------------------------------------------------------------------- 1 | Question: [Let's echo](https://www.hackerrank.com/challenges/bash-tutorials-lets-echo/problem?isFullScreen=true) 2 | 3 | ``` 4 | #!/bin/bash 5 | echo "HELLO" 6 | ``` 7 | -------------------------------------------------------------------------------- /Hackerrank/043-read-in-an-array.sh: -------------------------------------------------------------------------------- 1 | # Read in an array: https://www.hackerrank.com/challenges/bash-tutorials-read-in-an-array/problem?isFullScreen=true 2 | 3 | readarray -t arr; 4 | echo ${arr[@]}; 5 | -------------------------------------------------------------------------------- /Hackerrank/004-looping-with-numbers.sh: -------------------------------------------------------------------------------- 1 | # Looping with numbers: https://www.hackerrank.com/challenges/bash-tutorials---looping-with-numbers 2 | 3 | for ((i=1; i<=50; i++)); do 4 | echo $i; 5 | done 6 | -------------------------------------------------------------------------------- /Hackerrank/002-looping-and-skipping.sh: -------------------------------------------------------------------------------- 1 | # Looping and Skipping : https://www.hackerrank.com/challenges/bash-tutorials---looping-and-skipping 2 | 3 | for ((i = 1 ; i < 100 ; i+=2)); do 4 | echo $i 5 | done 6 | -------------------------------------------------------------------------------- /Hackerrank/009-arithmetic-operations.sh: -------------------------------------------------------------------------------- 1 | # Arithmetic Operations: https://www.hackerrank.com/challenges/bash-tutorials---arithmetic-operations 2 | 3 | read -s var; 4 | 5 | printf %.3f $(echo $var | bc -l) 6 | -------------------------------------------------------------------------------- /Hackerrank/025-tr-command-#1.sh: -------------------------------------------------------------------------------- 1 | # Tr Command 1 : https://www.hackerrank.com/challenges/text-processing-tr-1/ 2 | 3 | # My Solution: 4 | # tr "(" "[" | tr ")" "]" 5 | 6 | # Better One: 7 | tr "()" "[]" 8 | 9 | -------------------------------------------------------------------------------- /Hackerrank/a-personalized-echo.md: -------------------------------------------------------------------------------- 1 | [A Personalized Echo](https://www.hackerrank.com/challenges/bash-tutorials---a-personalized-echo/problem?isFullScreen=true) 2 | 3 | ``` 4 | read name 5 | echo "Welcome $name" 6 | ``` 7 | -------------------------------------------------------------------------------- /Hackerrank/046-filter-an-array-with-patterns.sh: -------------------------------------------------------------------------------- 1 | # Filter an array with patterns: https://www.hackerrank.com/challenges/bash-tutorials-filter-an-array-with-patterns/problem?isFullScreen=true 2 | 3 | readarray -t arr; 4 | echo ${arr[@]/*a*/} 5 | -------------------------------------------------------------------------------- /Hackerrank/048-dispaly-an-element-of-an-array.sh: -------------------------------------------------------------------------------- 1 | # Display an element of an array: https://www.hackerrank.com/challenges/bash-tutorials-display-the-third-element-of-an-array/problem?isFullScreen=true 2 | 3 | readarray -t arr; 4 | echo ${arr[3]}; 5 | -------------------------------------------------------------------------------- /Hackerrank/looping-with-number.md: -------------------------------------------------------------------------------- 1 | [Looping with Numbers](https://www.hackerrank.com/challenges/bash-tutorials---looping-with-numbers/problem?isFullScreen=true) 2 | 3 | ``` 4 | for ((i = 1; i < 51; i++)); do 5 | echo $i 6 | done 7 | 8 | ``` 9 | -------------------------------------------------------------------------------- /Hackerrank/looping-and-skipping.md: -------------------------------------------------------------------------------- 1 | Question: [Looping and skipping](https://www.hackerrank.com/challenges/bash-tutorials---looping-and-skipping/problem?isFullScreen=true) 2 | 3 | ``` 4 | for i in {1..99..2} 5 | do 6 | echo "$i" 7 | done 8 | 9 | ``` 10 | -------------------------------------------------------------------------------- /Hackerrank/047-count-the-number-of-elements-in-an-array.sh: -------------------------------------------------------------------------------- 1 | # Count the number of elements in an array: https://www.hackerrank.com/challenges/bash-tutorials-count-the-number-of-elements-in-an-array/problem?isFullScreen=true 2 | 3 | readarray -t arr; 4 | echo ${#arr[@]} 5 | -------------------------------------------------------------------------------- /Hackerrank/045-concatenate-an-array.sh: -------------------------------------------------------------------------------- 1 | # Concatenate an array: https://www.hackerrank.com/challenges/bash-tutorials-concatenate-an-array-with-itself/problem?isFullScreen=true 2 | 3 | readarray -t arr; 4 | newarr=("${arr[@]}" "${arr[@]}" "${arr[@]}") 5 | echo ${newarr[@]} 6 | -------------------------------------------------------------------------------- /Hackerrank/005-the-world-of-numbers.sh: -------------------------------------------------------------------------------- 1 | # The World of Numbers: https://www.hackerrank.com/challenges/bash-tutorials---the-world-of-numbers 2 | 3 | read -s X; 4 | read -s Y; 5 | 6 | echo $(($X + $Y)); 7 | echo $(($X - $Y)); 8 | echo $(($X * $Y)); 9 | echo $(($X / $Y)); 10 | -------------------------------------------------------------------------------- /Hackerrank/the-world-of-numbers.md: -------------------------------------------------------------------------------- 1 | [The world of numbers](https://www.hackerrank.com/challenges/bash-tutorials---the-world-of-numbers/problem?isFullScreen=true) 2 | 3 | ``` 4 | read x 5 | read y 6 | echo "$[x+y]" 7 | echo "$[x-y]" 8 | echo "$[x*y]" 9 | echo "$[x/y]" 10 | 11 | ``` 12 | -------------------------------------------------------------------------------- /Hackerrank/010-compute-the-average.sh: -------------------------------------------------------------------------------- 1 | # Compute the average: https://www.hackerrank.com/challenges/bash-tutorials---compute-the-average 2 | 3 | read -s N; 4 | 5 | sum=0; 6 | for ((i=0; i $Y)); then 10 | echo "X is greater than Y" 11 | elif (($X < $Y)); then 12 | echo "X is less than Y" 13 | else 14 | echo "X is equal to Y" 15 | fi 16 | 17 | ``` 18 | -------------------------------------------------------------------------------- /Hackerrank/008-more-on-conditionals.sh: -------------------------------------------------------------------------------- 1 | # More on conditionals: https://www.hackerrank.com/challenges/bash-tutorials---more-on-conditionals 2 | 3 | read -s X; 4 | read -s Y; 5 | read -s Z; 6 | 7 | if [ $X -eq $Y ] && [ $X -eq $Z ]; then 8 | echo "EQUILATERAL"; 9 | elif [ $X -eq $Y ] || [ $Y -eq $Z ] || [ $Y -eq $Z ]; then 10 | echo "ISOSCELES"; 11 | else 12 | echo "SCALENE"; 13 | fi 14 | -------------------------------------------------------------------------------- /Hackerrank/more-on-conditionals.md: -------------------------------------------------------------------------------- 1 | [More on conditionals](https://www.hackerrank.com/challenges/bash-tutorials---more-on-conditionals/problem?isFullScreen=true) 2 | 3 | ``` 4 | #!/bin/bash 5 | 6 | read X 7 | read Y 8 | read Z 9 | 10 | if [[ ($X == $Y) && ($X == $Z)]]; then 11 | echo "EQUILATERAL" 12 | elif [[ ($X == $Y) || ($Y == $Z) || ($X == $Z)]]; then 13 | echo "ISOSCELES" 14 | else 15 | echo "SCALENE" 16 | fi 17 | 18 | ``` 19 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | `Note: These notes are for my personal reference!` 2 | 3 | # 𝐁𝐚𝐬𝐡 𝐒𝐜𝐫𝐢𝐩𝐭𝐢𝐧𝐠 for Hackers & Bug Bounty Hunters 4 | 5 | ## 𝐁𝐚𝐬𝐢𝐜𝐬 6 | 7 | - Telling interpreter that the file is bash file 8 | 9 | ``` 10 | !#/bin/bash 11 | ``` 12 | 13 | - Make the file executable 14 | ``` 15 | $ chmod +x script.sh 16 | ``` 17 | 18 | ## 𝐂𝐨𝐦𝐦𝐞𝐧𝐭𝐬 19 | 20 | ``` 21 | # this is a single line comment 22 | ``` 23 | ``` 24 | # Multi line comment 25 | 26 | # - Method I 27 | < 174 | elif [condition]; then 175 | 176 | else 177 | 178 | fi 179 | 180 | ``` 181 | 182 | 183 | - `||` : OR 184 | - `&&` : AND 185 | 186 | ``` 187 | if [] || []; then 188 | 189 | elif [] && []; then 190 | 191 | else 192 | 193 | fi 194 | ``` 195 | 196 | Operators | Description 197 | --- | --- 198 | `! EXPRESSION` | To check if `EXPRESSION` is false. 199 | `-n STRING` | To check if the length of `STRING` is greater than zero. 200 | `-z STRING` | To check if the length of `STRING` is zero (i.e., it is empty) 201 | `STRING1 == STRING2` | To check if `STRING1` is equal to `STRING2`. 202 | `STRING1 != STRING2` | To check if `STRING1` is not equal to `STRING2`. 203 | `INTEGER1 -eq INTEGER2` | To check if `INTEGER1` is numerically equal to `INTEGER2`. 204 | `INTEGER1 -gt INTEGER2` | To check if `INTEGER1` is numerically greater than `INTEGER2`. 205 | `INTEGER1 -lt INTEGER2` | To check if `INTEGER1` is numerically less than `INTEGER2`. 206 | `-d FILE` | To check if `FILE` exists and it is a directory. 207 | `-e FILE` | To check if `FILE` exists. 208 | `-r FILE` | To check if `FILE` exists and the read permission is granted. 209 | `-s FILE` | To check if `FILE` exists and its size is greater than zero (which means that it is not empty). 210 | `-w FILE` | To check if `FILE` exists and the write permission is granted. 211 | `x FILE` | To check if `FILE` exists and the execute permission is granted. 212 | 213 | --- 214 | 215 | ## 𝐁𝐚𝐬𝐡 𝐂𝐚𝐬𝐞𝐬 216 | 217 | - Syntax: 218 | ``` 219 | #!/bin/bash 220 | 221 | echo "Which Operating System are you using?" 222 | echo "Windows, Android, Chrome, Linux, Others?" 223 | read -p "Type your OS Name:" OS 224 | 225 | case $OS in 226 | Windows|windows) 227 | echo "That's common. You should try something new." 228 | echo 229 | ;; 230 | Android|android) 231 | echo "This is my favorite. It has lots of applications." 232 | echo 233 | ;; 234 | Chrome|chrome) 235 | echo "Cool!!! It's for pro users. Amazing Choice." 236 | echo 237 | ;; 238 | Linux|linux) 239 | echo "You might be serious about security!!" 240 | echo 241 | ;; 242 | *) 243 | echo "Sounds interesting. I will try that." 244 | echo 245 | ;; 246 | esac 247 | ``` 248 | 249 | --- 250 | 251 | ## 𝐁𝐚𝐬𝐡 𝐟𝐨𝐫 𝐋𝐨𝐨𝐩 252 | 253 | - C++ like for loop 254 | 255 | ``` 256 | for ((i = 0 ; i < 100 ; i++)); do 257 | echo $i 258 | done 259 | ``` 260 | 261 | - To read a range 262 | ``` 263 | for num in {1..10} 264 | do 265 | echo $num 266 | done 267 | ``` 268 | 269 | - a range with increment 270 | 271 | ``` 272 | for num in {1..10..1} 273 | do 274 | echo $num 275 | done 276 | ``` 277 | 278 | - a range with decrement 279 | 280 | ``` 281 | for num in {10..0..1} 282 | do 283 | echo $num 284 | done 285 | ``` 286 | 287 | - Array variables 288 | 289 | ``` 290 | array=( "element1" "element 2" . . "elementN" ) 291 | 292 | for i in "${arr[@]}" 293 | do 294 | echo $i 295 | done 296 | ``` 297 | 298 | - white spaces in String as word separators 299 | 300 | ``` 301 | #!/bin/bash 302 | 303 | for word in $str; 304 | do 305 | 306 | done 307 | ``` 308 | 309 | - Each line in string as a word 310 | 311 | ``` 312 | #!/bin/bash 313 | 314 | for word in "$str"; 315 | do 316 | 317 | done 318 | ``` 319 | 320 | - Infinite loop 321 | 322 | ``` 323 | i=1; 324 | for (( ; ; )) 325 | do 326 | sleep 1s 327 | echo "Current Number: $((i++))" 328 | done 329 | ``` 330 | --- 331 | 332 | ## 𝐁𝐚𝐬𝐡 𝐰𝐡𝐢𝐥𝐞 𝐥𝐨𝐨𝐩 333 | 334 | - C++ Style while loop 335 | ``` 336 | i=1 337 | while((i <= 10)) 338 | do 339 | echo $i 340 | let i++ 341 | done 342 | ``` 343 | 344 | 345 | 346 | ## 𝐂𝐮𝐭 𝐂𝐡𝐞𝐚𝐭𝐬𝐡𝐞𝐞𝐭 347 | 348 | Command | Explanation 349 | :-:|--- 350 | `cut -c 3` | display `3rd character` from each line of text 351 | `cut -c 2,7` | display the `2nd and 7th character` from each line of text 352 | `cut -c 2-7` | display a range of characters starting at the `2nd position` of a string and ending at the `7th position`(both positions included) 353 | `cut -c -4` | display the `first four` characters from each line of text 354 | `cut -d $'\t' -f -3` | display `first three` fields of a `tab delimited` file 355 | `cut -c 13-` | display the characters from `13th` position to the `end` 356 | `cut -d ' ' -f 4` | display `4th word` with space `' '` as a delimiter 357 | `cut -d ' ' -f -3` | display `first three words` with space `' '` used as a delimiter 358 | `cut -d $'\t' -f 2-` | given a `tab` delimited file, display the fields from `second fields to last field` 359 | 360 | 361 | ## 𝐎𝐭𝐡𝐞𝐫 𝐍𝐨𝐭𝐞𝐬 362 | 363 | - Chop off the arithmetic operations to decimal points: `bc <<< "scale=3; $expression"` 364 | - Round of the arithmetic operation result: `printf %.3f $(echo $expression | bc -l)` 365 | 366 | - Performing Arithmatic Operations (add, subtract, multiply divide) on two variables: `$((EXPR))` 367 | ``` 368 | read -s X; 369 | read -s Y; 370 | 371 | echo $(($X + $Y)); 372 | echo $(($X - $Y)); 373 | echo $(($X * $Y)); 374 | echo $(($X / $Y)); 375 | ``` 376 | 377 | - `head` : output the first part of files 378 | 379 | ``` 380 | head -n # display first n lines from a text file 381 | head -c # display first n characters from a text file 382 | ``` 383 | 384 | - `tail` : output the last part of files 385 | ``` 386 | tail -n # display last n lines from a text file 387 | tail -c # display last n characters from a text file 388 | ``` 389 | 390 | - Reading a file line by line 391 | 392 | ``` 393 | while read -r line; do "$line" done < filename 394 | ``` 395 | 396 | ## Multi-threading or Parallel Execution in bash 397 | 398 | - We make use of `parallel` command to execute multiple threads in parallel 399 | - Installing parallel 400 | ``` 401 | sudo apt install parallel 402 | ``` 403 | - Reading Man Page 404 | ``` 405 | man parallel 406 | ``` 407 | 408 | - Usage 409 | ``` 410 | parallel [options] [command [arguments]] < list_of_arguments 411 | 412 | parallel [options] [command [arguments]] ( ::: arguments | :::+ arguments | :::: argfile(s) | ::::+ argfile(s) ) ... 413 | 414 | parallel --semaphore [options] command 415 | 416 | #!/usr/bin/parallel --shebang [options] [command [arguments]] 417 | 418 | #!/usr/bin/parallel --shebang-wrap [options] [command [arguments]] 419 | ``` 420 | 421 | --- 422 | > Done 423 | --------------------------------------------------------------------------------