bash - 在 if 语句中传递参数

标签 bash shell unix if-statement

在下面的代码中我试图搜索文件。 在这里,我想排除以“#”开头的文件

请帮我解决这个问题。

#!/bin/bash
while read -r name
    do
        if [[ $name!= "#" ]]
        then
            find ./2016* -name *files.txt
        fi
done < file.txt

出现以下错误:

conditional binary operator expected
syntax error near `"#"'
           if [[ $name!= "#" ]]

最佳答案

  1. $name!=之间添加空格

  2. 您需要排除以 # 开头的行。不仅仅是只有 # 的行。为此,将模式更改为 "#*"

  3. find 命令中使用变量name

#!/bin/bash

while read -r name
    do
        if [[ $name != "#*" ]]
        then
            find ./2016* -name "$name"
        fi
done < file.txt

这里提到了一种更好的排除注释行的方法 - bash loop skip commented lines

关于bash - 在 if 语句中传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38848148/

相关文章:

bash - 为什么 **sort** 不在每台机器上进行相同的排序?

linux - 如何在 200 台服务器上运行脚本并在堡垒实例终端中打印输出?我的 bash 脚本卡在 ssh 到远程服务器上

linux - CMake - 更改当前目录(例如 : project/build ) from CMakeLists. txt?

java - 从字符串中获取命令(密码)并设置为 Java 中 Unix 服务器的输入流(JSCH)

linux - 如何编辑字符串中的特定输出?

bash - Cron 作业未获取 .bashrc 中设置的环境变量

linux - 比较两个文件并写入不匹配的行和不匹配的列

android - Activity 创建者

linux - 导出新变量时如何组合变量名?

linux - (Unix) 将文本文件中的行更改为列