LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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
Posts: 682

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: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:55 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration