【问题标题】:What does 'bash -c' do?'bash -c' 有什么作用?
【发布时间】:2013-12-31 13:48:28
【问题描述】:

我按照以下教程进行操作:http://davidtsadler.com/archives/2012/06/03/how-to-install-magento-on-ubuntu/

在某些时候它告诉我执行以下命令:

sudo bash -c "cat >> /etc/apache2/sites-available/magento-store.com <<EOF
<VirtualHost *:80>

  ServerName  localhost.magento-store.com
  ServerAlias www.localhost.magento-store.com

  DocumentRoot /home/dev/public_html/magento-store.com/public

  LogLevel warn
  ErrorLog  /home/dev/public_html/magento-store.com/log/error.log
  CustomLog /home/dev/public_html/magento-store.com/log/access.log combined

</VirtualHost>
EOF"

这个命令做了什么,我怎样才能取消它?

我重新启动了计算机,它似乎仍在运行。我查看了.bashrc.profile,但我没有在里面找到。

【问题讨论】:

标签: linux bash


【解决方案1】:

引用man bash:

-c 字符串 如果存在 -c 选项,则读取命令 来自字符串。
如果字符串后面有参数,则将它们分配给位置参数,从 $0 开始。

你引用的命令会将heredoc中的文本(即VirtualHost标签中的文本)追加到文件/etc/apache2/sites-available/magento-store.com中。

【讨论】:

  • 没有-c 有什么不同?
  • @tjt263 实际上,在这种情况下不使用 -c 会给您带来数小时的调试时间,试图以各种方式转义各种引号,但仍然无法正常工作。
  • 还要注意,从bash -c 调用的命令将被强制为单线程。例如:"bash -c 'ffmpeg -threads 4 -i input.mov output.mp4'" 将调用 ffmpeg,但它只能使用一个内核,即使您指定了“线程”参数
  • @Christian 为什么?这似乎很不合理。
  • @Christian 此行为未记录在案,也与实际行为不符:i.imgur.com/ZeFAkDf.png
【解决方案2】:

The manual page for Bash(例如man bash)表示-c选项从字符串执行命令;即引号内的所有内容。

【讨论】:

    【解决方案3】:

    查看您机器上或 Internet 上的手册页,例如 this one

    引用:

    -c string
         If the -c option is present, then commands are read from string.
         If there are arguments after the string, they are assigned to the positional
         parameters, starting with $0.
    

    【讨论】:

      猜你喜欢
      • 2016-09-10
      • 2014-09-07
      • 1970-01-01
      • 2011-06-02
      • 2018-11-19
      • 2014-01-18
      • 2011-10-24
      • 2015-06-27
      • 2020-05-20
      相关资源
      最近更新 更多