no more loops


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting no more loops
# 1  
Old 06-08-2004
no more loops

Hello Guys
I need some help with my script
I'm not very good at this, hope you can point me in the right direction.
My idea, to write a file with some router commands and use it on a script for me to run on a demand basis, ( or added to the cron, if I get it to work).

The below script works, but the output is being multiply by the number of commands, for example:

cat files/commands
sh ver
sh hard
sh env

SCRIPT
~~~~~~~~~~~~~~~~~~~

#!/usr/bin/ksh

sed -n '1p' files/commands > files/temp1
sed -n '2p' files/commands > files/temp2
sed -n '3p' files/commands > files/temp3
sed -n '4p' files/commands > files/temp4
sed -n '5p' files/commands > files/temp5
sed -n '6p' files/commands > files/temp6
sed -n '7p' files/commands > files/temp7
sed -n '8p' files/commands > files/temp8
sed -n '9p' files/commands > files/temp9
sed -n '10p' files/commands > files/temp10

comm1=$(cat files/temp1)
comm2=$(cat files/temp2)
comm3=$(cat files/temp3)
comm4=$(cat files/temp4)
comm5=$(cat files/temp5)
comm6=$(cat files/temp6)
comm7=$(cat files/temp7)
comm8=$(cat files/temp8)
comm9=$(cat files/temp9)
comm10=$(cat files/temp10)

for router in user@metis
do
print
echo router $router
(sleep 5
echo password
echo $comm1
sleep 5
echo $comm2
sleep 5
echo $comm3
sleep 5
echo $comm4
sleep 5
echo $comm5
sleep 5
echo $comm6
sleep 5
echo $comm7
sleep 5
echo $comm8
sleep 5
echo $comm9
sleep 5
echo $comm10
sleep 5
echo exit
echo exit
sleep 5 ) | ssh $router >> ~/s/files/Router.commands
done

mailx -s "Router Output Requested" mymail@mail.com < ~/s/files/Router.commands

rm ~/s/files/Router.commands

# END
~~~~~~~~~~~~~~~~~~~
Output:

router metis
sh ver
sh hard
sh env

router metis
sh ver
sh hard
sh env

router metis
sh ver
sh hard
sh env

Thank you
Tony
tony3101
# 2  
Old 06-09-2004
use a for loop to get rid of all that repetition.
# 3  
Old 06-09-2004
Code:
sed -n '1p' files/commands > files/temp1
sed -n '2p' files/commands > files/temp2
sed -n '3p' files/commands > files/temp3
sed -n '4p' files/commands > files/temp4
sed -n '5p' files/commands > files/temp5
sed -n '6p' files/commands > files/temp6
sed -n '7p' files/commands > files/temp7
sed -n '8p' files/commands > files/temp8
sed -n '9p' files/commands > files/temp9
sed -n '10p' files/commands > files/temp10

becomes:

Code:
for (i = 1; i <= 10; i++)
{
    sed -n '$i'p' files/commands > files/temp$i
}

watch the syntax with the variable placement i have there, i dont know ksh.

you see how that works? it counds from 1 to 10 and stores that numbe in "i" each time. so when you use that variable it is just like having it there each time like you did before, except in a nice neat little loop.

you can use that for everytyhing that is repeated like that in your script.
# 4  
Old 06-09-2004
Unfortunately, that type of for construct will only work in c based shells and bash.

In ksh, you'll have to use the traditional
Code:
for i in 1 2 3 4 5 6 7 8 9 10
do
   # blah
done

or if you have the seq command,
Code:
for i in $(seq 1 10)
do
    #blah
done

You'll also need the sed command changed slightly to:
Code:
sed -n "${i}p" files/commands > files/temp${i}

Notice the double quotes - this allows the value of i to be evaluated.

Cheers
ZB
# 5  
Old 06-09-2004
I wouldn't even use all those temp files...
Code:
#!/usr/bin/ksh

export infile=files/commands
export outfile=~/s/files/Router.commands

for router in user@metis
do
  print
  echo router $router
  ( sleep 5
    echo password
    while read comm
    do
      echo $comm
      sleep 5
    done < $infile
    echo exit
    echo exit
    sleep 5 ) | ssh $router
done > $outfile 2>&1

mailx -s "Router Output Requested" mymail@mail.com < $outfile

rm $outfile

# END

# 6  
Old 06-09-2004
It works great

Thank you all
Tony
tony3101
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Need help with for loops

Why wont my for statements work? Im trying to get this script to swich to a user an if you put in a start/stop/or restart paramater to do just that for each user. I commented out the actual start/stop actions to test it just by using echos and not do anything hasty in the environment but it... (0 Replies)
Discussion started by: LilyClaro
0 Replies

2. UNIX for Dummies Questions & Answers

loops with tr

Hello, I'm not sure if this is more appropriate for the 'unix for dummies' or the 'unix for experts' forum because I'm new to this forum and this is the second topic I've discussed, but if you could let me know which one was more appropriate for something like this, please do! So in tr (an... (2 Replies)
Discussion started by: juliette salexa
2 Replies

3. Shell Programming and Scripting

Loops

Hi All, I want to execute a script the number of times a user enters. Please can you advise on hor can I do the same. Many Thanks, Shazin (4 Replies)
Discussion started by: Shazin
4 Replies

4. Shell Programming and Scripting

Help with the 2 for loops

#!/bin/bash IFS=$'\n' A= a c b t g j i e d B= t y u i o p counter=0 found="" for i in $(cat $A) do for j in $(cat $B) do if then found="yes" fi done if then (1 Reply)
Discussion started by: vadharah
1 Replies

5. UNIX for Dummies Questions & Answers

Help with While Loops

I am traversing down a list, and I am not quite sure how to tell the loop to break when it's done going through the file. #!/bin/sh while : do read list <&3 echo $list done is the code. The file "list" is simply 5 4 3 2 1 any advice on how to break the loop after the file is... (1 Reply)
Discussion started by: MaestroRage
1 Replies

6. Shell Programming and Scripting

while loops

Hi I've a file like so: Now, I want to read my file and take ex. the Media ID and the Type for each groups of Media (Media1,Media2,...,Media(n): cat /tmp/file|\ while read FILE do while $(FILE|cut -d: -f1)=Media$i do #here will be some test, ex: #if Media ID < 23 ... (4 Replies)
Discussion started by: nymus7
4 Replies

7. UNIX for Dummies Questions & Answers

two loops

Hi, how can I use "for" to have two loops : this is my script : for i in (A B C) do for j in (a b c) do echo $i$j done done #End I want to print out Aa Ab Ac .... But I have error message : syntax error at line 1 : `(' unexpected Many thanks before. How should I use "for" ?? (2 Replies)
Discussion started by: big123456
2 Replies

8. UNIX for Dummies Questions & Answers

While Loops

I'm trying to create a loop that will prompt the user for 15 values, not forcing them to enter all 15. If the user enters through one or more of the prompts the null value needs to be converted to 0, otherwise set the parameter = to the value entered: ex. Please enter file no #1: 17920 ... (4 Replies)
Discussion started by: vdc
4 Replies

9. Shell Programming and Scripting

Loops within loops

I am running on HPUX using ksh. I have a script that uses a loop within a loop, for some reason the script seems to hang on a particuliar record. The record is fine and hits the condition in Blue. If I kill the 1st loop process the script continues on with no problem. Begin code> <Some... (8 Replies)
Discussion started by: bthomas
8 Replies

10. UNIX for Dummies Questions & Answers

loops?

hello....very new user to unix...and i have a question..i am not sure if there is such a thing For example...the user is asked if he likes Bananas....if he says yes.... echo You like Bananas $name at the end of the script it echos all that the user has entered so they can read it.... but... (1 Reply)
Discussion started by: jonas27
1 Replies
Login or Register to Ask a Question