0

I have the following in a bash script with Wwhich I want to login to to host and execute a command as directed:

#!/bin/bash
echo $1;
if [ -n ${2+x} ] ; then
    if [ $2 != "log" -a $2 != "cat" ] ; then
        echo "$0 $1 : second argument must be empty, "tail" or "cat""
        exit 1
    fi
elif [ -z ${2+x} ] ; then

    ssh -o StrictHostKeyChecking=no -p 2222 root@$1; echo "Reconnect?"; while read < /dev/tty; do ssh -o StrictHostKeyChecking=no -p 2222 root@$1; echo "Reconnect?"; done

elif [ $2 == "tail" ] ; then

    ssh -o StrictHostKeyChecking=no -p 2222 root@$1 tail -f /var/log/dp.log; echo "Reconnect?"; while read < /dev/tty; do ssh -o StrictHostKeyChecking=no -p 2222 root@$1 tail -f /var/log/dp.log; echo "Reconnect?"; done

elif [ $2 == "cat" ] ; then
    ssh -o StrictHostKeyChecking=no -p 2222 root@$1 cat /var/log/dp.log; echo "Reconnect?"; while read < /dev/tty; do ssh -o StrictHostKeyChecking=no -p 2222 root@$1 cat /var/log/dp.log; echo "Reconnect?"; done

fi

and turns out that if I do not provide $2, it still complains about line 4: [: too many arguments why is this, I'm wondering?

  • 2
    1. double-quote your variables AND your positional parameters. 2. is ${2+x} correct, or did you mean ${2:+x}? neither of them make much sense in this context . 3. you might want to use a case statement rather than if/elif/elif/elif/fi. 4. and put the repetitive parts of the ssh commands into a function so you don't have to repeat the same code. – cas Feb 22 '18 at 3:08
1

Personally, I think the entire idea of repeatedly asking "Reconnect?" is an abomination that, at best, pointlessly duplicates your shell's command history but much, much crappier. Seriously, how hard is to to just run ssh root@remotehost tail -f /var/log/dp.log and then hit Up-ArrowEnter if you want to run it again? or just write a couple of shell functions or tiny scripts if you don't want to type the whole command even once (see below for examples).

That said, here's a better way to do what you're trying to do:

#!/bin/bash

error() {
  local ec msg

  ec="$1" ; shift
  msg="$*"

  [ -n "$msg" ] && echo "$msg" >&2
  [ "$ec" != 0 ] && exit "$ec"
}

[ -z "$1" ] && error 1 "first argument missing. must be hostname"
echo "$1"

myssh() {
  local remote reconnect quit

  remote="$1" ; shift
  quit=0

  while [ $quit != 1 ] ; do
    ssh -o StrictHostKeyChecking=no -p 2222 "$remote" "$@"

    read -p "Reconnect (Y/n) [Y]? " reconnect
    [[ $reconnect =~ ^[Nn] ]] && quit=1
  done
}

case "$2" in
   cat|log) myssh "root@$1" cat /var/log/dp.log ;;
      tail) myssh "root@$1" tail -f /var/log/dp.log ;;
        "") myssh "root@$1" ;;
         *) error 1 "Unknown second argument '$2'" ;;
esac

Here's an even better way of doing it.

Add ~/bin to your $PATH and then create the following scripts (don't forget to make them executable with chmod +x)

~/bin/myssh.sh:

#!/bin/sh
[ -z "$1" ] && echo hostname is required && exit 1
remote="$1" ; shift
ssh -o StrictHostKeyChecking=no -p 2222 "root@$remote" "$@"

~/bin/remote-cat.sh:

#!/bin/sh
myssh.sh "$1" cat /var/log/dp.log

~/bin/remote-tail.sh:

#!/bin/sh
myssh.sh "$1" tail -f /var/log/dp.log

Run these as you need them. Use your shell's command-history to run them again as often as you want.

myssh.sh can be used both to get a root login shell on the remote host AND (as demonstrated by the other two scripts) to run any arbitrary commands as root on the remote host.

0

If you mask $2 in both cases and in the later ones, you prevent the $2 being nothing when empty.

#!/bin/bash
echo $1;
if [ -n ${2+x} ] ; then
    if [ "$2" != "log" -a "$2" != "cat" ] ; then
        echo "$0 $1 : second argument must be empty, "tail" or "cat""
        exit 1
    fi

(with German locale):

> echo $([ != "log" -a != "cat" ])
> bash: [: Zu viele Argumente.

compare:

> echo $([ "" != "log" -a "" != "cat" ])
>

A second problem might be, that you first check for log and cat, but tell the user, you're checking for tail and cat.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.