linux - 大括号扩展 - 表达式太多

标签 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"

最佳答案

-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

关于linux - 大括号扩展 - 表达式太多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18004192/

相关文章:

linux - centos使用wget下载文件(https),​​在服务器上可用,但在本地虚拟机中停止?

linux - 排除日志中的 grep 文件名

c - 将时间戳格式化为文本

php - 只有硬编码的字符串必须在 PHP 和 Mysql 中转义

Java:Apache Mina IoAcceptor 无法在 Linux 中解除绑定(bind)

linux - bash, if then 解析文件的脚本

python - 对元组列表的元素进行算术并检测所需的成对组合

git - 使用git作为部署工具

arrays - For 循环中奇怪的 Bash 变量赋值行为

c - Waitpid 函数中的 WNOHANG 宏