运行belwo脚本时出现awk语法错误

时间:2019-12-08 12:34:11

标签: shell

下面是代码,运行脚本时出现awk语法错误。

#list of servere where app runs

echo "Servers to be checked :"
cat serverlist
sleep 3
for peer in `cat serverlist`; do
    sleep 3
    echo "Verifying the application environment on node $peer"
    status=$(ssh $peer echo ok 2>/dev/null)
    if [ "$status" == "ok" ];then
        # Check hostname
                ssh -Xt hotcadm@$peer bash -c "
                # Check RAM and SWAP Usages
                free -h | grep -v + > /tmp/ramcache
                printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
                echo -e "Ram Usages :"
                cat /tmp/ramcache | grep -v "Swap"
                printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
                echo -e "Swap Usages :"
                cat /tmp/ramcache | grep -v "Mem"

                # Check Disk Usages
                df -h| grep  'Filesystem\|/apps*' > /tmp/diskusage
                printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
                echo -e "Disk Usages :"
                cat /tmp/diskusage

                # Check System Uptime
                uptime | awk '{print $3,$4}' | cut -f1 -d, > sysuptime
                printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
                echo -e "System Uptime Days :"  `cat sysuptime`
                printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' = "
    fi
done

运行代码后,出现以下错误。不知道我要去哪里错了。已经在互联网上查看过,但是我无法为我遇到的错误找到解决方案。

错误:

Verifying the application environment on node hotvlc218
+---------------------------------------------------------------------+
|                                                                     |
|  Use of this network is restricted to authorized users only. User   |
|  activity may be monitored and/or recorded. Anyone using this       |
|  network expressly consents to such monitoring and/or recording.    |
|                                                                     |
|  BE ADVISED: if possible criminal activity is detected, these       |
|  records, along with certain personal information, may be provided  |
|  to law enforcement officials.                                      |
|                                                                     |
+---------------------------------------------------------------------+
bash: -c: option requires an argument
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
Ram Usages :
             total       used       free     shared    buffers     cached
Mem:           31G        31G       300M       736K       747M        11G
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
Swap Usages :
             total       used       free     shared    buffers     cached
Swap:         2.0G         0B       2.0G
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
Disk Usages :
Filesystem            Size  Used Avail Use% Mounted on
                       59G   49G  7.7G  87% /apps
awk: {print ,}
awk:        ^ syntax error
awk: {print ,}
awk:         ^ syntax error
awk: cmd. line:1: {print ,}
awk: cmd. line:1:          ^ unexpected newline or end of string

2 个答案:

答案 0 :(得分:2)

尝试传递需要复杂引号的任何内容(例如多行shell脚本)作为ssh选项是一个坏主意。您已在脚本中嵌入了引号(“),该引号与用于包围整个脚本的引号之间的交互作用较弱,即使您修复了可能仍会遇到问题的问题。

如果将其传递给stdin上的远程shell,您的生活将变得更加简单:

for peer in `cat serverlist`; do
    sleep 3
    echo "Verifying the application environment on node $peer"
    status=$(ssh $peer echo ok 2>/dev/null)
    if [ "$status" == "ok" ];then
        ssh -Xt hotcadm@$peer bash <<- 'EOF'
        export TERM=xterm
        # Check RAM and SWAP Usages
        free -h | grep -v + > /tmp/ramcache
        printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
        echo -e "Ram Usages :"
        cat /tmp/ramcache | grep -v "Swap"
        printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
        echo -e "Swap Usages :"
        cat /tmp/ramcache | grep -v "Mem"

        # Check Disk Usages
        df -h| grep  'Filesystem\|/apps*' > /tmp/diskusage
        printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
        echo -e "Disk Usages :"
        cat /tmp/diskusage

        # Check System Uptime
        uptime | awk '{print $3,$4}' | cut -f1 -d, > sysuptime
        printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
        echo -e "System Uptime Days :"  `cat sysuptime`
        printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' =
        EOF
    fi
done

我们在这里使用'EOF'是因为该格式禁止本地shell对脚本执行变量扩展。另外,请注意,按照书面要求,使用制表符而不是空格是非常重要的。

答案 1 :(得分:0)

您的脚本似乎存在多个报价不平衡的问题。至少在我在您的脚本上运行shellcheck实用程序时,它会告诉我。这也将与您从awk发出的错误消息以及该错误发生在awk'print'语句中的事实一致。

要探究要纠正哪些引号以使错误消失,建议您执行以下操作:

  • 在计算机上安装shellcheck或访问www.shellcheck.net的在线版本。

  • Weite #!/usr/bin/bash在第一行(或您的外壳碰巧是)。

  • 在下面粘贴您的脚本。然后运行检查。

  • 一键修复Shellcheck的警告消息。每次修复后,请在计算机上测试脚本;看看它的行为是否如您所愿。

这是我能做的最好的事情,而不能在计算机上运行您的脚本。我希望它能有所帮助。