简体   繁体   English

[:关于Cygwin Shell脚本的参数太多了

[英][: too many arguments on Cygwin Shell script

I inherited some shell scripts written to do automated performance testing without having any shell scripting knowledge. 我继承了一些编写的shell脚本来进行自动化性能测试,而没有任何shell脚本知识。 I skimmed through a few guides and played around with the language until I could do my project. 我浏览了几个指南并玩弄了这个语言,直到我可以完成我的项目。 It mostly went well, but I'm stuck on something I absolutely have not been able to find an answer to anywhere else. 它大部分进展顺利,但我坚持一些我无法在其他地方找到答案的东西。

Part of the old code ssh's into a server used in the testing with a script as an argument. 部分旧代码ssh进入测试中使用的服务器,脚本作为参数。 I need this script do do different things on Windows and Linux. 我需要这个脚本在Windows和Linux上做不同的事情。 My best solution so far is to grep for Cygwin in uname -a's output, and if found, do the Windows code. 到目前为止,我最好的解决方案是在uname -a的输出中使用Cygwin,如果找到,则执行Windows代码。

Unfortunately the script is going belly up both when passed via ssh, and when executed through Cygwin on the Windows system. 不幸的是,当通过ssh传递脚本时,以及在Windows系统上通过Cygwin执行脚本时,脚本都会出现问题。

#!/bin/bash
if [ `uname -a | grep "Cygwin"` ]; then
    echo "ARRGG WINDOWS!"
else
    echo "Some Linux command."
fi

yields: 收益率:

./test.sh: line 2: [: too many arguments ./test.sh:第2行:[:参数太多了

and then branches to the not-windows logic. 然后分支到not-windows逻辑。 The same thing appears to run fine on any Linux distribution I've tried. 在我试过的任何Linux发行版上,同样的东西似乎运行正常。

I'm left with three questions. 我有三个问题。 Why am I getting this exception, what are some good ways for me to get answers to hyper-specific questions like this (intersection between shell scripting, Cygwin, ssh, and one error) besides Googling, and is there a better, more canonical way to test for platform? 为什么我会遇到这个异常,除了谷歌搜索之外,有什么方法让我能够获得像这样的超特定问题(shell脚本,Cygwin,ssh和一个错误之间的交集)的答案,并且有更好的,更规范的方式测试平台?

Thanks for your time, and I'll be happy to provide any additional info that will help. 感谢您的时间,我很乐意提供任何有用的信息。

The troublesome clause was copied verbatim, the rest was made up for SO. 麻烦的条款是逐字复制的,剩下的就是SO。

I tried your command, it executed without error on bash 4.2.37. 我尝试了你的命令,它在bash 4.2.37上没有错误地执行。

It will also work without the square brackets or back-ticks -- if tests the return code of the next command... you can use the test command, which can also be spelled [ , but it will also work with the return code of grep or even : which is always true. 它也可以在没有方括号或后退的情况下工作 - if测试下一个命令的返回代码...你可以使用test命令,也可以拼写[ ,但它也可以使用返回代码grep或者甚至:这总是如此。

#!/bin/bash
if uname -a | grep Cygwin > /dev/null; then
     echo "ARRGG WINDOWS!"
else
     echo "Some Linux command."
fi

Note, there is also a bash environment variable $OSTYPE which will be linux-gnu under Linux. 注意,还有一个bash环境变量$OSTYPE ,它在Linux下是linux-gnu

There's an in-depth discussion here: Detect the OS from a Bash script 这里有一个深入的讨论: 从Bash脚本中检测操作系统

The output for uname -a on Cygwin will be many words including Cygwin . Cygwin上uname -a的输出将是很多单词,包括Cygwin Each word will be an argument to test (aka [ ). 每个单词都是test的参数(又名[ ]。 Hence the complaint. 因此投诉。 You probably want: 你可能想要:

case "$(uname -a)" in
(*Cygwin*) echo "Oh bother!";;
(*)        echo "Phew!";;
esac

or: 要么:

if [ -n "$(uname -a | grep Cygwin)" ]
then echo "Oh bother!"
else echo "Phew!"
fi

or: 要么:

if [ $(uname) = Cygwin ]
then echo "Oh bother!"
else echo "Phew!"
fi

This gets either Cygwin or Linux — or Darwin on Mac OS X, and equivalent names on AIX, Solaris, HP-UX — and avoids the multiple arguments issue and only runs one command instead of two. 这可以获得Cygwin或Linux - 或Mac OS X上的Darwin,以及AIX,Solaris,HP-UX上的等效名称 - 并避免多参数问题,只运行一个命令而不是两个命令。

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

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