zoukankan      html  css  js  c++  java
  • Shell中获取命令输出的一个坑

    在写一个Shell脚本的时候,遇到一个奇怪的问题,如下语句:

    desc=$(grep desc main.cpp)
    

    正常情况下,应该是把grep的输出的内容存储到desc变量里,然而结果竟然把当前目录下所有文件的文件名都存进去了。

    折腾了很久,终于在grep的文件中找到了猫腻,见main.cpp:

    /*
     * desc: bilibala bilibala...
     */
    
    int main()
    {
        return 0;
    }
    

    这样grep desc main.cpp的结果便是:* desc: bilibala bilibala...

    注意,前面有一个*号,这就是问题所在,当使用$(...)的方法获取命令的输出时,会自动把*号扩展成当前目录下的文件名(为什么呢?我没有细究)。这就肯定出问题了,解决的办法就是把注释改成双斜杠的,避免出现*号(懒得找其他方式了,能让我的脚本work就好)。

  • 相关阅读:
    杭电 1013 Digital Roots
    杭电 1040 As Easy As A+B 【排序】
    杭电 2092 整数解
    bzoj3223
    bzoj3224
    LA3905
    bzoj3601
    bzoj1002
    bzoj3105
    bzoj3332
  • 原文地址:https://www.cnblogs.com/demon90s/p/7737099.html
Copyright © 2011-2022 走看看