sriram2207
sriram2207

Reputation: 25

Passing output of a command within a for Statement

I am trying to use a Nested for to make updates to sub-directories within a directory Structure , the value of j is fetched based on value of i in the previous for , the script does not seem to be fetching the value of j properly , here's the script attached ,the challenge seems to be in executing the line for j in cat /tmp/echo $i`` , I have tried putting simply $i , but does not seem to picking it , any help for the problem is appreciated , Thanks in advance .

#!/bin/bash
set -xv
rm /tmp/MDMs /tmp/MDMswithBlanks
rm -rf /tmp/Tenants*
cd /images/SCWA-SaaS/latest/config/
ls -lrt | awk '{print $9;}' | grep -v "controller" >> /tmp/MDMswithBlanks
sed '/^$/d' /tmp/MDMswithBlanks >> /tmp/MDMs

for i in `cat /tmp/MDMs`
do
cd /images/SCWA-SaaS/latest/config/
cd $i
ls -lrt | grep "drwx" | awk '{print $9;}' >> /tmp/`echo $i`

**for j in `cat /tmp/`echo $i``**
do
cd /images/SCWA-SaaS/latest/config/
cd $i
cd $j
cd /tmp
mkdir $i$j
cd /images/SCWA-SaaS/latest/config/
cd $i
cd $j
cp -p configFiles.zip /tmp/`echo $i$j`
cd /tmp/`echo $i$j`
unzip configFiles.zip
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/Tivoli/TWS/GSKit32/8/lib
/usr/Tivoli/TWS/GSKit32/8/bin/gsk8capicmd -cert -add -file /tmp/DigiCertTrustedRoot.pem -label DigiCertTrustedRoot -db /tmp/`echo $i$j`/TWSClientKeyStore.kdb -pw default
/usr/Tivoli/TWS/GSKit32/8/bin/gsk8capicmd -cert -add -file /tmp/DigiCertCA2.pem -label DigiCertCA2 -db /tmp/`echo $i$j`/TWSClientKeyStore.kdb -pw default
rm configFiles.zip
zip configFiles.zip TWSClientKeyStore.kdb installAgent.properties
chown root:root configFiles.zip
chmod 544 configFiles.zip
cd /images/SCWA-SaaS/latest/config/
cd $i
cd $j
#rm configFiles.zip
cp -p /tmp/`echo $i$j`/configFiles.zip .
#rm -rf /tmp/`echo $i$j`
done
done

Regards, Sriram.V

Upvotes: 0

Views: 46

Answers (2)

sriram2207
sriram2207

Reputation: 25

Thanks for the Suggestions everyone , I managed to execute it Successfully this way ,so suing a variable for whole path TENANT="/tmp/CERT/echo $i" :

#!/bin/bash
set -xv
rm /tmp/CERT/MDMs /tmp/CERT/MDMswithBlanks
cd /images/SCWA-SaaS/latest/config/
ls -lrt | awk '{print $9;}' | grep -v "controller" >> /tmp/CERT/MDMswithBlanks
sed '/^$/d' /tmp/CERT/MDMswithBlanks >> /tmp/CERT/MDMs

for i in `cat /tmp/CERT/MDMs`
do
cd /images/SCWA-SaaS/latest/config/
cd $i
ls -lrt | grep "drwx" | awk '{print $9;}' >> /tmp/CERT/`echo $i`

TENANT="/tmp/CERT/`echo $i`"

for j in `cat $TENANT`
do
cd /images/SCWA-SaaS/latest/config/
cd $i
cd $j
cd /tmp/CERT
mkdir $i$j
cd /images/SCWA-SaaS/latest/config/
cd $i
cd $j
TENANT_PREFIX="/tmp/CERT/`echo $i$j`"
cp -p configFiles.zip $TENANT_PREFIX
cd $TENANT_PREFIX
unzip configFiles.zip
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/Tivoli/TWS/GSKit32/8/lib
/usr/Tivoli/TWS/GSKit32/8/bin/gsk8capicmd -cert -add -file /tmp/DigiCertTrustedRoot.pem -label DigiCertTrustedRoot -db $TENANT_PREFIX/TWSClientKeyStore.kdb -pw default
/usr/Tivoli/TWS/GSKit32/8/bin/gsk8capicmd -cert -add -file /tmp/DigiCertCA2.pem -label DigiCertCA2 -db $TENANT_PREFIX/TWSClientKeyStore.kdb -pw default
rm configFiles.zip
zip configFiles.zip TWSClientKeyStore.kdb installAgent.properties
chown root:root configFiles.zip
chmod 544 configFiles.zip
cd /images/SCWA-SaaS/latest/config/
cd $i
cd $j
rm configFiles.zip
cp -p $TENANT_PREFIX/configFiles.zip .
#rm -rf $TENANT_PREFIX
done
done

Upvotes: 0

nullPointer
nullPointer

Reputation: 4574

For iterating over dirs/subdirs, it would be better you use find command:

for i in $(find /tmp/MDMs -type d)
do
...
done

Upvotes: 1

Related Questions