AboutWelcome to Free Software Daily (FSD). FSD is a hub for news and articles by and for the free and open source community. FSD is a community driven site where members of the community submit and vote for the stories that they think are important and interesting to them. Click the "About" link to read more...
Previously I've shown how to use the bash while loop in a bash script. Today I'm going to show how to use another type of do loop in bash: the for loop.
There are times when you will want to trim some information from the output of a command. This may be because you want to feed that output into another command. Whatever the reason for wanting to manipulate the output, awk is one of many tools available in GNU/Linux to perform this task.
One of the most common structures in programming is the Do Loop. The version that I’m going to show today is the While Do Loop. This post shows how to use the basic structure of the While Do loop in a Bash script.
Bash has had multi-processing for a long time, via job control, the $! environment variable, and the "wait" command. Judicious use of parentheses for sub-processes, and pipes where necessary, can put comparatively long-term procedures into the background. Bash 4 now provides a new multi-processing paradigm for shell scripting, via the "coproc" statement.
It took me ages to learn bash redirection properly, and I still have to concentrate sometimes to keep my &s and my >s straight. Here's the lowdown in case you, too, have intermittent brain failure on this one. Bash has three standard file descriptors: stdin, stdout and stderr, which refer respectively to input, output and error output.
Reading files is no big deal with bash: you just redirect the input to the script or pipe the output of another command into the script, or you could do it inside the script if the file names are pre-determined. You could also use process substitution to pass in the open files (command pipelines actually) from the command line.
Let's say you have a directory with hundreds of files with the wrong file names, and you'd like to replace every filename containing test with prod. (this is a contrived example). We can easily do this with the "for" command in bash, combined with a little bit of bash goodness. Today we'll learn how to replace text in a variable in a for loop.