无法在脚本

时间:2015-07-27 22:14:36

标签: bash for-loop output unzip

我编写了一个脚本,从拉链中解压缩证书并针对我们的某个服务器测试证书:

#!/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

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

3 个答案:

答案 0 :(得分:7)

/dev/null行为真的很奇怪。最好只使用unzip的{​​{1}}(安静)选项。

答案 1 :(得分:2)

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

解压缩users.zip -d users

答案 2 :(得分:1)

我想通了,感觉就像傻瓜一样。

我第一次拨打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

它按预期工作。

相关问题