Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8122723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:55:22+00:00 2026-06-06T05:55:22+00:00

I am writing a Bash script and using Expect to do sftp. Now in

  • 0

I am writing a Bash script and using Expect to do sftp. Now in the Expect block I want to access a Bash variable in a conditional statement. But, I am unable to do so. How can do this?

Also, the execution of this script is controlled from a C program and I want redirect the output to a log file (which again is dynamic). Can I do that and suppress all the output on standard output.

Here is the code:

!/usr/bin/bash
host=$1
user=$2
pass=$3
action=$4
path=$5
echo "Starting...."

function doAction {

strAction="\""$action"\""
echo $strAction

/usr/bin/expect <<EOF > logfile.txt
**set bashaction $strAction**
spawn sftp $user@$host

expect "password:"
send "$pass\r"
expect"sftp>"
send "cd $path\r"
**if {$bashaction == "TEST"} {**
  expect "sftp>"
  send "prompt\r"
}

expect "sftp>"
send <sftp command>
expect "sftp>"
send_user "quit\n"

exit
  EOF
}

doAction
echo "DONE....."

For 1. using an Expect script instead worked.
For the logging issue, using log_user 0 and log_file -a <file> helped.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T05:55:24+00:00Added an answer on June 6, 2026 at 5:55 am

    You don’t need to use Bash. Expect can handle all that:

    #!/usr/bin/expect
    
    set host [lindex $argv 0]
    set user [lindex $argv 1]
    set pass [lindex $argv 2]
    set action [lindex $argv 3]
    set path [lindex $argv 4]
    puts "Starting...."
    
    puts "\"$action\""
    spawn sftp $user@$host
    
    expect "password:"
    send "$pass\r"
    
    expect"sftp>"
    send "cd $path\r"
    
    if {$action == "TEST"} {
        # Do something
    } else {
        # Do something else
    }
    
    expect "sftp>"
    send_user "quit\r"
    
    puts "DONE....."
    

    Coming from Bash, the Tcl/Expect syntax is a little strange, but you should not have any problem expanding the above skeleton.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im writing a bash-script to perform an offsite backup, using rsync over SSH. I'm
I am writing some BASH script and I want it have some error handling
I'm writing a Bash script, and I want to checkout a tag and then
While writing a bash script to help creating polaroid thumbnail using Imagick's convert commmand.
I'm writing a Perl script that generates a Bash script. I'm using open() with
I am writing a bash script which accept parameters. I am using getopts to
I am writing a bash script , I used 1 variable in my bash
I'm writing a script using multiple dialog screens in bash. One of the functions
I'm writing a bash script in which I read single characters from the input.
I'm writing a bash script that automatically deploys an application to a tomcat server.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.