0

I'm creating a bash script that does checkouts for me using whiptail. I've got it all working but I'd like to check if i can reach svn before showing a menu etc.

Currently I'm going to do svn ls svn://host and check if the responce is equals svn: Unknown hostname 'host'. if so exit / show msg.

Is there a better way to do this using shell commands?

Grtz Michael

2 Answers 2

4

Your solution seems to be good enough, because if you're going to do checkout, it's guaranteed that svn is installed on the system. So there are no additional requirements only to check if the remote port is open.

However, another possible solution would be to use netcat which is installed on most UNIX-like systems:

#! /bin/sh

TIMEOUT=2 # in seconds
HOST=$1
PORT=$2

$(nc -z -w ${TIMEOUT} ${HOST} ${PORT}) || \
    exit 1 # Port is closed

# Port is open, continue processing

The benefit from the netcat-based script is that the timeout value prevents us from waiting a lot of time on ports protected with DROP firewall rules.

3
  • my solution works at the moment but yours looks more promising. Mine feels kinda iffy using substring & string compare. I'll give it a go.
    – Michael
    Sep 18, 2011 at 12:08
  • Its working now. I did it like this:
    – Michael
    Sep 18, 2011 at 12:25
  • nc -z -w ${timeout} ${host} ${port} 2> $tmp > /dev/null; check=$(<$tmp) if [ "$check" != "" ]; then whiptail --title "Error" \ --msgbox "Svn host [$host] on port [$port] is not reachable" 8 78 exit fi
    – Michael
    Sep 18, 2011 at 12:25
1

You shouldn't need to check the command output.. just the status should be sufficient.

#!/bin/sh

svn info ${SVN_URL}
if [ $? -eq 1 ]; then
    echo "svn command failed: unable to access ${SVN_URL}";
    exit 1;
fi

#else continue svn server appears to be ok

The advantage of this is it will also fail on a miss typed url.

cheers

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .