Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2004
    Posts
    2

    Unanswered: newbie needs help with script

    Hi guys this is my first foray into shell scripting. Thought I would try something easy but also something I could actually use, so I tried to make a remote session launcher. Having trouble processing the command-line arguments it seems. If I pass no arguments it works but if I pass an argument it always tries the first one in the list. This may make some of you cringe, but here's my code below:

    #!/bin/sh
    # remote session launcher
    # 2/16/04



    if [ -z $1 ]
    then
    echo "Please enter a hostname followed by the command 'go'."
    echo "Valid hostnames: " # add hosts here in addition to below
    echo "foo: mattelmore.com"
    echo "taki: shell.takiweb.net"
    echo
    exit 0
    else
    fi

    if [ '$1 = foo' ]
    then
    echo "Connecting to server $1 ..."
    ssh -p 5224 -l matt 204.214.213.11
    exit 0
    else
    fi

    if [ '$1 = taki' ]
    then
    echo "Connecting to server $1 ..."
    ssh -l mattelmore shell.takiweb.com
    exit 0
    else
    fi
    There's more but I snipped some of it out since it's redundant. Problem is this: I type ./go foo it tries to connect to the foo server from the first if/fi loop. If I type ./go taki it tries to connect to foo.

    Appreciate any input!

  2. #2
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    Hi MattElmore

    The syntax of your tests is invalid
    Code:
    if [ "$1" = "foo" ]
    then
       echo "Connecting to server $1 ..."
       ssh -p 5224 -l matt 204.214.213.11
       exit 0
    fi
    You can remove the empty 'else' statements.

    I think that using 'case' is a better way to do what you want.
    For example:
    Code:
    server=$1
    case "$1" in
      foo)  cmde='ssh -p 5224 -l matt 204.214.213.11' ;;
      taki) cmde='ssh -l mattelmore shell.takiweb.com' ;;
      *)     echo 'Unknown hostname : $server" >&2
              exit 1
              ;;
    esac
    echo "Connecting to server $1..."
    $cmde
    exit $?
    Jean-Pierre.

  3. #3
    Join Date
    Feb 2004
    Posts
    2
    That works beautifully, Jean. Thank you very much

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •