summaryrefslogtreecommitdiff
path: root/xsh
blob: c655c5718f3996d4e80c189b4c366dceb827cc44 (plain)
  1. #!/bin/bash
  2. SCRIPT=$(basename $0)
  3. usage() {
  4. echo "Usage: $SCRIPT [--x] [--verbose] [su|sudo] [[{user}]@{host}] [[--] command...]"
  5. }
  6. # origin: http://stackoverflow.com/a/34515468
  7. quote_args() {
  8. local sq="'"
  9. local dq='"'
  10. local space=""
  11. local arg
  12. for arg; do
  13. echo -n "$space'${arg//$sq/$sq$dq$sq$dq$sq}'"
  14. space=" "
  15. done
  16. }
  17. # Initial setup
  18. SU=''
  19. SUDO=''
  20. X=''
  21. V=''
  22. HOST=''
  23. PORT=''
  24. while [ $# -gt 0 ]; do
  25. case $1 in
  26. su) SU=1; SUDO=;;
  27. sudo) SUDO=1; SU=;;
  28. --x|-x|x) X=1;;
  29. --verbose|-v) V=1;;
  30. --port|-p) PORT="$2"; shift;;
  31. --) shift; break;;
  32. -*) usage; exit 1;;
  33. @*) HOST=${1#@};;
  34. *@*) HOST=$1;;
  35. *) break;;
  36. esac
  37. shift
  38. done
  39. [ $# -gt 0 ] || set -- /bin/bash
  40. [ -z "$SU" ] || set -- su -c "cd; $(quote_args "$@")"
  41. [ -z "$SU" ] || set -- sh -c "echo -n 'Changing to root...: '; $(quote_args "$@")"
  42. [ -z "$SUDO" ] || set -- sudo -i -- "$@"
  43. # Pause for a moment if verbose
  44. [ -z "$V" ] || set -- sh -c "$(quote_args "$@"); sleep 4"
  45. [ -z "$HOST" ] || set -- ssh -t${X:+ -X}${V:+ -v}${PORT:+ -p "$PORT"} $HOST "$(quote_args "$@")"
  46. [ -z "$V" ] || echo "$@"
  47. [ -z "$V" ] || sleep 4
  48. if [ $# -gt 0 ]; then
  49. x-terminal-emulator -e "$@" &
  50. else
  51. x-terminal-emulator &
  52. fi