Linux Bash While Read Line loop
“while read line” is bash while loop which is used many times for processing data on the line basis. this really makes life easy, when we like to process data from a file on liine basis we can also do these things with help of for loop. But this can also be done with help of “while read line” in easy method. I am using it most of times for my one line scripts that used to do many things as scripts do.
Syntax for “while read line” in script or one line
while read line do commands done
while read line ; do commands ; commands; done
Let check how it works with some examples.Like sometime we need to check cron deploy for all users present on system
awk -F: '{print }' /etc/passwd| while read line ; do echo +++++++++Cron for +++++++++;crontab -u -l ; done
This way we can able to get cron deploy by all Users present in /etc/passwd file”.
The second example, like we want to deploy bulk Users in Linux machine. This could be easily completed with help of “while read line” in small script.
#!/bin/bash cat user_list | while read line do useradd echo passw0rd | passwd --stdin done
So As per situation based there can so many use for this loop. I’ll try to add few more examples time to time for same.
In your last code there’s “echo cat user_list”
Is this right, or should it be only: “cat user_list”
You are right … i wrote it long time ago . but i agree . it should work with “cat user_list” as well. Thanks . let me edit it …