Link to home
Start Free TrialLog in
Avatar of celtician
celticianFlag for American Samoa

asked on

Calling twice or more expect script /alternatives

I have two unix scripts, the first scrip calls a second one which contains several expect/send commands, (it has been modified, as it was working before without the expect). It works fine for the first time the expect script is being called, but can i call it several times? It does some kind of search from a remote machine... and tries to find some files.

This is the first (main) script, and the sentences where it calls the second one (as it is now) :
[...]

ferror=`./sftpER_v1.sh 2>&1 | grep Error_1234_$yesterday | grep -v ".done" | cut -c34-42`
[...]
echo "Error File size:" $ferror

[....]

fs1=`cat ./salidaftp | grep 01_$today | grep (blah blah) `
fs2=`cat ./salidaftp | grep 02_$today  | grep (blah blah)`
fsd1=`cat ./salidaftp | grep .03_$today | wc -l`
fsd2=`cat ./salidaftp | grep .04_$today| wc -l`

fee=`cat ./salidaftp | grep 07_$today| wc -l`

Open in new window


salidaftp has no value now, but before it would be storing the next info from the second script:

  rm salidaftp
  rm errorftp
  ./sftpER_v1.sh
  chmod 777 salidaftp
  chmod 777 errorftp

Open in new window



Where the second script (sftpER_v1) contains:

#!/usr/local/bin/expect -f

spawn sftp user@ip

expect "password:"

send -- "pass\r"



expect "sftp>"
send -- "cd ../../a/b/\r"
expect "sftp>"
send -- "ls -l\r"
expect "sftp>"
send -- "cd ../../c/d/\r"
expect "sftp>"
send -- "ls -l\r"
expect "sftp>"
send -- "quit\r"

expect eof

Open in new window


Before using the expect, it would contain the same info but just without the expect/send. Somehow i think it stored locally in a temp file the content of those folders in the remote server, and makes some greps over it in the main script.

Can i call the expect twice or more? if not, how can i make these sentences work???
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

I'm not sure if I fully understand what you're after.

Of course you can call a script from another script as often as you like.

I think the culprit is that the expect script does not write to salidaftp.

You could  run it once (i.e. a second time in the main script) to have salidaftp created,
or call it as often as needed to populate the variables fs1, fs2 etc. without creating a file (salidaftp).

The first solution would just consist of adding a new call to the expect script at line 8 of the main script:

./sftpER_v1.sh > salidaftp 2>&1
fs1=`cat ./salidaftp | grep 01_$today | grep (blah blah) ` # as already present in the main script!
fs2= ... ...

The second solution would consist of modifying the way the variables are filled:

fs1=`./sftpER_v1.sh 2>&1 | grep 01_$today | grep (blah blah) `
fs2=`./sftpER_v1.sh 2>&1 | grep 02_$today  | grep (blah blah)`
fsd1=`./sftpER_v1.sh 2>&1 | grep .03_$today | wc -l`
fsd2=`./sftpER_v1.sh 2>&1  | grep .04_$today| wc -l`
Are you having a problem calling expect a second time?
What did the second script contain before it contained expect?
I have to leave for round about three hours now, sorry.

I'll be back!
Avatar of celtician

ASKER

I understood i couldn't call twice the expect, i did call it and it seemed to work... but im not sure if this is correct or shouldn't be this way.

In theory it is working now (i have set the calls to the expect script)... But im not sure there can be any problem in the future, thats why i ask :)

ill read the answers tomorrow, so take your time.
ASKER CERTIFIED SOLUTION
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Oh ok thanks, somehow i understood i couldn't make multiple calls. Its working now flawlessly :)