Bash Scripting While Loop

Hacking Truth
0





In Linux, all tasks from execution of services to loading and unloading of modules are carried out by programs and all programs need to be executed. You use the commands to access all the basic features of kernel. Shell scripting is a way to automate such tasks, and bash is one of the language, that has capabilities enough to be called as scripting as well as a language that can be used for programming on the POSIX platform, for small tasks. Bash Scripting While Loop



Using While Loop:



Create a bash file with the name, ‘while_loop.sh’, to know the use of while loop. In the example, while loop will iterate for 5 times. The value of count variable will increment by 1 in each step. When the value of count variable will 5 then the while loop will terminate.


#!/bin/bash

valid=true
count=1
while [ $valid ]
do
echo $count
if [ $count -eq 5 ];
then
break
fi
((count++))
done



Run the file with bash command.

$ bash while_loop.sh







Example-2: Using break statement for conditional exit


break statement is used to exit from the loop early based on a particular condition. Create a new bash file named while_loop2.sh with the following code.




#!/bin/bash

n=1
while [ $n -le 8  ]
do
        echo "Runnning $n time"
        (( n++ ))
done



Run the file with bash command.


$ bash while_loop2.sh





Example-3: Using continue statement to omit particular step


Create a new bash file named bash while_loop3.sh with the following code.

                                             

#!/bin/bash

n=0
while [ $n -le 5 ]
do
     (( n++ ))

     if [ $n == 3 ]
     then
           continue
     fi
     echo "Position: $n"

done







Run the file with bash command.

$ bash while_loop3.sh






In this example, the loop will iterate for 5 times but it will not print all 5 positions.


When the loop will iterate for 3rd times then continue statement will be executed and the loop will go for the next


iteration without printing the text of 3rd position. The following output will appear after executing the script.





Example-4: Creating infinite loop



Sometimes, it is required to declare infinite loop for various programming purposes. Create a new bash file named while_loop4.sh and test the code of infinite loop.


n=1
while :
do
         printf "The current value of n=$n\n"
         if [ $n == 3 ]
         then
                   echo "good"
         elif [ $n == 5 ]
         then
                  echo "bad"
         elif [ $n == 7 ]
         then
                  echo "ugly"
         elif [ $n == 10 ]
         then
                   exit 0
         fi
         ((n++))
done




Run the file with bash command.

$ bash while_loop4.sh









No termination condition is set for the loop in this example. This type of loop is called infinite loop. Here, exit statement is used to quit from the infinite loop. So, this loop will iterated for 10 times and when the iteration value become equal to 10 then exit statement will execute for exiting from the infinite loop.






I hope you liked this post, then you should not forget to share this post at all.
Thank you so much :-)

 

Disclaimer


This was written for educational purpose and pentest only.
The author will not be responsible for any damage ..!
The author of this tool is not responsible for any misuse of the information.
You will not misuse the information to gain unauthorized access.
This information shall only be used to expand knowledge and not for causing  malicious or damaging attacks. Performing any hacks without written permission is illegal ..!


All video’s and tutorials are for informational and educational purposes only. We believe that ethical hacking, information security and cyber security should be familiar subjects to anyone using digital information and computers. We believe that it is impossible to defend yourself from hackers without knowing how hacking is done. The tutorials and videos provided on www.hackingtruth.in is only for those who are interested to learn about Ethical Hacking, Security, Penetration Testing and malware analysis. Hacking tutorials is against misuse of the information and we strongly suggest against it. Please regard the word hacking as ethical hacking or penetration testing every time this word is used.


All tutorials and videos have been made using our own routers, servers, websites and other resources, they do not contain any illegal activity. We do not promote, encourage, support or excite any illegal activity or hacking without written permission in general. We want to raise security awareness and inform our readers on how to prevent themselves from being a victim of hackers. If you plan to use the information for illegal purposes, please leave this website now. We cannot be held responsible for any misuse of the given information.



- Hacking Truth by Kumar Atul Jaiswal


Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Post a Comment (0)
Our website uses cookies to enhance your experience. Learn More
Accept !