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=`./ 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
  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???
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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:

./ > 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=`./ 2>&1 | grep 01_$today | grep (blah blah) `
fs2=`./ 2>&1 | grep 02_$today  | grep (blah blah)`
fsd1=`./ 2>&1 | grep .03_$today | wc -l`
fsd2=`./ 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!
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

celticianAuthor Commented:
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.
Unless the expect script would modify something on the remote side
so that the results of subsequent calls would be different from the results of previous calls
there's no reason to get worried.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
celticianAuthor Commented:
Oh ok thanks, somehow i understood i couldn't make multiple calls. Its working now flawlessly :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Unix OS

From novice to tech pro — start learning today.