Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using "for" & multiple commands inside of ssh in script

Status
Not open for further replies.

bajjer31

MIS
Sep 21, 2006
5
US
Forgive my newness...I am composing a small script to execute multiple times on several boxes. The local box works fine, but when it ssh's into box2, the "for" command doesn't work...it's only pulling the last date ($sun).

Code:
#!/bin/bash

# This will ask for input on which sa.* files you want to process
# then issue a sar command against them, output to /tmp/bill.##
# then email them to specified recipients

# Instructions
echo "Put in the date for each day in question to process those files..."

############# USER INPUTS VARIBLES HERE #################
# Variables for day of week
printf "Mon: "
read mon
printf "Tues: "
read tues
printf "Wed: "
read wed
printf "Thurs: "
read thurs
printf "Fri: "
read fri
printf "Sat: "
read sat
printf "Sun: "
read sun

############### THIS WORKS FINE ########################
# Take date variables and process files through sar to /tmp/bill.dd

for file in $mon $tues $wed $thurs $fri $sat $sun;
do echo "--- Processing sa$file ---";
sar -uf /var/adm/sa/sa$file > /tmp/bill.$file; 
done

# Email the files 
(/usr/bin/unix2dos -437 /tmp/bill.$mon | /usr/bin/uuencode bill.$mon;
/usr/bin/unix2dos -437 /tmp/bill.$tues | /usr/bin/uuencode bill.$tues;
/usr/bin/unix2dos -437 /tmp/bill.$wed | /usr/bin/uuencode bill.$wed;
/usr/bin/unix2dos -437 /tmp/bill.$thurs | /usr/bin/uuencode bill.$thurs;
/usr/bin/unix2dos -437 /tmp/bill.$fri | /usr/bin/uuencode bill.$fri ;
/usr/bin/unix2dos -437 /tmp/bill.$sat | /usr/bin/uuencode bill.$sat ;
/usr/bin/unix2dos -437 /tmp/bill.$sun | /usr/bin/uuencode bill.$sun) > deleteme.txt

######### HAVING PROBLEMS BELOW ###############
# ssh to 2ndserver
/usr/bin/ssh -l bjones 2ndserver "
for file in $mon $tues $wed $thurs $fri $sat $sun;
do echo "--- Processing sa$file ---";
sar -uf /var/adm/sa/sa$file > /tmp/bill.$file;
done

(/usr/bin/unix2dos -437 /tmp/bill.$mon | /usr/bin/uuencode bill.$mon;
/usr/bin/unix2dos -437 /tmp/bill.$tues | /usr/bin/uuencode bill.$tues;
/usr/bin/unix2dos -437 /tmp/bill.$wed | /usr/bin/uuencode bill.$wed;
/usr/bin/unix2dos -437 /tmp/bill.$thurs | /usr/bin/uuencode bill.$thurs;
/usr/bin/unix2dos -437 /tmp/bill.$fri | /usr/bin/uuencode bill.$fri ;
/usr/bin/unix2dos -437 /tmp/bill.$sat | /usr/bin/uuencode bill.$sat ;
/usr/bin/unix2dos -437 /tmp/bill.$sun | /usr/bin/uuencode bill.$sun) > deleteme.txt"

exit 0
 
What about this ?
# ssh to 2ndserver
/usr/bin/ssh -l bjones 2ndserver "
for file in $mon $tues $wed $thurs $fri $sat $sun;
do echo [!]\[/!]"--- Processing sa$file ---[!]\[/!]";
sar -uf /var/adm/sa/sa$file > /tmp/bill.$file;
done

(/usr/bin/unix2dos -437 /tmp/bill.$mon | /usr/bin/uuencode bill.$mon;
/usr/bin/unix2dos -437 /tmp/bill.$tues | /usr/bin/uuencode bill.$tues;
/usr/bin/unix2dos -437 /tmp/bill.$wed | /usr/bin/uuencode bill.$wed;
/usr/bin/unix2dos -437 /tmp/bill.$thurs | /usr/bin/uuencode bill.$thurs;
/usr/bin/unix2dos -437 /tmp/bill.$fri | /usr/bin/uuencode bill.$fri ;
/usr/bin/unix2dos -437 /tmp/bill.$sat | /usr/bin/uuencode bill.$sat ;
/usr/bin/unix2dos -437 /tmp/bill.$sun | /usr/bin/uuencode bill.$sun) > deleteme.txt"

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hi

Just some general ideas :
[ul]
[li]use quotes ( " )
Code:
sar -uf [red]"[/red]/var/adm/sa/sa$file[red]"[/red] > /tmp/bill.$file
[/li]
[li]remove the semicolons ( ; ) from the end of lines[/li]
[/ul]
Beside this, give us details about what the used enters for the day names and why all that is solved with seven [tt]read[/tt]s instead of one and a loop.

Feherke.
 
The user inputs a 2 digit number corresponding to the date (today is 21). So, it traps the numbers in the top of the script and uses them just fine in the first "for" expression. Then I ssh in to box2, and the only variable it reads in the "for" expression is the last one (for $sun). The rest seems to be working fine, it just won't process the for multiple times for each variable.

Thanks for the suggestions...I'm about at my wits end :)

I've tried both the "" in the sar -uf command and the // in the echo, but neither seem to change the broken functionality.

The output of the first "for" looks like:
Code:
--- Processing sa12 ---
--- Processing sa13 ---
--- Processing sa14 ---
--- Processing sa15 ---
--- Processing sa16 ---
--- Processing sa17 ---
--- Processing sa18 ---
and the second output looks like:
Code:
--- Processing sa18 ---
--- Processing sa18 ---
--- Processing sa18 ---
--- Processing sa18 ---
--- Processing sa18 ---
--- Processing sa18 ---
--- Processing sa18 ---
unix2dos: Couldn't open input file /tmp/bill.12.  No such file or directory.
unix2dos: Couldn't open input file /tmp/bill.13.  No such file or directory.
unix2dos: Couldn't open input file /tmp/bill.14.  No such file or directory.
unix2dos: Couldn't open input file /tmp/bill.15.  No such file or directory.
unix2dos: Couldn't open input file /tmp/bill.16.  No such file or directory.

Keep em coming! I'm willing to try anything!

 
You my try this:
# ssh to 2ndserver
/usr/bin/ssh -l bjones 2ndserver "
for file in $mon $tues $wed $thurs $fri $sat $sun;
do echo [!]\[/!]"--- Processing sa$file ---[!]\[/!]";
sar -uf /var/adm/sa/sa[!]\[/!]$file > /tmp/bill.[!]\[/!]$file;
done

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thanks for all the tips. I ended up just forgoing the 'for' process and spelling it out...it makes for a longer script, but it works!

Code:
# ssh to 2ndserver
/usr/bin/ssh -l username 2ndserver "

echo \"--- Processing sa$mon ---\";
sar -uf /var/adm/sa/sa$mon > /tmp/bill.$mon;
echo \"--- Processing sa$tues ---\";
sar -uf /var/adm/sa/sa$tues > /tmp/bill.$tues;
echo \"--- Processing sa$wed ---\";
sar -uf /var/adm/sa/sa$wed > /tmp/bill.$wed;
echo \"--- Processing sa$thurs ---\";
sar -uf /var/adm/sa/sa$thurs > /tmp/bill.$thurs;
echo \"--- Processing sa$fri ---\";
sar -uf /var/adm/sa/sa$fri > /tmp/bill.$fri;
echo \"--- Processing sa$sat ---\";
sar -uf /var/adm/sa/sa$sat > /tmp/bill.$sat;
echo \"--- Processing sa$sun ---\";
sar -uf /var/adm/sa/sa$sun > /tmp/bill.$sun;

(/usr/bin/unix2dos -437 /tmp/bill.$mon | /usr/bin/uuencode bill.$mon;
/usr/bin/unix2dos -437 /tmp/bill.$tues | /usr/bin/uuencode bill.$tues;
/usr/bin/unix2dos -437 /tmp/bill.$wed | /usr/bin/uuencode bill.$wed;
/usr/bin/unix2dos -437 /tmp/bill.$thurs | /usr/bin/uuencode bill.$thurs;
/usr/bin/unix2dos -437 /tmp/bill.$fri | /usr/bin/uuencode bill.$fri ;
/usr/bin/unix2dos -437 /tmp/bill.$sat | /usr/bin/uuencode bill.$sat ;
/usr/bin/unix2dos -437 /tmp/bill.$sun | /usr/bin/uuencode bill.$sun) |
/usr/bin/mailx -s \"Unix CPU Reports for 2ndserver\" email@email.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top