summaryrefslogtreecommitdiff
path: root/xsh
blob: 3fa48e950e45a60e6f76c1976da89f408926e60e (plain)
  1. #!/bin/bash
  2. SCRIPT=$(basename $0)
  3. usage() {
  4. echo "Usage: $SCRIPT [--x] [--verbose] [su|sudo] [[{user}]@{host}] [[--] command...]"
  5. }
  6. termwrapper() {
  7. PRG=$@
  8. if [ "$V" = 1 ]; then
  9. echo $PRG
  10. sleep 4
  11. fi
  12. if [ -n "$PRG" ]; then
  13. x-terminal-emulator -e $PRG &
  14. else
  15. x-terminal-emulator &
  16. fi
  17. }
  18. # Initial setup
  19. SU=''
  20. SUDO=''
  21. X=''
  22. V=''
  23. HOST=''
  24. ssh_opts="-t"
  25. while [ $# -gt 0 ]; do
  26. case $1 in
  27. su) SU=1; SUDO=;;
  28. sudo) SUDO=1; SU=;;
  29. --x|-x|x) X=1; ssh_opts="$ssh_opts -X";;
  30. --verbose|-v) V=1; ssh_opts="$ssh_opts -v";;
  31. --port|-p) ssh_opts="$ssh_opts -p $2"; shift;;
  32. --) shift; break;;
  33. -*) usage; exit 1;;
  34. @*) HOST=${1#@};;
  35. *@*) HOST=$1;;
  36. *) break;;
  37. esac
  38. shift
  39. done
  40. [ $# -gt 0 ] || set -- /bin/bash
  41. # Split program name from its options
  42. if [ $# -gt 0 ]; then
  43. prg_base=$1
  44. shift
  45. prg_opts=$@
  46. fi
  47. # Pause for a moment if verbose
  48. if [ "$V" = 1 ]; then
  49. prg_opts="$prg_opts; sleep 4"
  50. fi
  51. prg="$prg_base $prg_opts"
  52. prg_su="echo -n 'Changing to root...: '; su -c \"cd; $prg\""
  53. prg_sudo="sudo -i -- $prg"
  54. case "$HOST" in
  55. '')
  56. if [ -n "$SU" ]; then
  57. termwrapper $prg_su
  58. elif [ -n "$SUDO" ]; then
  59. termwrapper $prg_sudo
  60. else
  61. termwrapper $prg
  62. fi
  63. ;;
  64. *)
  65. if [ -n "$SU" ]; then
  66. termwrapper ssh $ssh_opts $HOST $prg_su
  67. elif [ -n "$SUDO" ]; then
  68. termwrapper ssh $ssh_opts $HOST $prg_sudo
  69. else
  70. termwrapper ssh $ssh_opts $HOST $prg
  71. fi
  72. ;;
  73. esac