I have a main_parent_script.sh
inside which I am calling one more parent_script.sh
which has the below command:
/usr/bin/expect sftp_pass.sh list 22 sample_user hostname.hc.uk pass@123 get/FOR_20210420125933_NOTIFICATION_HEADER.json.zip > /temp/src_list.txt
Whenever I run the main parent script, it never populates the src_list.txt
file. But when I run the sftp_pass.sh command separately, it populates the src_list.txt
file correctly with below o/p:
-rwxrwxrwx 0 300096102 300096102 576 May 24 15:04 get/FOR_20210420125933_NOTIFICATION_HEADER.json.zip
sftp>
Also, I observed that the output is correctly populated when run by the main parent script, only when the output of ls command gives 2 or more files.
Below is the content of the sftp_pass.sh script:
log_user 0
##-------------Start of script----------------
set expectedcmd [lindex $argv 0]
set port [lindex $argv 1]
set user [lindex $argv 2]
set host [lindex $argv 3]
set pass [lindex $argv 4]
if {$expectedcmd=="list"} {
set file [lindex $argv 5]
spawn sftp -oPort=$port $user@$host
expect "password:"
send "$pass\n"
set prompt "sftp>";
set command "ls -l $file";
}
##---------------------------------
expect "$prompt" ;# wait for prompt
send "$command\r" ;# send command
expect "$command\r" ;# discard command echo
log_user 1
sleep 5
expect -re "(.*)\r" ;# match and save the result
expect eof
send -- "exit\r"
##-------------End of script--------------------
I even tried to use a log_file
to output the execution inside the script. But still I am having the same issue.
Try like this:
...
expect "$command\r" ;# discard command echo
expect *
send_user -- $expect_out(buffer)
log_user 1
sleep 5
expect -re "(.*)\r" ;# match and save the result
expect eof