0

I am trying to run below shell script but i am getting syntax error.

script.sh env1 ManagedSvr1 line 29: warning: here-document at line 6 delimited by end-of-file (wanted `EOF') line 30: syntax error: unexpected end of file

#!/bin/bash
case "$1" in "env1")
ssh  weblogic@hostname1 << EOF
    case "$server" in
    "ManagedSvr1")
            tailf /app/Oracle/Middleware/domains/dq/servers/ManagedSvr1/logs/ManagedSvr1.log
    ;;
    "ManagedSvr2")
            tailf /app/Oracle/Middleware/domains/dq/servers/ManagedSvr2/logs/ManagedSvr2.log
    ;;
    esac
;;
"env2")
    ssh  weblogic@hostname2 << EOF
    case "$server" in
    "ManagedSvr1")
            tailf /app/Oracle/Middleware/domains/dq/servers/ManagedSvr1/logs/ManagedSvr1.log
    ;;
    "ManagedSvr2")
            tailf /app/Oracle/Middleware/domains/dq/servers/ManagedSvr2/logs/ManagedSvr2.log
    ;;
    esac
;;
esac
3
  • 2
    As far as I can see you didn't terminate your HERE doc. I'll try to reproduce, but after your esac you need to terminate EOF before ;; Commented Jan 16, 2019 at 13:58
  • 2
    If you're running the same ssh / tailf commands in each case, why do you duplicate them, instead of just set a variable inside the case statements, and use that variable unconditionally later? Also removes the need for a heredoc entirely. Commented Jan 16, 2019 at 14:14
  • @CharlesDuffy's suggestion is the same as my answer below Commented Jan 16, 2019 at 20:17

2 Answers 2

2

Your Here Docs specify EOF to end them (<< EOF) but you never have an EOF to end them. Note that EOF doesn't mean End of File, it means the string 'EOF'. https://en.wikipedia.org/wiki/Here_document has examples.

I'm not sure what you're hoping to accomplish, but it looks to me that you need to specify which file to tail. Are you hoping to pass the inner case into the remote shell on the server you're sshing into? It would simplify your code to set your filename and servername first and then ssh and execute the command. Actually, I don't see much purpose in your inner case statements anyway. Instead of wrapping everything in the 'env' case, you could just set the hostname to a variable. And then the "servername" can just be interpolated into the filesystem path. something like this seems like a simple approach:

#!/bin/bash
case "$1" in
env1)
  hostname="hostname1"
;;
env2)
  hostname="hostname2"
;;
esac
echo ssh weblogic@$hostname tailf  /app/Oracle/Middleware/domains/dq/servers/$servername/logs/$servername.log

Seems to work simply and straight forward:

servername=ManagedSvr1 ./t.sh env1
ssh weblogic@hostname1 tailf /app/Oracle/Middleware/domains/dq/servers/ManagedSvr1/logs/ManagedSvr1.log

Take out the "echo" to actually execute the ssh.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Dan for your reply. The shell script is for tailing logs of more than 2 remote server and i am passing env & managedserver name as a parameter
1

You actually don't need "HERE" docs, per say, you're using one case statement which is taking standard input to your SSH command. take single quotes as input to ssh This lets you do multiline input in peace. Quoting your "EOF" and terminating it, is also acceptable usage.

#!/bin/bash

case "$1" in

"env1")

        ssh -tt weblogic@hostname1 <<< '
        case "$server" in
        "ManagedSvr1")
                cat /var/log/syslog
        ;;
        "ManagedSvr2")
                tailf /app/Oracle/Middleware/domains/dq/servers/ManagedSvr2/logs/ManagedSvr2.log
        ;;
        esac '
        ;;
"env2")
        ssh -tt weblogic@hostname2 <<< '
        case "$server" in
        "ManagedSvr1")
                tailf /app/Oracle/Middleware/domains/dq/servers/ManagedSvr1/logs/ManagedSvr1.log
        ;;
        "ManagedSvr2")
                tailf /app/Oracle/Middleware/domains/dq/servers/ManagedSvr2/logs/ManagedSvr2.log
        ;;
        esac '
        ;;
esac

This would be an easier way to manage the script. Especially as you're not utilising remote user change: Lets you set all your variables locally, and then just connect and execute the single command.

#!/bin/bash

server="$2"
case "$1" in

"env1")
        hostname="hostname1"
        case "$server" in
        "ManagedSvr1")
                remote_command=$(tailf /app/Oracle/Middleware/domains/dq/servers/ManagedSvr2/logs/ManagedSvr1.log)
        ;;
        "ManagedSvr2")
                remote_command=$(tailf /app/Oracle/Middleware/domains/dq/servers/ManagedSvr2/logs/ManagedSvr2.log)
        ;;
        esac
        ;;
"env2")
        hostname="hostname2"
        case "$server" in
        "ManagedSvr1")
                remote_command=$(tailf /app/Oracle/Middleware/domains/dq/servers/ManagedSvr1/logs/ManagedSvr1.log)
        ;;
        "ManagedSvr2")
                remote_command=$(tailf /app/Oracle/Middleware/domains/dq/servers/ManagedSvr2/logs/ManagedSvr2.log)
        ;;
        esac
        ;;
*)
        exit 1
        ;;
esac

if [[ $? == 0 ]]
then ssh weblogic@$hostname $remote_command
fi

8 Comments

doesn't that assume that $server is defined by default in the environment of the ssh command? I think it's more likely to be defined from the calling script. The single quotes are going to throw that off aren't they?
As per your comments i have taken single quotes but giving error : Pseudo-terminal will not be allocated because stdin is not a terminal.
@DanFarrell As OP hasn't mentioned it one can never assume. the afaik ${server} is not defined by default on any system, so it would have to be an exported variable on the destination system (maybe?) or perhaps he's doing a prior server=$(hostname -s) but skipped for brevity?
Try ssh -t -t (or ssh -tt for short) to force pseudo-tty allocation even if stdin isn't a terminal.
My mistake .. i have added server=$2 export server; and passing server as a parameter.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.