支撑扩张 - 表达太多

时间:2013-08-01 20:44:55

标签: linux bash unix

我想找到FileName_trojan.sh,FileName_virus.sh,FileName_worm.sh类型的所有文件。如果找到任何此类文件,则显示一条消息。

这里FileName是传递给脚本的参数。

#!/bin/bash
file=$1
if [ -e "$file""_"{trojan,virus,worm}".sh" ]
then
echo 'malware detected'

我尝试使用大括号扩展,但它没有用。我得到错误“太多的论点” 我如何解决它 ?我可以只用OR条件吗?

此外,这不起作用 -

-e "$file""_trojan.sh" -o "$file""_worm.sh" -o "$file""_virus.sh"

2 个答案:

答案 0 :(得分:2)

-e运算符只能使用一个参数;在将参数传递给-e之前扩展了大括号扩展,因此还有两个额外的参数。你可以使用一个循环:

for t in trojan virus worm; do
    if [ -e "{$file}_$t.sh" ]; then
        echo "malware detected"
    fi
do

或在Mark完成输入之前建议:

for f in "${file}_"{trojan,virus,worm}.sh; do
    if [ -e "$f" ]; then
        echo "malware detected"
    fi
done

答案 1 :(得分:1)

问题不在于扩展,而是在努力。问题在于-e测试:它只需要一个参数,而不是三个。

可能的解决方法:

i=0
for f in "$1"_{trojan,virus,worm}.sh ; do
    [ -e "$f" ] && (( i++ ))
done
if ((i)) ; then
    echo Malware detected.
fi
相关问题