Run expect script in current directory

Charles Chow

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?

glenn jackman

You might want

cd [file dirname $argv0]

to change into the directory where the script lives.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Run a script in the same directory as the current script

How to change current work directory using expect script?

expect + change directory in expect script

Run bash script in current directory Linux

Expect script fails if directory exists

Expect script to run shell command

Shell script current directory?

How can I get Atom's script package to run a script from the script's current working directory?

Run bash script inside the expect script

How to run a Python script in a different directory accessing file of current directory(see example explaining issue)

Expect script continues to run even the expect does not match anything

Run local command from expect script

How to run bash script in the current directory from cmd-file by using Cygwin?

Bash script to run random file of few specified extentions contained in current directory or its subdirectories

Using bash to loop through nested folders to run script in current working directory

Calling bash shell script from expect script fails : "no such file or directory"

Obtain script current directory (so that I can do include files without relative paths and run the script from everywhere)

Run script from any directory

Run script error : No such file or directory

Bash script to run commands if in directory

Expect to scp directory does not work when run from post commit

Textmate 2 Ruby Run settings for current directory

How to run expect script from remote server via ssh?

SSH + Sudo + Expect in Bash script: Run command with sudo in remote machine

can you run half a script in bash and the other half in expect

ssh and run script on remote host and answer expect answer automate

Getting error while trying to run expect script inside bash

How to know the directory where the current script lies

Change the current directory from a Bash script