Setup Ubuntu 16.04 expect Scripts
In the simple script bellow the goal is to simply find the oldest .zip file in a directory on a remote machine using ssh.
Then use scp to download this file to the local "workingBuild" directory.
Current Script
#!/usr/bin/expect
set user "hidden"
set pass "hidden"
set sourceDir "/opt/tomcat/someDirectoryName/"
set workingDir "/home/someUser/workingBuild"
spawn sh -c "ssh $user@www ls -t $sourceDir | head -1"
expect "password:"
send "$pass\r"
expect -re ".*\.zip"
set sourceFile $expect_out(0,string)
spawn sh -c "echo 'bob'$sourceFile'bob2'"
spawn sh -c "scp $user@www:$sourceDir$sourceFile $workingDir"
expect {
password: {send "$pass\r"; exp_continue}
}
Problem
It seems I'm getting a hidden carriage return (\r) populated into my $sourceFile variable at the Front of the file name stored. This in turn is causing problem in the scp command because it puts the filename and destinationDirectory on a new line like such. Causing an invalid command:
spawn sh -c scp meliudaj@www:/opt/tomcat/someDirectoryName/
build_0.0.1.zip /home/meliudaj/workingBuild
This is also demonstrated with the following debugging line in the above code:
spawn sh -c "echo 'bob'$sourceFile'bob2'"
$sourceFile'bob2' are always on a new line below 'bob'
The desired result would be as such:
spawn sh -c scp meliudaj@www:/opt/tomcat/someDirectoryName/ build_0.0.1.zip /home/meliudaj/workingBuild
Question
How can I either Remove the \r from the $sourceFile variable, or how can I populate the $sourceFile variable differently to prevent the (\r) from ever appearing in the first place.
P.S. I have tried a number of different attempts at using sed, awk, & tr But nothing I have tried seems to help.