Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run expect script in current directory

I wrote a sftp expect script to upload and download files.

I put the script file in a folder. And double click the script to run the script to log in the remote server, but every time my script will log in the server in home folder not in the folder that where the script is.

#!/usr/bin/env expect
set login "username"
set addr "server.com"
set pw "mypassword"
set timeout -1
sleep 1
spawn sftp $login@$addr
expect "Password:" {send "$pw\r"}
sleep 1
interact

For example, I put this script on /Desktop and if I want to upload some files to my server from /Desktop in my local machine, I still have to cd into /Desktop and than run this script, if I just double click to execute the script it will log into my server from the ~ or /root whatever the default directory is. I want to log in my server from the directory where the script is.

Is there any way that I can find the location of a file? Or I need perform a searching to locate the file?

like image 756
Charles Chow Avatar asked Jun 17 '26 05:06

Charles Chow


1 Answers

You might want

cd [file dirname $argv0]

to change into the directory where the script lives.

like image 152
glenn jackman Avatar answered Jun 19 '26 01:06

glenn jackman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!