LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Expect spawn shell commands (http://www.linuxquestions.org/questions/linux-newbie-8/expect-spawn-shell-commands-903004/)

finwe 09-14-2011 10:46 AM

Expect spawn shell commands
 
Hey there,

I'm trying to write a little script for a database backup but I don't know if it's actually possible to use spawn shell commands from within expect.

The problem : everything in this script runs, but apparently the mv command just creates an empty testltape.tmp even if testltape wasn't empty the same applies to using the cp command.
If I execute expect from the command line and use the same spawn commands, it works as it should and the .tmp file isn't empty.

Anyone any ideas?


#!/bin/sh

LTAPEDEV=testltape
TMP_FILE=testtemp
EXPECT=/usr/bin/expect

cat /dev/null > $LTAPEDEV
chmod 666 $LTAPEDEV
$EXPECT -c "
spawn ./ontap.sh
expect {
\"Please mount tape 1 on logical_log and press Return to continue ...\" {
send \"\r\"
exp_continue
}
\"Please mount tape 2 on logical_log and press Return to continue ...\" {
spawn mv $LTAPEDEV $LTAPEDEV.tmp
spawn touch $LTAPEDEV
spawn chmod 666 $LTAPEDEV
spawn chown informix:informix $LTAPEDEV
send \"\r\"
exp_continue
}
\"Do you want to back up the current logical log\" {
send \"\r\"
exp_continue
}
}

" > $TMP_FILE

The ontap.sh looks like :
#!/bin/sh

echo -n "Please mount tape 1 on logical_log and press Return to continue ...\n"
read a
echo -n "Please mount tape 2 on logical_log and press Return to continue ...\n"
read b
echo -n "Do you want to back up the current logical log\n"
read c
exit 0


nainesh 09-16-2011 12:26 AM

Hi finwe

just use system command instead of spawn command. For you script use below code. keep the double quote in script, don't remove. Use this code in your script as you can see.

Code:


system "mv $LTAPEDEV $LTAPEDEV.tmp"
system "touch $LTAPEDEV"
system "chmod 666 $LTAPEDEV"
system "chown informix:informix $LTAPEDEV"


finwe 09-17-2011 02:26 AM

Stupid...
 
Thank you Nainesh for the quick response.

I changed the spawn commands into system commands but then I saw a stupid mistake of me..

First I was trying to make a new $LTAPEDEV by using "cat /dev/null > $LTAPEDEV or > /$LTAPEDEV" but apparently even with escape character expect gives me an error when using the ">" so I changed the copy and > into a move and touch. I set appart the "cat /dev/null > $LTAPEDEV" before the expect procedure and I forgot to comment it out.. So every run it just empties the $LTAPEDEV before starting the expect which obviously leads to two empty files all the time..

Another question I have :

Now I'm using exp_continue for every expect part. I thought the last part
"\"Do you want to back up the current logical log\" {
send \"\r\"
exp_continue
}
doesnt need an exp_continue to make sure that after that the expect is not waiting anymore but when I don't put the exp_continue there, the script already stops before it checks the "Do you want to back up the current logical log\"

Suppose you can't know how many tapes we have, I would use for example tape 1, 2, 3 ,4 ,5 in the statements with continue but when expect finds the "Do you want to backup the current logical log" I want to break from the expect procedure so I shouldn't use the continue right?
Instead of using numbers, would it also be possible to use a counter in this?

Kind regards


Quote:

Originally Posted by nainesh (Post 4473187)
Hi finwe

just use system command instead of spawn command. For you script use below code. keep the double quote in script, don't remove. Use this code in your script as you can see.

Code:


system "mv $LTAPEDEV $LTAPEDEV.tmp"
system "touch $LTAPEDEV"
system "chmod 666 $LTAPEDEV"
system "chown informix:informix $LTAPEDEV"



nainesh 09-19-2011 12:27 AM

yes you can use the counter script.

---------- Post added 09-19-11 at 12:27 AM ----------

yes you can use counter script.


All times are GMT -5. The time now is 03:43 AM.