LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 09-14-2011, 10:46 AM   #1
finwe
LQ Newbie
 
Registered: Sep 2011
Posts: 2

Rep: Reputation: Disabled
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

 
Old 09-16-2011, 12:26 AM   #2
nainesh
LQ Newbie
 
Registered: May 2010
Location: Ahmedabad-Gujarat
Distribution: Fedora
Posts: 27

Rep: Reputation: 0
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"
 
Old 09-17-2011, 02:26 AM   #3
finwe
LQ Newbie
 
Registered: Sep 2011
Posts: 2

Original Poster
Rep: Reputation: Disabled
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 View Post
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"
 
Old 09-19-2011, 12:27 AM   #4
nainesh
LQ Newbie
 
Registered: May 2010
Location: Ahmedabad-Gujarat
Distribution: Fedora
Posts: 27

Rep: Reputation: 0
yes you can use the counter script.

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

yes you can use counter script.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Expect: spawn id expr4 not open gabes Linux - General 3 09-15-2011 12:07 PM
[SOLVED] spawn with Expect script najee1987 Linux - General 5 05-25-2011 01:53 AM
exp_i_parse_states: : file16 is not an expect channel - use spawn -open to convert pcs1976 Programming 0 03-15-2011 07:58 AM
send: spawn id exp1025 not open in tcl/expect bharatbsharma Programming 3 04-22-2010 02:04 AM
Expect interpreter, spawn and cat Ricio Programming 11 08-16-2008 05:55 PM


All times are GMT -5. The time now is 04:58 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration