Shell脚本中的嵌套Case语句取得语法错误

时间:2019-01-16 13:54:49

标签: linux bash shell scripting

我正在尝试在shell脚本下运行,但是出现语法错误。

  

script.sh env1 ManagedSvr1    第29行:警告:第6行的此处文档以文件结尾分隔(需要使用“ EOF”)    第30行:语法错误:u 文件结尾意外

#!/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

2 个答案:

答案 0 :(得分:2)

您的此处文档指定EOF来结束它们(import com.auth0.jwt.JWT; import com.auth0.jwt.algorithms.Algorithm; import lombok.SneakyThrows; import java.io.FileInputStream; import java.security.KeyStore; import java.security.interfaces.ECPrivateKey; import java.util.Date; KeyStore store = KeyStore.getInstance("PKCS12"); // store.load(new FileInputStream("/home/andrew/keystore.p12"), "test".toCharArray()); ECPrivateKey pk = (ECPrivateKey) store.getKey("selfsigned", "test".toCharArray()); Algorithm algorithmHS = Algorithm.ECDSA256(null, pk); String token = JWT.create() .withSubject("") .withExpiresAt(new Date()) .withNotBefore(new Date()) .withIssuer("issuer") .withClaim("roles", "b") .withClaim("keys", "b") .sign(algorithmHS); ),但是您永远都没有EOF来结束它们。请注意,EOF并不表示文件尾,而是字符串'EOF'。 https://en.wikipedia.org/wiki/Here_document包含示例。

我不确定您希望完成什么,但是在我看来,您需要指定要尾随哪个文件。您是否希望将内壳传递到要插入的服务器上的远程外壳中?这样可以简化代码,先设置文件名和服务器名,然后再设置ssh并执行命令。实际上,无论如何,我在您的内部案例陈述中看不出任何目的。不必将所有内容都包装在“ env”中,您只需将主机名设置为变量。然后可以将“服务器名”插入到文件系统路径中。像这样的事情似乎很简单:

<< EOF

似乎简单而直接地工作:

#!/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

取出“ echo”以实际执行ssh。

答案 1 :(得分:1)

实际上,您实际上不需要"HERE"文档,您使用的是一个case语句,它将标准输入输入到SSH命令中。将单引号用作ssh的输入。这样您就可以安心地进行多行输入。引用"EOF"并终止它也是可以接受的用法。

#!/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

这将是一种更容易管理脚本的方法。尤其是当您不使用远程用户更改时:可以在本地设置所有变量,然后只需连接并执行单个命令即可。

#!/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