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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Conditional Statement in Expect script

Status
Not open for further replies.

rubbersoul69

IS-IT--Management
Oct 2, 2006
10
CA
Hi All,

I'm writing an expect script for an installation of some software on a Linux box. Where I'm getting stuck is in the beginning when I'm looking for the presence of a file. If it's there I want to rename it to acme.conf.1, so that when the software installs it can touch acme.conf.

Hope someone can help here.....

#!/usr/local/bin/expect

set authFile "/etc/acme.conf"

if {![file exists $authFile]} { ;# Does file not exist
set
Need say do nothing here
else
send "/etc/psynch.conf" "/etc/psynch.conf1"
}

#connect to system
spawn ssh root@10.10.42.27\r
#modify line below if you have already added system fingerprint to known hosts.
expect "Please type 'yes' or 'no':"
send yes\r
expect "password: "
send 5555555\r
expect "linux-zwny:~ #"


That's the basis of it...you can see where im stuck at the top.

 
Is the file on the remote or local system? Are "acme" and "psynch" referring to the same file?

Annihilannic.
 
OK sorry....

forget everything I put above.

What I need is a simple loop that looks for a file name acme.conf under /etc

When it finds it I just want it to append a number say acme.conf.1

If it's run again it should copy the file again and call it acme.conf.2

If it doesn't find it the script ends....that's it. Sorry guys...Newb here :)


thxs
 
I see... but my question still stands... is /etc/acme.conf on the local system, or the system you are connecting to via ssh?

Annihilannic.
 
It would reside on a remote system that I'd be connecting too.
 
Was thinking something like this....but I'm NO script guy at all and this doesn't work:

#!/bin/bash

# test to see if acme.conf exists

i=0

if [ -e "/etc/acme.conf" ];

then

echo "you have a conf file already. I'm gonna back it up."

mv "/etc/acme.conf" "/etc/acme.conf"+ i

i=0+1

else
echo "you have no conf file. Let me install the listner."
fi
 
This should do it. You shouldn't need expect for this task; I've added the -o StrictHostKeyChecking=no SSH option so that it shouldn't prompt you to type "yes".

Code:
#!/bin/bash

ssh -o StrictHostKeyChecking=no root@10.10.42.27 '
    if [[ -e "/etc/acme.conf" ]]
    then
        echo "You have a conf file already. I am gonna back it up."
        i=1
        until [[ ! -e "/etc/acme.conf.$i" ]] ; do ((i=i+1)) ; done
        mv "/etc/acme.conf" "/etc/acme.conf.$i"
    else
        echo "You have no conf file. Let me install the listener."
    fi 
'

Notice that I changed the "I'm" to "I am" since the apostrophe would have prematurely terminated the script supplied to ssh.

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top