【问题标题】:Shell Scripts with conditional echos带有条件回显的 Shell 脚本
【发布时间】:2011-12-06 21:21:33
【问题描述】:

我如何排除我的脚本正在执行的操作而只打印 echo 的内容?

例如,我正在对一个目录进行 taring,我不希望它 tar 的每个文件都回显。只有回显命令。

#! /bin/bash 

clear

echo "Compressing the files"

cd ~/LegendaryXeo/html/
tar --exclude=".git" -cvf site.tar *
mv site.tar ~/LegendaryXeo/work/
cd ~/LegendaryXeo/work/

clear

echo "Extracting the site"

tar -xvf site.tar 

echo "Deleting Tar"

cd ~/LegendaryXeo/work/

rm -f site.tar

clear

echo "Copying files to server"

scp -r ~/LegendaryXeo/work/* user@site.com:~/../../domains/

【问题讨论】:

  • 如果你不想 tar 发出输出,你为什么要传递它 -v?
  • 我不明白的是:为什么要使用三个步骤? tar -C ~/LegendaryXeo/html/ --exclude=".git" -cf - . | tar -C ~/LegendaryXeo/work/ -xf - && scp -r ~/LegendaryXeo/work/* user@site:~/../../domains/。 tar 部分将在没有 tmep tar 文件的情况下使用。如果您不需要工作目录的内容,您应该使用 ssh 一步完成此操作,例如 ``tar -C ~/LegendaryXeo/html/ --exclude=".git" -cf - 。 | ssh user@site "tar -C ~/../../domains/` -xf -"

标签: bash shell terminal


【解决方案1】:

tar的输出重定向到/dev/null

tar [your options] [files] &> /dev/null

脚本中的任何echo 命令仍将输出到屏幕。

【讨论】:

  • 我相信这正是我想要的
  • 我也是 ;) 现在我只需要再获得 18 票即可获得逆转徽章。 ;)
  • 由于某种原因,Tar 进程仍然回响...这是设计使然吗?
  • 您还应该将 stderr 重定向到 /dev/null
猜你喜欢
  • 1970-01-01
  • 2010-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-07
  • 2014-06-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多