Ssh – How to write a shell script for OS X to log in to a remote server via SSH and execute commands

scriptingshellssh

This seems simple enough… I'm trying to write a script that I can just double click on in OS X. The script is to login via SSH to a remote server, run a git commit and exit. When I copy/paste my commands into terminal they work but when I save them in a .command file it seems like it connects but then hangs, if I type exit into the terminal window that has opened, the script will continue where it left off.

The script/commands are just:

#!/bin/sh
echo "**** committing remote changes on remote ****"
ssh username@server.com
cd /home/username/git/repo/
git commit -m "remote server commit"
exit

I'm new to writing scripts like this so it could be something really obvious I'm just missing (Since having read many q&a's here someone is bound to ask, yes my SSH keys are setup properly, copy/pasting this code works perfectly).

I don't think it relevant, but the server I'm getting to is a BlueHost/HostMonster shared web host with a jailed shell.

Best Answer

Haven't tried scripting from mac, but try this: ssh username@server.com 'cd /home/username/git/repo/ && git commit -m "remote server commit"'