Skip to content

Commit

Permalink
add support for : in the path
Browse files Browse the repository at this point in the history
Signed-off-by: Mattias Andrée <[email protected]>
  • Loading branch information
maandree committed May 7, 2015
1 parent bab697a commit f941ae5
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions sshcd
Original file line number Diff line number Diff line change
@@ -1,7 +1,28 @@
#!/bin/bash
options=("${@:1:$(($# - 1))}") # Everything in "$@" except the last argument
target="${!#}" # The last argument from "$@"
remote="${target%:*}"
path="${target##*:}"
options=("${@:1:$(($# - 1))}") # Everything in "$@" except the last argument
target="${!#}" # The last argument from "$@"

# Get user
usertest="${target%%:*}"
if [ ! "${usertest/@/}" = "${usertest}" ]; then # user is included in the target
user="${target%%@*}" # Get user
target="${target#*@}" # Strip out user
fi

# Get hostname and path
if [ "${target::1}" = "[" ]; then # Syntax: [user@]\[hostname\]:path
remote="${target%\]*}]"
path="${target#*\]}"
path="${path:1}"
else # Syntax: [user@]hostname:path
remote="${target%%:*}"
path="${target#*:}"
fi

# Construct target argument for ssh
if [ ! "${usertest/@/}" = "${usertest}" ]; then # user is included in the target
remote="${user}@${remote}" # Reattach user
fi

path="${path/\'/\'\\\'\'}"
exec ssh -t "${options[@]}" "${remote}" "cd '${path}'; exec \$SHELL -l"

0 comments on commit f941ae5

Please sign in to comment.