Reese Knowledgebase

How to redirect command line results into while loop

View Kristian Reese's profile on LinkedIn


If you like this article, please +1 or Recommend via FB with the provided buttons above:

Article ID: 164
by: Reese K.
Posted: 25 Mar, 2015
Last updated: 25 Mar, 2015
Views: 623

How to redirect command line results into while loop

In a scenario where there are various config files contained throughout various subdirectories where one needs to commit mass updates quickly, it's possible to redirect the results of a command into a while loop.

#!/bin/bash

cd /opt/Jboss_5.1.2/server/

for file in file1.xml file2.xml file3.xml file4.xml file5.xml
do
	while read filename
	do
		#sed -i -e 's:<old_password>:<new_password>:g' $filename
		ls -1 ${filename}
 	done < <(find . -name ${file})
done

RESULTS

[kreese@linux01 /]$ ~/scripts/updatepw2.sh 
./JbossServer1/deploy/file1.xml
./JbossServer2/deploy/file1.xml
./JbossServer3/deploy/file1.xml
./JbossServer1/deploy/file2.xml
./JbossServer2/deploy/file2.xml
./JbossServer3/deploy/file2.xml
./JbossServer1/deploy/file3.xml
./JbossServer2/deploy/file3.xml
./JbossServer3/deploy/file3.xml
./JbossServer1/deploy/file4.xml
./JbossServer2/deploy/file4.xml
./JbossServer3/deploy/file4.xml
./JbossServer1/deploy/file5.xml
./JbossServer2/deploy/file5.xml
./JbossServer3/deploy/file5.xml

END

This article was:   Helpful | Not Helpful
Also listed in
folder UNIX -> Linux

Prev   Next
Rename batch of files     Ternary Example

RSS