2

I want to search different packages in multiple servers in Solaris 10. One file contains the package information, another file contains the server information.

I have tried this:

bash-3.00# cat pk
"VRTSvcs|VRTSvxfen"
"SUNWfmd|SUNWfsmgtr"

bash-3.00# cat ser
mokshi
niki

This is my script:

bash-3.00# cat tt
#!/usr/bin/bash
>output
for j in `cat ser`
do
for ip in `cat pk`
do

M=`ssh $j  "pkginfo |egrep $ip |cut -d \" \" -f7 "`;
echo "$j " >>output
echo "$M" >>output
done
done

The expected output is

cat output
bash-3.00# cat output

moksha

VRTSvcs
VRTSvcsag
VRTSvcsea
VRTSvxfen

niki

SUNWfmd
SUNWfmdr
SUNWfsmgtr

but when I run the script it is running twice like this:

bash-3.00# bash -x tt

++ cat ser

+ for j in '`cat ser`
'
++ cat pk

+ for ip in '`cat pk`'
++ ssh mokshi 'pkginfo |egrep "VRTSvcs|VRTSvxfen" |cut -d " " -f7 '
Password:
+ M='VRTSvcs
VRTSvcsag
VRTSvcsea
VRTSvxfen'
+ echo 'mokshi '
+ echo 'VRTSvcs
VRTSvcsag
VRTSvcsea
VRTSvxfen'
+ for ip in '`cat pk`'
++ ssh mokshi 'pkginfo |egrep "SUNWfmd|SUNWfsmgtr" |cut -d " " -f7 '
Password:
+ M='SUNWfmd
SUNWfmdr
SUNWfsmgtr'
+ echo 'mokshi '
+ echo 'SUNWfmd
SUNWfmdr
SUNWfsmgtr'
+ for j in '`cat ser`'
++ cat pk
+ for ip in '`cat pk`'
++ ssh niki 'pkginfo |egrep "VRTSvcs|VRTSvxfen" |cut -d " " -f7 '
Password:
+ M='VRTSvcs
VRTSvcsag
VRTSvcsea
VRTSvxfen'
+ echo 'niki '
+ echo 'VRTSvcs
VRTSvcsag
VRTSvcsea
VRTSvxfen'
+ for ip in '`cat pk`'
++ ssh niki 'pkginfo |egrep "SUNWfmd|SUNWfsmgtr" |cut -d " " -f7 '
Password:
+ M='SUNWfmd
SUNWfmdr
SUNWfsmgtr'
+ echo 'niki '
+ echo 'SUNWfmd
SUNWfmdr
SUNWfsmgtr'

And I am getting output like this:

bash-3.00# cat output

moksha

VRTSvcs
VRTSvcsag
VRTSvcsea
VRTSvxfen

moksha

SUNWfmd
SUNWfmdr
SUNWfsmgtr

niki

VRTSvcs
VRTSvcsag
VRTSvcsea
VRTSvxfen
niki

SUNWfmd
SUNWfmdr
SUNWfsmgtr

The main goal of the script is that its take one server from server file and it has to search first line in package file. The it has to take second server name from server name and it searches for second line in package file.

please help me where I made a mistake.

codeforester
  • 39,467
  • 16
  • 112
  • 140
naveen v
  • 21
  • 1

1 Answers1

2

You could do this - assuming you have an equal number of lines in your server and package files:

#!/usr/bin/env bash
while read -u 3 -r server && read -u 4 -r pkg; do
  m=$(ssh -n "$server" "pkginfo | egrep '$pkg' | cut -d' ' -f7")
  echo "$server"
  echo "$m"
done 3<ser 4<pk >> output
  • 3<ser connects fd 3 to file ser and 4<pk connects fd 4 to file pk
  • read -u 3 reads from file descriptor 3, read -u 4 reads from file descriptor 4
  • $() is better than back ticks (see the post below to find why)
  • cut -d' ' -> guess your delimiter is a space
  • ssh -n is needed so that ssh ignored stdin and doesn't interfere with read
  • it is better to put >> output at the end for more efficient I/O

See also:

Community
  • 1
  • 1
codeforester
  • 39,467
  • 16
  • 112
  • 140
  • thanks for help .can I know what is 3 and 4 here. And below sentence also I did not understand.what is fd 3 – naveen v Feb 26 '17 at 06:22
  • Check this post to understand file descriptors: http://stackoverflow.com/questions/5256599/what-are-file-descriptors-explained-in-simple-terms – codeforester Feb 26 '17 at 06:41