Table of Contents
How to Loop in Bash | Linux /Shell Script | Automation.
Here is a list of different ways and scenarios where we answer the question How to Loop in Bash | Linux /Shell Script | Automation.
Before that, we really need to know why do we need a loop in bash?
The answer is simple if we have repeated tasks and we want to automate it by command line in shell script we use the loop in bash.
Looping makes repeated things easy and can achieve many tasks like copy, move or delete files.
You can even use the loop in bash for server task automation like copying files to many remote network servers or exiting scripts in a bash loop script.
The bash loop is pretty easy if you practice a few scenarios. Using the bash loop I have automated so may day to day boring and no value tasks.
I have listed some common scenarios with examples and output and tried to answer How to Loop in Bash | Linux /Shell Script | Automation.
1. How to Loop in Bash | Linux /Shell Script | Automation: Bash For-Loop Script
Command : Bash For-Loop Script
for i in {1..10} do echo "num: $i" done
Command Output
➜ Downloads for i in {1..10} do echo "num: $i" done num: 1 num: 2 num: 3 num: 4 num: 5 num: 6 num: 7 num: 8 num: 9 num: 10
Few More Useful articles in thedbadmin.com
https://www.thedbadmin.com/list-of-top-5-software-technology-trends-in-2020/
2. How to Loop in Bash | Linux /Shell Script | Automation: Bash for loop strings
Note : vari=variable
Command: Bash for loop strings
for vari in john marry young Sheldon do echo "Name: $vari" done
Command Output
➜ Downloads for vari in john marry young Sheldon do echo "Name: $vari" done Name: john Name: marry Name: young Name: Sheldon
3.How to Loop in Bash | Linux /Shell Script | Automation: Bash Loop with until
Command
#!/bin/bash Number=10 until [ $Number -lt 1 ]; do echo Number $Number let Number-=1 done
Command Output
➜ ~ #!/bin/bash Number=10 until [ $Number -lt 1 ]; do echo Number $Number let Number-=1 done Number 10 Number 9 Number 8 Number 7 Number 6 Number 5 Number 4 Number 3 Number 2 Number 1
4.How to Loop in Bash | Linux /Shell Script | Automation: Bash Loop with Array
Command-Bash Loop with Array
whoryou=('I am engineer ' 'I am doctor ' 'I am dbadmin' 'I am a Teacher' 'I am a leader') for x in "${whoryou[@]}"; do echo "Who are you : $x" done
Command Output
➜ Downloads whoryou=('I am engineer ' 'I am doctor ' 'I am dbadmin' 'I am a Teacher' 'I am a leader') for x in "${whoryou[@]}"; do echo "Who are you : $x" done Who are you : I am engineer Who are you : I am doctor Who are you : I am dbadmin Who are you : I am a Teacher Who are you : I am a leader
5. How to Loop in Bash | Linux /Shell Script | Automation: Bash loop: SCP file to remote servers
Command
for server in $(cat server_name_list.txt) do scp file.txt "root@$server":/tmp/ done
[su_box title=”Note” box_color=” #0074be” title_color=”#0174be”]Note: You can send multiple files also using a wildcard character like *.txt or *.* (All the files) and can also add as many servers as you like in new line inside the server_name_list.txt according to requirement[/su_box]
Command output
➜ Downloads cat server_name_list.txt 192.168.56.22 ➜ Downloads for server in $(cat server_name_list.txt) do scp server_name_list.txt "root@$server":/tmp/ done [email protected]'s password: server_name_list.txt
6.How to Loop in Bash | Linux /Shell Script | Automation: List remote server directory
Command
➜ Downloads cat server.txt 192.168.56.21 192.168.56.22 192.168.56.23 example1.thedbadmin.com example2.thedbadmin.com
for server in $(cat server.txt) do ssh "root@$server" 'ls -lrt /tmp/file.txt' done
Command output
➜ Downloads for server in $(cat server.txt) do ssh "root@$server" 'reboot' done [email protected]'s password: Connection to 192.168.56.22 closed by remote host.
7. How to Loop in Bash | Linux /Shell Script | Automation: Bash loop: Copy/Move & Delete
Command: Bash loop: Copy/Move and Delete the file
# COPY File for x in *.txt; do cp $x /tmp; done # MOVE File for x in *.txt; do mv $x /tmp; done # DELETE File
— –Be extra careful before running it!!—-
for x in *.txt; do rm -f $x; done
I have given you a basic idea about “How to loop in bash”. You can modify the commands and loop as per your requirements and automation need. \
If you have any questions and need any help feel free to leave a comment and I will try to provide a solution.