回声:参数列表过长 [英] Echo: Argument List too long

查看:152
本文介绍了回声:参数列表过长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,我的回声参数列表太长,想要一些有关如何解决此问题的想法,或者至少要测试一下条件以便我能够正确处理它,否则它将无法解决.杀死我的脚本

I'm running into an issue where my argument list for echo is too long and would like some ideas on how to get around this issue, or at least test for the condition so I can properly handle it, and it won't kill my script

for file in `cat filelist`; do 
    PROTOCOLS1=`egrep -i 'rsh|rsync|remsh' "$file" | egrep -v '^[ |      ]*#'`
    FIELDS=`echo $PROTOCOLS1|wc -l`
    if [[ $FIELDS -gt 1024 ]]; then
        echo $file >> $debuglog
    else
        set -A myarray $PROTOCOLS1
        do stuff.....
    fi
done

所以问题是当我的echo的arg列表太长时,$FIELDS设置为null,因此我对$FIELDS -gt 1024的测试始终为true并且不会被捕获. 问题在于,当它转到数组时,显然太大了,并且出现下标超出范围错误,并且我的脚本退出了.

So the problem is that when my arg list for echo is too long, $FIELDS is set to null, and thus my test for $FIELDS -gt 1024 always is true and does not get caught. Problem is when it goes to the array it's obviously too big and I get a subscript out of range error and my script exits.

任何想法都将不胜感激.

Any ideas are greatly appreciated.

编辑9/18

好的,所以这个问题更基本了.

OK so the problem is a little more basic.

myserver-v1> echo $variable
myserver-v1> /usr/bin/echo: too many args

我想在我的脚本中对此进行测试

I want to test for this in my script

我尝试了以下方法,但效果很好,但我把所有这些废话都填到了stdout上,这填满了我的调试日志,很烦人

I tried the following, which works, but I get all this crap to stdout, which fills up my debug log and is annoying

echo $variable
if [[ $? -ne 0 ]]; then
write to error log
fi

有没有一种方法可以测试echo $ variable ....而无需将其发送到stdout? 我尝试了以下方法,但似乎都没有用,所以我有点不知所措.

Is there a way to test echo $variable....without sending it to stdout? I tried the following, but neither seemed to work, so I am kind of at a loss here.

[[ ! `echo $variable ]]
[[ `echo $variable ]]

推荐答案

如果将未加引号的变量$PROTOCOLS1保留在echo中,则可以通过替换以下内容来简化工作:

If you keep the unquoted variable $PROTOCOLS1 in the echo, you could simplify life by replacing:

FIELDS=`echo $PROTOCOLS1|wc -l`

使用

FIELDS=1

这是因为当您回显$PROTOCOLS1时,如果没有任何引号,您将只有一行(可能很长)的输出.或者,您可以使用:

This is because when you echo $PROTOCOLS1 without any quotes around it, you will only have one (possibly very long) line of output. Alternatively, you can use:

FIELDS=$(echo "$PROTOCOLS1" | wc -l)

其中双引号将换行符保留在PROTOCOLS1的值中(但它会使您回到参数列表过长"的问题).

where the double quotes will preserve the newlines in the value of PROTOCOLS1 (but it gets you back to the 'argument list too long' problem).

因此,您需要考虑使用:

So, you need to think about using:

FIELDS=$(egrep -i 'rsh|rsync|remsh' "$file" | egrep -c -v '^[ |      ]*#')

获得第二个egrep为您进行行计数.显然,由于脚本的后半部分使用$PROTOCOLS1,因此您将需要重新评估egreps以获得数据,但是您应该考虑您的处理方案是否合适.如果遇到的字符串值太长,则可能不是以最佳方式进行处理.替代方案是什么,取决于您要尝试执行的操作,而问题并没有揭示出这一点.使用诸如Perl,Python或Ruby之类的脚本语言进行额外的处理可能是适当的.

which gets the second egrep to do the line counting for you. Obviously, since the later portion of the script uses $PROTOCOLS1, you will need to re-evaluate the egreps to get the data, but you should think about whether your processing scheme is appropriate. If you are running into a string value that is too long, you are probably not doing the processing in the best way. What the alternatives are depends on what you are trying to do, and the question does not reveal that. It might be appropriate to do the extra processing with a scripting language such as Perl, Python or Ruby.

这篇关于回声:参数列表过长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆