[SSH] Accessing remote directory with user-passed path


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [SSH] Accessing remote directory with user-passed path
# 1  
Old 10-19-2011
[SSH] Accessing remote directory with user-passed path

Hi everybody,

Currently, I have a script which access a remote computer via SSH, go to a folder already defined in the code and then executes a program in it, just like that:

Code:
ssh user@host  << EOI
cd path
./file
EOI

It executes fine, but now I want to pass an argument in the command line call. which is the name of the folder to "cd" to and the name of the file to run. So I tried the following:

Code:
dir=  $1
ssh user@host << EOI
cd $dir
pwd
./$1
EOI

, which doesn't work. It's as if after the "ssh user@host << EOI" line the script lost knowledge of the variable $dir as well as $1.

I'm pretty noob in shell scripting, so would you know any way to solve this?

Thanks in advance,
Luiz.
# 2  
Old 10-19-2011
Code:
dir=  $1

The spaces between = and $1 stop this from working, which is probably why $dir doesn't work.

When dir is $1, ./$1 won't do what you want, since you want to run ./file, not ./dir
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 10-19-2011
Quote:
Originally Posted by Corona688
Code:
dir=  $1

The spaces between = and $1 stop this from working, which is probably why $dir doesn't work.

When dir is $1, ./$1 won't do what you want, since you want to run ./file, not ./dir
You'are right. Now it works.
Thank you very much!

Luiz.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

What is the difference ../directory path and ./directory path in ksh?

What is the difference ../directory path and ./directory path in ksh? (1 Reply)
Discussion started by: TestKing
1 Replies

2. Shell Programming and Scripting

Unable to ssh and list files in local directory from remote.

#!/bin/bash script_work_dir="/home/websys/TEST_dpi_42_file_trnsfer_engine/PORT22/script_work_area" script_config_dir="/home/websys/TEST_dpi_42_file_trnsfer_engine/PORT22/script_config" dpi_sourceServerList=$script_config_dir"/dpi_sourceServerList" dpi_srvr_42="rtm@1.1.1.1"... (8 Replies)
Discussion started by: sadique.manzar
8 Replies

3. Shell Programming and Scripting

Execute ssh command with additional terminal command to any remote user not working script

Hello i am having an issue with bash script and this is the code now=$(cat hosts1.txt | awk '{print $2;}') while read n ;do ssh root@$now 'useradd test1; echo -e "test1\ntest1" | passwd test1 && echo "test1 ALL=(ALL:ALL) ALL" >> /etc/sudoers' When i execute only part with cat, it... (8 Replies)
Discussion started by: tomislav91
8 Replies

4. Programming

Trying to ssh as another user and cd to directory

I can ssh to another directory by doing the following: for server in server1; do ssh -t $server1 "cd /tmp; 'ls'";done However, if I try to do it as another user it fails: I have tried: for server in server1 do dir=$(su - nbadaccount -c "ssh $server `cd '/tmp/TSTCON'` " ) pwd... (3 Replies)
Discussion started by: newbie2010
3 Replies

5. Shell Programming and Scripting

ssh user@server ' cd path;j=0; for i in *;do;d=`du -sh $i | awk '{print( $1 )}'`;p=$d'|'$i;j=`expr $

Please help me to resolve below the issue in script ssh user@server ' cd path;j=0; for i in *;do;d=`du -sh $i | \ awk '{print( $1 )}'`;p=$d'|'$i;j=`expr $j + 1 `;arr=$p;echo ${arr};done' (1 Reply)
Discussion started by: SAUD PASHA
1 Replies

6. UNIX for Dummies Questions & Answers

ssh to remote machine with 2nd user and password

Hi all, Really hope someone can help me, i have been trying lots of things and just cant seem to nail it - and for something that seems straight forward.... Anyway, scenario is I need to log onto a second machine (remote server) from main workstation. Once logged in I need to run a batch... (2 Replies)
Discussion started by: Hopper_no1
2 Replies

7. Shell Programming and Scripting

[SSH] Need to connect to remote server as different user and without password

I have a task requiring that USER_A run a script, which connects to HOST_B as USER_B and does not ask for a password. If I am logged in on HOST_A as USER_B, I can connect to HOST_B without a password, no problem. However, if I try running ssh with the command line "ssh USER_B@HOST_B" while... (3 Replies)
Discussion started by: Totengraber
3 Replies

8. Shell Programming and Scripting

My variable cannot be passed through into my path

Hi, Can you please help. I am scripting in sh and I am trying to simply copy one directory to another but for some reason my variables are not recognised? echo "The latest version of the program is being found......." cd $SOFTWARE/src/$progname version=`ls $SOFTWARE/src/$progname | grep... (13 Replies)
Discussion started by: cyberfrog
13 Replies

9. Shell Programming and Scripting

Expect, SSH and multiple passed commands

Hey Everyone, I have found this script online that has almost all the features I am looking for. However, I do not know enough expect to debug the problem. http://linuxgazette.net/100/misc/tips/sshtool.expect.txt First, it Traps out after it collects the user's password. I do not know... (0 Replies)
Discussion started by: patchsmyle
0 Replies

10. Shell Programming and Scripting

how to find the path of a file when it is passed as ....filename(parameter) to script

hi unix guru's..................:confused: question is posted in the #3 permalink shown below. (3 Replies)
Discussion started by: yahoo!
3 Replies
Login or Register to Ask a Question