Advertisement
efxtv

FOR AND WHILE LOOP IN BASH WITH EXAMPLE

Sep 24th, 2023 (edited)
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.27 KB | Cybersecurity | 0 0
  1. OPIC: For and while LOOP in BASH
  2. FOR LOOP: In computer science, a for-loop or for loop is a control flow statement for specifying iteration. Specifically, a for loop functions by running a section of code repeatedly until a certain condition has been satisfied. For-loops have two parts: a header and a body.
  3.  
  4. WHILE LOOP: In most computer programming languages, a while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. The while loop can be thought of as a repeating if statement.
  5.  
  6. Telegram post (TAP COPY AND USE FROM TELEGRAM)
  7. https://t.me/efxtv/2781
  8.  
  9.  
  10. # for loop syntax
  11.  
  12. for VARIABLE in 1 2 3 4 5 .. N
  13. do
  14. command1
  15. command2
  16. commandN
  17. done
  18.  
  19. or
  20.  
  21. for VARIABLE in file1 file2 file3
  22. do
  23. command1 on $VARIABLE
  24. command2
  25. commandN
  26. done
  27.  
  28. or
  29.  
  30. for OUTPUT in $(Linux-Or-Unix-Command-Here)
  31. do
  32. command1 on $OUTPUT
  33. command2 on $OUTPUT
  34. commandN
  35. done
  36.  
  37. # Examples
  38.  
  39.  
  40. #!/bin/bash
  41. #This type of for loop is characterized by counting
  42. for i in 1 2 3 4 5
  43. do
  44. echo "Welcome $i times"
  45.  
  46.  
  47. done
  48.  
  49.  
  50. #!/bin/bash
  51. #Sometimes you may need to set a step value (allowing one to count by two’s or to count backwards for instance).
  52. for i in {1..5}
  53. do
  54. echo "Welcome $i times"
  55. done
  56.  
  57.  
  58. #!/bin/bash
  59. #has inbuilt support for setting up a step value using {START..END..INCREMENT} syntax
  60. echo "Bash version ${BASH_VERSION}..."
  61. for i in {0..10..2}
  62. do
  63. echo "Welcome $i times"
  64. done
  65.  
  66.  
  67. #!/bin/bash
  68. #The seq command can be used as follows. A representative example in seq is as follows
  69. for i in $(seq 1 2 20)
  70. do
  71. echo "Welcome $i times"
  72. done
  73.  
  74.  
  75. #!/bin/bash
  76. #A representative three-expression example in bash as follows
  77. # set counter 'c' to 1 and condition
  78. # c is less than or equal to 5
  79. for (( c=1; c<=5; c++ ))
  80. do
  81. echo "Welcome $c times"
  82. done
  83.  
  84.  
  85. #!/bin/bash
  86. ## $@ expands to the positional parameters, starting from one. ##
  87. #./script one foo bar two three
  88. for i in $@
  89. do
  90. echo "Script arg is $i"
  91. done
  92.  
  93.  
  94. #!/bin/bash
  95. #find Png time
  96. # Purpose: Ping all of the IP addresses associated with my domain name www.cyberciti.biz
  97. ips="$(host -t a www.cyberciti.biz | awk '{ print $4}')"
  98. for i in $ips; do ping -q -c 4 "$i"; done
  99.  
  100.  
  101. # WHILE LOOP
  102.  
  103.  
  104. The syntax is as follows:
  105.  
  106. while [ condition ]
  107. do
  108. command1
  109. command2
  110. command3
  111. done
  112.  
  113.  
  114. OR
  115.  
  116. while [ condition ]; do commands; done
  117. while control-command; do COMMANDS; done
  118.  
  119.  
  120. # EXAMPLE
  121.  
  122.  
  123. #!/bin/bash
  124. #loop will print welcome 5 times on screen
  125. x=1
  126. while [ $x -le 5 ]
  127. do
  128. echo "Welcome $x times"
  129. x=$(( $x + 1 ))
  130. done
  131.  
  132.  
  133. #!/bin/bash
  134. # one liner for the above script
  135. x=1; while [ $x -le 5 ]; do echo "Welcome $x times" $(( x++ )); done
  136.  
  137.  
  138.  
  139. #!/bin/bash
  140. # Here is a sample shell code to calculate factorial using while loop
  141. counter=$1
  142. factorial=1
  143. # check if bash variable $counter set or not
  144. # see https://www.cyberciti.biz/faq/see-check-if-bash-variable-defined-in-script-on-linux-unix-macos/
  145. if [ -v $counter ]
  146. then
  147. echo "Syntax: $0 <number>"
  148. else
  149. while [ $counter -gt 0 ]
  150. do
  151. factorial=$(( factorial * counter ))
  152. counter=$(( counter - 1 ))
  153. done
  154. echo "$factorial"
  155. fi
  156.  
  157.  
  158.  
  159. #!/bin/bash
  160. # While loops are frequently used for reading data line by line from file
  161. FILE="$1"
  162. #
  163. # Check for input file else die with an error/syntax
  164. # message
  165. if [ -v $FILE ]
  166. then
  167. echo "Syntax: $0 <filename>"
  168. exit 1
  169. else
  170. # read $FILE using the file descriptors (FDs)
  171. exec 3<&0
  172. exec 0<"$FILE"
  173. while read -r line
  174. do
  175. # use $line variable to process line
  176. echo "$line"
  177. done
  178. exec 0<&3
  179. fi
  180.  
  181.  
  182.  
  183. #!/bin/bash
  184. # Infinite for while can be created with empty expressions
  185. while :
  186. do
  187. echo "infinite loops [ hit CTRL+C to stop]"
  188. done
  189.  
  190.  
  191.  
  192. #!/bin/bash
  193. # In this example, the break statement will skip the while loop when user enters -1, otherwise it will keep adding two numbers:
  194. while :
  195. do
  196. read -p "Enter two numnbers ( - 1 to quit ) : " a b
  197. if [ $a -eq -1 ]
  198. then
  199. break
  200. fi
  201. ans=$(( a + b ))
  202. echo "$ans"
  203. done
  204.  
  205.  
  206.  
  207. # UNTIL loop syntax
  208.  
  209. until [CONDITION]
  210. do
  211. [COMMANDS]
  212. done
  213.  
  214.  
  215. #!/bin/bash
  216. # In the example below, on each iteration the loop prints the current value of the variable counter and increments the variable by one.
  217. counter=0
  218. until [ $counter -gt 5 ]
  219. do
  220. echo Counter: $counter
  221. ((counter++))
  222. done
  223.  
  224.  
  225. #!/bin/bash
  226. # The following script may be useful when your git host has downtime
  227. until git pull &> /dev/null
  228. do
  229. echo "Waiting for the git host ..."
  230. sleep 1
  231. done
  232.  
  233. echo -e "\nThe git repository is pulled."
  234.  
  235.  
  236.  
  237. #!/bin/bash
  238. # Bash Until Loop example with a single condition
  239.  
  240. i=1
  241. until [ $i -gt 10 ]
  242. do
  243. echo $i
  244. ((i++))
  245. done
  246.  
  247.  
  248.  
  249. #!/bin/bash
  250. # Bash Until Loop example with multiple conditions
  251.  
  252. max=5
  253. a=1
  254. b=0
  255.  
  256. until [[ $a -gt $max || $b -gt $max ]];
  257. do
  258. echo "a = $a & b = $b."
  259. ((a++))
  260. ((b++))
  261. done
  262.  
  263.  
  264. #!/bin/bash
  265. # You can create an infinite loop using a false statement as an expression.
  266. count=0
  267. until false
  268. do
  269. echo "Counter = $count"
  270. ((count++))
  271. sleep 2
  272. done
  273.  
  274.  
  275.  
  276. #!/bin/bash
  277. # one liner for above
  278. until false; do echo "Counter = $count"; ((count++)); sleep 2; done
  279.  
  280.  
  281. #!/bin/bash
  282. # Using the same infinite loop example. Here when the count is equal to five continue
  283. count=0
  284. until false
  285. do
  286. ((count++))
  287. if [[ $count -eq 5 ]]
  288. then
  289. continue
  290. elif [[ $count -ge 10 ]]
  291. then
  292. break
  293. fi
  294. echo "Counter = $count"
  295. done
  296.  
  297.  
  298.  
  299. #!/bin/bash
  300. # Create a Bash script and paste the following lines
  301. #!/bin/bash
  302. until (( num > 8 ))
  303. do
  304. # Generate a random number between 1 and 10
  305. num=$(( (RANDOM % 10) + 1 ))
  306. echo "Generated number: $num"
  307. done
  308. echo "Loop finished!"
  309.  
  310.  
  311.  
  312. #!/bin/bash
  313. # until Loop with Multiple Conditions
  314. #!/bin/bash
  315. count=1
  316. until (( count > 10 ))
  317. do
  318. if (( count % 2 == 0 ))
  319. then
  320. echo "Even number: $count"
  321. else
  322. echo "Odd number: $count"
  323. fi
  324. if (( count == 5 ))
  325. then
  326. echo "Reached 5! Continuing to next iteration..."
  327. (( count++ ))
  328. continue
  329. fi
  330. if (( count == 8 ))
  331. then
  332. echo "Reached 8! Exiting the loop..."
  333. break
  334. fi
  335. (( count++ ))
  336. done
  337. echo "Loop finished!"
  338.  
  339.  
  340.  
  341.  
  342.  
  343.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement