Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- OPIC: For and while LOOP in BASH
- 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.
- 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.
- Telegram post (TAP COPY AND USE FROM TELEGRAM)
- https://t.me/efxtv/2781
- # for loop syntax
- for VARIABLE in 1 2 3 4 5 .. N
- do
- command1
- command2
- commandN
- done
- or
- for VARIABLE in file1 file2 file3
- do
- command1 on $VARIABLE
- command2
- commandN
- done
- or
- for OUTPUT in $(Linux-Or-Unix-Command-Here)
- do
- command1 on $OUTPUT
- command2 on $OUTPUT
- commandN
- done
- # Examples
- #!/bin/bash
- #This type of for loop is characterized by counting
- for i in 1 2 3 4 5
- do
- echo "Welcome $i times"
- done
- #!/bin/bash
- #Sometimes you may need to set a step value (allowing one to count by two’s or to count backwards for instance).
- for i in {1..5}
- do
- echo "Welcome $i times"
- done
- #!/bin/bash
- #has inbuilt support for setting up a step value using {START..END..INCREMENT} syntax
- echo "Bash version ${BASH_VERSION}..."
- for i in {0..10..2}
- do
- echo "Welcome $i times"
- done
- #!/bin/bash
- #The seq command can be used as follows. A representative example in seq is as follows
- for i in $(seq 1 2 20)
- do
- echo "Welcome $i times"
- done
- #!/bin/bash
- #A representative three-expression example in bash as follows
- # set counter 'c' to 1 and condition
- # c is less than or equal to 5
- for (( c=1; c<=5; c++ ))
- do
- echo "Welcome $c times"
- done
- #!/bin/bash
- ## $@ expands to the positional parameters, starting from one. ##
- #./script one foo bar two three
- for i in $@
- do
- echo "Script arg is $i"
- done
- #!/bin/bash
- #find Png time
- # Purpose: Ping all of the IP addresses associated with my domain name www.cyberciti.biz
- ips="$(host -t a www.cyberciti.biz | awk '{ print $4}')"
- for i in $ips; do ping -q -c 4 "$i"; done
- # WHILE LOOP
- The syntax is as follows:
- while [ condition ]
- do
- command1
- command2
- command3
- done
- OR
- while [ condition ]; do commands; done
- while control-command; do COMMANDS; done
- # EXAMPLE
- #!/bin/bash
- #loop will print welcome 5 times on screen
- x=1
- while [ $x -le 5 ]
- do
- echo "Welcome $x times"
- x=$(( $x + 1 ))
- done
- #!/bin/bash
- # one liner for the above script
- x=1; while [ $x -le 5 ]; do echo "Welcome $x times" $(( x++ )); done
- #!/bin/bash
- # Here is a sample shell code to calculate factorial using while loop
- counter=$1
- factorial=1
- # check if bash variable $counter set or not
- # see https://www.cyberciti.biz/faq/see-check-if-bash-variable-defined-in-script-on-linux-unix-macos/
- if [ -v $counter ]
- then
- echo "Syntax: $0 <number>"
- else
- while [ $counter -gt 0 ]
- do
- factorial=$(( factorial * counter ))
- counter=$(( counter - 1 ))
- done
- echo "$factorial"
- fi
- #!/bin/bash
- # While loops are frequently used for reading data line by line from file
- FILE="$1"
- #
- # Check for input file else die with an error/syntax
- # message
- if [ -v $FILE ]
- then
- echo "Syntax: $0 <filename>"
- exit 1
- else
- # read $FILE using the file descriptors (FDs)
- exec 3<&0
- exec 0<"$FILE"
- while read -r line
- do
- # use $line variable to process line
- echo "$line"
- done
- exec 0<&3
- fi
- #!/bin/bash
- # Infinite for while can be created with empty expressions
- while :
- do
- echo "infinite loops [ hit CTRL+C to stop]"
- done
- #!/bin/bash
- # In this example, the break statement will skip the while loop when user enters -1, otherwise it will keep adding two numbers:
- while :
- do
- read -p "Enter two numnbers ( - 1 to quit ) : " a b
- if [ $a -eq -1 ]
- then
- break
- fi
- ans=$(( a + b ))
- echo "$ans"
- done
- # UNTIL loop syntax
- until [CONDITION]
- do
- [COMMANDS]
- done
- #!/bin/bash
- # In the example below, on each iteration the loop prints the current value of the variable counter and increments the variable by one.
- counter=0
- until [ $counter -gt 5 ]
- do
- echo Counter: $counter
- ((counter++))
- done
- #!/bin/bash
- # The following script may be useful when your git host has downtime
- until git pull &> /dev/null
- do
- echo "Waiting for the git host ..."
- sleep 1
- done
- echo -e "\nThe git repository is pulled."
- #!/bin/bash
- # Bash Until Loop example with a single condition
- i=1
- until [ $i -gt 10 ]
- do
- echo $i
- ((i++))
- done
- #!/bin/bash
- # Bash Until Loop example with multiple conditions
- max=5
- a=1
- b=0
- until [[ $a -gt $max || $b -gt $max ]];
- do
- echo "a = $a & b = $b."
- ((a++))
- ((b++))
- done
- #!/bin/bash
- # You can create an infinite loop using a false statement as an expression.
- count=0
- until false
- do
- echo "Counter = $count"
- ((count++))
- sleep 2
- done
- #!/bin/bash
- # one liner for above
- until false; do echo "Counter = $count"; ((count++)); sleep 2; done
- #!/bin/bash
- # Using the same infinite loop example. Here when the count is equal to five continue
- count=0
- until false
- do
- ((count++))
- if [[ $count -eq 5 ]]
- then
- continue
- elif [[ $count -ge 10 ]]
- then
- break
- fi
- echo "Counter = $count"
- done
- #!/bin/bash
- # Create a Bash script and paste the following lines
- #!/bin/bash
- until (( num > 8 ))
- do
- # Generate a random number between 1 and 10
- num=$(( (RANDOM % 10) + 1 ))
- echo "Generated number: $num"
- done
- echo "Loop finished!"
- #!/bin/bash
- # until Loop with Multiple Conditions
- #!/bin/bash
- count=1
- until (( count > 10 ))
- do
- if (( count % 2 == 0 ))
- then
- echo "Even number: $count"
- else
- echo "Odd number: $count"
- fi
- if (( count == 5 ))
- then
- echo "Reached 5! Continuing to next iteration..."
- (( count++ ))
- continue
- fi
- if (( count == 8 ))
- then
- echo "Reached 8! Exiting the loop..."
- break
- fi
- (( count++ ))
- done
- echo "Loop finished!"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement