Hi Team,
Need your help for the below code snippet. I wrote a module to read the file names remote server using file name convention.
Issue : My script is coming out from while loop without reading complete file.
test1()
{
while read line
do
echo $line
file_nm_convention=`echo $line | awk -F${CFG_DELIMETER} '{print $1}'`
uid=`echo $line | awk -F${CFG_DELIMETER} '{print $2}'`
svr=`echo $line | awk -F${CFG_DELIMETER} '{print $3}'`
dir=`echo $line | awk -F${CFG_DELIMETER} '{print $4}'`
ssh $uid@$svr "cd $dir;ls -lrt $file_nm_convention" 1>${LST_FILE_NM}
cat $LST_FILE_NM
done < ${CFG_FILE}
exit
}
cat cfg_file
test*.ack|kittuad|svr_abc|/home/kittuad
s_test*.ack|kittuad|svr_abc|/home/kittuad
I am getting output only for test*.ack but not for s_test*.ack
sea
November 4, 2014, 7:57pm
2
How about:
CF=cfg_file
cat > $CF << EOF
test*.ack|kittuad|svr_abc|/home/kittuad
s_test*.ack|kittuad|svr_abc|/home/kittuad
EOF
oIFS="$IFS"
IFS="|"
while read file user svr home;do
printf '%s\n' \
"" \
"File: $file" \
"User: $user" \
"SVR: $svr" \
"Home: $home"
ssh $user@$svr << EOF
echo "Tasks on the remote"
cd $home
ls $file
EOF
done<$CF
IFS="$oIFS""
Produces this for me: (added the ssh line later)
File: test*.ack
User: kittuad
SVR: svr_abc
Home: /home/kittuad
File: s_test*.ack
User: kittuad
SVR: svr_abc
Home: /home/kittuad
Hope this helps
EDIT:
Dont know where LST_FILE_NM
is, but its not really required... unless you do another access somewhen later.
But since you just cat
the regular ls
output, you can loose cat
thanks for your reply. I got the solution
made below changes to the code and it is working
test1()
{
while read line
do
echo $line
file_nm_convention=`echo $line | awk -F${CFG_DELIMETER} '{print $1}'`
uid=`echo $line | awk -F${CFG_DELIMETER} '{print $2}'`
svr=`echo $line | awk -F${CFG_DELIMETER} '{print $3}'`
dir=`echo $line | awk -F${CFG_DELIMETER} '{print $4}'`
ssh $uid@$svr "cd $dir;ls -lrt $file_nm_convention" 1>${LST_FILE_NM} </dev/null
cat $LST_FILE_NM
done < ${CFG_FILE}
exit
}
Yes ssh was eating your standard input redirect from /dev/null will fix it for you.
It's worth looking at sea's solution as there are some nice techniques for extracting the uid, svr, etc without spawning 3 sub-shells and 3 awks.