简体   繁体   中英

Brace expansion - too many expressions

I want to find all files of the type 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.

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

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; the brace expansion is expanded before passing arguments to the -e , so there are two extra arguments. 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.

Possible workaround:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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