Linux Tutorials

Bash foreach loop examples for Linux / Unix

SEOClerks

I used foreach on a Unix working system and csh shell loop. Now, I’m a Linux working system. How do I take advantage of foreach loop in bash shell operating on Linux?

The C shell (csh) or the improved model, tcsh is a Unix shell from the late 1970s. The csh foreach loop syntax is as follows: foreach n ( 1 2 three four 5 )
#command1
#command2
finish

Linux foreach tcsh example
Nonetheless, bash lacks foreach syntax; as a substitute, you should utilize bash whereas loop or bash for loop syntax as described under.

Bash foreach loop examples

Allow us to say you wish to convert the next csh forloop instance:

foreach i ( * )
echo “Working $i filename …”
finish

Unix foreach tcsh example
Above csh or tcsh foreach displayed a listing of information utilizing loop. Right here is comparable code in a bash shell:

for i in *
do
echo “Engaged on $i file…”
completed

Bash foreach loop example
Nonetheless, one can discover and safely deal with file names containing newlines, areas, particular characters utilizing the discover command

## examined on GNU/Linux discover ##
## and bsd/discover solely ##
discover /dir/ -print0 | xargs -r0 command
discover /tmp/take a look at -print0 | xargs -I -r0 echo “Engaged on “” file …”
discover /tmp/take a look at -type f -print0 | xargs -I -r0 echo “Engaged on ” file …”

Engaged on ‘/tmp/take a look at’ file …
Engaged on ‘/tmp/take a look at/hosts.deny’ file …
Engaged on ‘/tmp/take a look at/My Resume.pdf’ file …
Engaged on ‘/tmp/take a look at/hostname’ file …
Engaged on ‘/tmp/take a look at/hosts.permit’ file …
Engaged on ‘/tmp/take a look at/One other file identify.txt’ file …
Engaged on ‘/tmp/take a look at/resolv.conf’ file …
Engaged on ‘/tmp/take a look at/hosts’ file …
Engaged on ‘/tmp/take a look at/host.conf’ file …

Howto use foreach in bash shell

Say you could have a file named lists.txt as follows:
cat lists.txt
Pattern outputs:

/nfs/db1.dat
/nfs/db2.dat
/nfs/share/gross sales.db
/nfs/share/acct.db
/nfs/non-public/customers.db
/nfs/non-public/payroll.db

Every in there’s a file situated in your Unix or Linux server. Right here is methods to learn lists.txt file and work on every file listed in lists.txt:

for n in $(cat lists.txt )
do
echo “Engaged on $n file identify now”
# do one thing on $n under, say rely line numbers
# wc -l “$n”
completed

Though for loop appears straightforward to make use of for studying the file, it has some issues. Don’t attempt to use “for” to learn file line by line in Linux or Unix. As a substitute, use whereas loop as follows:

#!/bin/bash
## path to enter file
enter=“lists.txt”
 
## Allow us to learn a file line-by-line utilizing whereas loop ##
whereas IFS= learn -r line
do
printf ‘Engaged on %s file…n’ $line
completed < $enter

Foreach loop in bash

Posted by: Vivek Gite

The creator is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a coach for the Linux working system/Unix shell scripting. Get the newest tutorials on SysAdmin, Linux/Unix and open supply subjects by way of RSS/XML feed or weekly e-mail e-newsletter.

Source link

Related Articles

Leave a Reply

Back to top button