简体   繁体   English

支撑扩张 - 表达太多

[英]Brace expansion - too many expressions

I want to find all files of the type FileName_trojan.sh, FileName_virus.sh, FileName_worm.sh. 我想找到FileName_trojan.sh,FileName_virus.sh,FileName_worm.sh类型的所有文件。 If any such file is found, then display a message. 如果找到任何此类文件,则显示一条消息。

Here FileName is the argument passed to a script. 这里FileName是传递给脚本的参数。

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

I tried to use brace expansion, but it did not work. 我试图使用大括号扩展,但它没有用。 I get the error "too many arguments" How do I fix it ? 我得到错误“太多参数”如何修复它? Can I do it only with OR conditions ? 我可以只用OR条件吗?

Also, this does not work - 此外,这不起作用 -

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

The -e operator can take only one argument; -e运算符只能使用一个参数; the brace expansion is expanded before passing arguments to the -e , so there are two extra arguments. 在将参数传递给-e之前扩展大括号扩展,因此有两个额外的参数。 You can use a loop: 你可以使用一个循环:

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

or as Mark suggested before I could finish typing it: 或者在我完成输入之前马克建议:

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

The problem is not with the expansion, it worked ok. 问题不在于扩展,它运作正常。 The problem is with the -e test: it only takes one argument, not three. 问题在于-e test:它只需要一个参数,而不是三个。

Possible workaround: 可能的解决方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM