Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm trying to make a bash function as follows:

  1. Will SSH into a server and chdir to ~/projects
  2. If you pass extra arguments ('git pull'), these will be executed. If not, skip step 2
  3. Leaves you with a bash shell

Right now, I have this:

function xyz {
   ssh -t xyz.com 'cd ~/projects; $*; bash'
}

Using this, running 'xyz' leaves me with a shell at xyz.com:~/projects, just like I want, but running 'xyz git pull' yields the following error:

/usr/bin/git: /usr/bin/git: cannot execute binary file

I'm sure I'm missing something simple, can anyone point me in the right direction?

Thanks!

share|improve this question
    
Not really helpful for your problem, but as a practice, I try to provide the directory (full path) as an argument to the program (git in this case) instead of chdir if I can. Usually ends up being less buggy. –  Kyle Brandt Dec 2 '09 at 18:50
    
Are you able to remove the $*; and execute the git pull by hand without error? –  retracile Dec 2 '09 at 19:23

3 Answers 3

up vote 0 down vote accepted

You've used a single quote, and variable substitution does not work within single-quoted strings:

$ function test { echo '$*'; }
$ test a b c
> $*

You see, $* stays as a literal. Use double quotes instead:

$ function test { echo "$*"; }
$ test a b c
> a b c

I believe in your case the $* is transferred to the target host "as is" and is substituted there, which executes something in your projects directory.

share|improve this answer
    
Thanks, this fixed the function! –  linkedlinked Dec 3 '09 at 21:26

One explanation would be that /usr/bin/git isn't executable. It could be a shell script without a #! line or a binary file that's not right for the host. When you log into the host interactively, you may be using another copy of git somewhere else due to your command path.

share|improve this answer

The error message looks to me like git is trying to execute git. My suspicion is that there is another function or alias that is getting executed.

Another potential problem is that it's really hard to get the quoting right when executing a variable. As a quick demonstration of the result of that effect, try this (with the quotation marks) at a shell prompt:

"ls -l"

In order to solve your problem, you might try:

function xyz {
   ssh -t xyz.com 'cd ~/projects; eval "$*"; bash'
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.