【问题标题】:Unable to mute unzip output in script无法在脚本中静音解压缩输出
【发布时间】:2015-10-18 07:07:37
【问题描述】:

我编写了一个脚本,用于从 zip 中解压缩证书并针对我们的一台服务器测试证书:

#!/bin/bash
WORKINGDIR=$(pwd)
if [ ! -f ./users.zip ]; then
    echo "users.zip not found. Exiting."
    exit 1
        else 
            unzip users.zip -d users
            echo "users.zip extracted."
fi
cd ./users/client

echo "Extracting files..."
for file in `ls *.zip`; do 
    unzip -j $file -d `echo $file | cut -d . -f 1` &> /dev/null
done
echo "name,result" > $WORKINGDIR/results.csv
i=0 # Total counter
j=0 # Working counter
k=0 # Failed counter
for D in `ls -d */`; do
        cd "$D"
        SHORT=`find *.p12 | cut -f1 -d "."`
        openssl pkcs12 -in `echo $SHORT".p12"` -passin file:./password -passout pass:testpass -out `echo $SHORT".pem"` &> /dev/null
        echo "Trying: "$SHORT
        ((i++))
        curl --cert ./`echo $SHORT".pem"`:testpass https://example.com -k &> /dev/null
        OUT=$?
        if [ $OUT -eq 0 ];then
                    ((j++)) ; echo -e $(tput setaf 2)"\t"$SHORT": OK $(tput sgr0)" ; echo $SHORT",OK" >> $WORKINGDIR/results.csv
                else
                    ((k++)) ; echo -e $(tput setaf 1)"\t"$SHORT": FAILED $(tput sgr0)" ; echo $SHORT",FAILED" >> $WORKINGDIR/results.csv
        fi
        rm `echo $SHORT".pem"`
        cd ..
done
echo "Test complete:"
echo "Tested: "$i
echo "Working: "$j
echo "Failed: "$k
echo "Results saved to "$WORKINGDIR"/results.csv"
exit 0

当它进入解压缩部分时,我总是得到这个输出:

Archive:  users.zip
   creating: users/keys/
  inflating: users/keys/user1.zip
  inflating: users/keys/user2.zip
  inflating: users/keys/user3.zip
  inflating: users/keys/user4.zip
  inflating: users/keys/user5.zip
  inflating: users/keys/user6.zip
  inflating: users/keys/user7.zip
  inflating: users/keys/user8.zip
  inflating: users/keys/user9.zip
  inflating: users/keys/user10.zip
  inflating: users/keys/user11.zip

我尝试以不同的方式将输出通过管道传输到 /dev/null: &> /dev/null 1>&- 2>&- 2>&1 等等。 没有任何效果。奇怪的是,如果我将脚本的解压缩部分放入单独的脚本文件中:

#!/bin/bash
for file in `ls *.zip`; do
        unzip -j $file -d `echo $file | cut -d . -f 1` &> /dev/null
done

没问题。有没有想过为什么会这样?

【问题讨论】:

    标签: bash for-loop output unzip


    【解决方案1】:

    /dev/null 的行为真的很奇怪。最好只使用unzip-q(安静)选项。

    【讨论】:

      【解决方案2】:

      我想通了,觉得自己很傻。

      我第一次调用unzip时得到了输出:

      unzip users.zip -d users
      

      而不是来自循环:

      for file in `ls *.zip`; do 
          unzip -j $file -d `echo $file | cut -d . -f 1` &> /dev/null
      done
      

      我将-qq添加到第一个unzip

      unzip -qq users.zip -d users
      

      它按预期工作。

      【讨论】:

        【解决方案3】:

        在您发布的脚本中,没有专门针对 users.zip 的重定向。

        解压 users.zip -d users

        【讨论】:

        • 看起来您在我输入自己的答案时已经猜到了。荣誉。
        猜你喜欢
        • 2021-09-15
        • 1970-01-01
        • 1970-01-01
        • 2021-11-20
        • 2011-03-13
        • 2013-07-27
        • 1970-01-01
        • 1970-01-01
        • 2011-01-18
        相关资源
        最近更新 更多