LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-20-2006, 05:04 AM   #1
noir911
Member
 
Registered: Apr 2004
Location: Baltimore, MD
Posts: 681

Rep: Reputation: Disabled
Bash if statement


I have a program which SSH me to different servers and server name is taken as a command line argument ($ARG1). All servers are in port 22 but only 1 in a different port. I want to make the code such that if I type that server name it will connect me that server and for everything else it will connect me to port 22.

But it is not working since I added the function for the different port server in the if-statement.

Here's the code

Code:
server23()
{
    ssh -p 600 me@server
}

if [ -f $PASSWD_FILE ]; then
   if [ $1 = server23 ]; then
      server23
else
grep $1 $PASSWD_FILE
ssh username@$1
     fi 
fi

Last edited by noir911; 08-20-2006 at 05:06 AM.
 
Old 08-20-2006, 05:40 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

Too bad you don't tell us what is going wrong. Looking at the code I see some things that are incorrect or could be improved.

This should work, using your original idea:
Code:
#!/bin/bash

server23()
{
  ssh -p 23 username@$SERVER
}

SERVER="$1"

if [[ -f $PASSWD_FILE ]]; then
  if [[ $SERVER == "server23" ]]; then
    server23
  else
    grep $SERVER $PASSWD_FILE
    ssh username@$SERVER
  fi 
fi
But the above can be done a lot simpler:
Code:
#!/bin/bash

SERVER="$1"
PORT="22"
[[ ${SERVER} == "server23" ]] && PORT="23"
ssh -p ${PORT} username@${SERVER}
Hope this helps.
 
Old 08-20-2006, 05:43 AM   #3
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
You could have a variable called "port" with a default value of 22. Then test if this is the server with a different port. If so, assign a different number to the port and then use the line "ssh -p $port me@server".
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH IF statement kinetik Programming 10 05-07-2006 02:48 AM
Bash: Print usage statement & exit; otherwise continue using Bash shorthand operators stefanlasiewski Programming 9 02-07-2006 05:20 PM
bash statement os2 Programming 2 03-20-2005 10:13 PM
bash if statement question xscousr Programming 3 09-02-2003 11:58 AM
bash for statement with 2 arrays? Noerr Linux - General 10 05-27-2002 12:58 PM


All times are GMT -5. The time now is 05:24 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration