#!/bin/bash # get the username parameter while getopts ":l:" options; do case $options in l ) username=$OPTARG;; esac done # correct the index so the next unattributed parameter is first shift $(($OPTIND - 1)) # read the name of the machine on which to operate hostname=$1 # collect all further arguments as a command to execute on server shift 1 command=$@ # if we are to execute locally, use local shell if [ "$username" = "$USER" -a "$hostname" = "localhost" ]; then eval sh -c \'$command\' else eval ssh -l $username $hostname \'$command\' fi