If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > newbie needs help with script

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-16-04, 11:20
MattElmore MattElmore is offline
Registered User
 
Join Date: Feb 2004
Posts: 2
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:

Quote:
#!/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!
Reply With Quote
  #2 (permalink)  
Old 02-16-04, 13:18
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
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.
Reply With Quote
  #3 (permalink)  
Old 02-16-04, 13:27
MattElmore MattElmore is offline
Registered User
 
Join Date: Feb 2004
Posts: 2
That works beautifully, Jean. Thank you very much
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On