LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   script help (https://www.linuxquestions.org/questions/linux-newbie-8/script-help-872418/)

dnoy 04-01-2011 11:44 AM

script help
 
All,

can someone please help me with something very basic. I have one script that goes through some IPs:


###############################3
#/usr/bin/sh
# This is a script to copy files from one host to a group of h osts


# $3 = third paramter (file that contains list of hosts)


HOSTFILE="list.txt"

#Im trying to do a loop that will go through every ip in the list.txt file
for host in $HOSTFILE
do
#need to figure out how to pass the variable to the expect script
/scripts/expect.sh
done
################################################

#this is a seperate script called expect.sh
#!/usr/bin/expect
#need to get variable here
spawn ssh test@$IP
expect "Password:"
send "test\r"
expect "#"
send "mkdir /test\r"
expect "#"


##################################################

thank you in advance

grail 04-01-2011 11:50 AM

So what is happening for you so far?

I see you have a variable called host (which may not be a great idea as there is an executable by the same name on a number of machines) which you never use anywhere?

Also I notice the variable $IP in expect script, where is this being set?

Answer some of these and it may help you when reading through the literature you are reading to help you.

dnoy 04-01-2011 12:14 PM

Ok so lists.txt has:
192.168.0.1
192.168.0.2
192.168.0.3

What im trying to do is bring this into the original script in one by one so I can pass them to the expect script. I saw a while loop that sais while read servers. Im not sure what variable I put after expect.sh.
It doesn’t seem that “/scripts/expect.sh $HOSTFILE” would work. One that is in do I modify my spawn ssh in expect to look like “spawn ssh test@$1”
?

grail 04-02-2011 02:40 AM

So you saw a while loop but changed to a for loop?? The can both read from a file but do it quite differently and have different affects.

This will demonstrate both using your list.txt:
Code:

#!/bin/bash

FILE=list.txt

while read -r LINE
do
    echo $LINE
done<$FILE

for LINE in $(< $FILE)
do
    echo $LINE
done

Now this will simply print the 3 IPs twice through. The catch with the for loop would be if you changed the file like so:
Code:

192.168 0.1
192.168 0.2
192.168 0.3

Due to the space, the for loop will now impose word splitting. So running the same script above you will get:
Code:

192.168 0.1
192.168 0.2
192.168 0.3
192.168
0.1
192.168
0.2
192.168
0.3

Here you see the while loop has kept the lines as they were in the file but the for loop has processed the line based on each space and each new line.

So assuming you know that you file has no white space anywhere to cause the issues above, your shell script can look like:
Code:

#/usr/bin/sh
# This is a script to copy files from one host to a group of h osts


# $3 = third paramter (file that contains list of hosts)


HOSTFILE="list.txt"

#Im trying to do a loop that will go through every ip in the list.txt file
for host in $(< $HOSTFILE)
do
#need to figure out how to pass the variable to the expect script
    /scripts/expect.sh $host
done

Are you sure that sh is under /usr/bin and not just /bin? Also, is there a reason for using sh as opposed to bash?

Quote:

One that is in do I modify my spawn ssh in expect to look like “spawn ssh test@$1”
I do not know a lot about expect scripts, but assuming it processes parameters the same as sh / bash, then yes that should work.


All times are GMT -5. The time now is 06:28 PM.