3
votes

I have two scripts to run in local computer shell The first script is a BASH shell which calls an expect script inside itself The bash shell is as follow

BASH SHELL SCRIPT SaveOnRemote.sh

#!/bin/bash
REMOTE_IP_ADDR="192.168.100.67"
REMOTE_PASS="some_pass_with_whitespaces"     
#The following line space is defined {\ }
PC_EXEC_FILE="/home/workspace/prog/Modified\ code/test/test_file"

#The following line space is included in the variable
TARGET_EXEC_FILE="/sd/1/test/test file target dir/target_file"

#Calling the expect script
scp_ToTarget.exp  $REMOTE_PASS $REMOTE_IP_ADDR $PC_EXEC_FILE $TARGET_EXEC_FILE

End of BASH script SaveOnRemote.sh

And one more we have scp_ToTarget.exp Tcl or Expect script which is called inside the bash shell

Tcl Expect SCRIPT scp_ToTarget.exp

#!/usr/bin/expect -f
set remote_password [lrange $argv 0 0]     
set remote_ipaddr [lrange $argv 1 1]   
set src [lrange $argv 2 2]    
set dst [lrange $argv 3 3]     
set timeout 3    
log_user 1

eval spawn scp $src root@$remote_ipaddr:$dst    
match_max 100000

#Look for passwod prompt
set timeout 5

expect {    
  "*?assword:*" {    
  send $remote_password\r\n    
  }    
}

## End of Tcl Expect SCRIPT scp_ToTarget.exp

This scripts does not work and I get unknown directory/file error

In fact I can not avoid spaces neither in file names nor passwords and even directories and I have to make the scripts work.

As far as I know if I send command like

scp "/tmp/try me.txt" [email protected]:"/sd/no tries/" 

it works and I have no problem with white spaces. But as soon as I am in Tcl and add double-quotation to

"$src" or \"$src\" or "'$src'"

none of them make the syntax exactly the way I want, Tcl changes double-quote to braces etc I got stuck in this problem and I have no idea how to fix this problem either in bash or Expect or Tcl

Any good reference or web links for handling different string variables in Bash/Tcl is appreciated Thanks

3
For this purpose you'd better use an ssh key.gniourf_gniourf
remove the eval in eval spawn -- it will cause your command to not work for parameters with whitespace.glenn jackman

3 Answers

0
votes

You need to use quotes in the shell script when you're passing the parameters:

This is bad:

scp_ToTarget.exp  $REMOTE_PASS $REMOTE_IP_ADDR $PC_EXEC_FILE $TARGET_EXEC_FILE

This is good:

scp_ToTarget.exp "$REMOTE_PASS" "$REMOTE_IP_ADDR" "$PC_EXEC_FILE" "$TARGET_EXEC_FILE"

Because you did not quote the variables, the shell exands them into multiple words and the Expect script receives more than 4 parameters

Once you quote the variables in the shell script, Expect will receive them as you have planned.

0
votes

Thanks, I have implemented as follows

REMOTE_PASS="some_pass_with_whitespaces"
REMOTE_PASS="\"$REMOTE_PASS\""
# I have no clue if the passwords with whitespace or \ works fine for password

#The following line space is defined {\ }
PC_EXEC_FILE="/home/workspace/prog/Modified\ code/test/test_file"
eval PC_EXEC_FILE=$PC_EXEC_FILE
PC_EXEC_FILE="\"$PC_EXEC_FILE\""

#The following line space is included in the variable
TARGET_EXEC_FILE="/sd/1/test/test file target dir/target_file"
eval TARGET_EXEC_FILE=$TARGET_EXEC_FILE
TARGET_EXEC_FILE="\"$TARGET_EXEC_FILE\""

# The script is supposed to handle strings with {spc} and {\spc} used for
# directories but the filenames can not contain spaces 

#Calling the expect script
eval scp_ToTarget.exp  $REMOTE_PASS $REMOTE_IP_ADDR $PC_EXEC_FILE $TARGET_EXEC_FILE

In the expect file I added a line after expect {

  "*?assword:*" {    
  send $remote_password\r\n  
  interact 
  }    

It solves the problem with spaces in directories but not in the filenames!

I am still confused on how to solve the spaces in file name :(

0
votes

Would it fix the problem to use a string like this:

REMOTE_PASS="\\\"$REMOTE_PASS\\\""

It's "doubled escaping" what I figure you seem to have asked for.

(I'm aware that this is an old question but I found it searching with google and so might others ... and they might appreciate to find at least what finally solved my problem.)