我想找到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'我试着用支撑扩张,但它不起作用。我得到的错误“太多的论点”,我如何纠正它?我只能在条件或条件下做这件事吗?
而且,这不管用-
-e "$file""_trojan.sh" -o "$file""_worm.sh" -o "$file""_virus.sh"发布于 2013-08-01 20:49:33
-e运算符只能接受一个参数;在将参数传递给-e之前,大括号展开是展开的,因此有两个额外的参数。您可以使用循环:
for t in trojan virus worm; do
if [ -e "{$file}_$t.sh" ]; then
echo "malware detected"
fi
do或者就像马克在我打完字之前建议的那样:
for f in "${file}_"{trojan,virus,worm}.sh; do
if [ -e "$f" ]; then
echo "malware detected"
fi
done发布于 2013-08-01 20:49:21
问题不在于扩张,它运作得很好。问题在于-e测试:它只需要一个参数,而不是三个。
可能的解决办法:
i=0
for f in "$1"_{trojan,virus,worm}.sh ; do
[ -e "$f" ] && (( i++ ))
done
if ((i)) ; then
echo Malware detected.
fihttps://stackoverflow.com/questions/18004192
复制相似问题