简体   繁体   English

无法在shell脚本中使用awk命令进行打印

[英]Unable to print by using awk command in the shell script

#!/bin/bash
echo "Number of hosts entered are "$#
echo "Hostnames are "$@
for i in "$@"
do
    echo "Logging in to the host "$i
    pbsh root@$i '
    ipaddr=`ip r | awk '{print $9}'`
    if [ ipaddr = 172.*.*.* ]
    then
        echo "Script can not be run in this IP series"
        exit
    else
        cd /var/tmp ; wget http://**********
    fi'
done

After executing the above script it is throwing below error. 执行完上述脚本后,它抛出以下错误。 The script is getting execute but not in the desired way. 脚本正在执行,但不是以所需的方式执行。

awk: cmd. awk:cmd。 line:1: {print 行:1:{打印

awk: cmd. awk:cmd。 line:1: ^ unexpected newline or end of string 行:1:^意外的换行符或字符串结尾

I am newbie to the scripting. 我是脚本的新手。 Kindly correct me if anything wrong in the script. 如果脚本中有任何错误,请纠正我。

In the listing which you posted, the opening single quote in the pbsh line is closed by single quote immediately followed by the awk command. 在您发布的清单中, pbsh行中的开头单引号由单引号结束,紧接着是awk命令。 You can escape the latter by prefixing it with a backslash. 您可以通过在反斜杠前面加上反斜杠来逃避后者。

If pbsh also accepts the command to be executed from stdin, an alternative would be to use a HERE document (see the bash man-page, section Here Documents ). 如果pbsh也接受要从stdin执行的命令,则替代方法是使用HERE文档(请参见bash手册页的“ 此处文档”部分)。

UPDATE: Gordon Davisson is right in his comment. 更新:戈登·戴维森在他的评论中是对的。 \\' doesn't work either. \\'也不起作用。 pbsh insists on getting the command to be executed as a single argument, you could either fiddle around with the quotes, as he suggested, or put the whole input for pbsh into a separate file and use, ie, pbsh坚持要求将命令作为单个参数执行,您可以按照他的建议摆弄引号,也可以将pbsh的整个输入放入一个单独的文件中并使用,即

pbsh root@$i $(<input_script.pbsh)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM