【问题标题】:error accessing filenames on bash loop [duplicate]在 bash 循环中访问文件名时出错 [重复]
【发布时间】:2018-12-21 12:27:45
【问题描述】:

我正在编写一个 bash 文件,该文件使用其中文件的相对路径遍历运行某个命令的目录。

#the inputCSV is a path, that the user introduces, i'm currently passing '.'

inputCSV="." 
for loopVar in $inputCSV"/*.csv"
do 
    echo "Dir result: "
    dir
    echo "inputCSV contains: "$inputCSV
    echo "This should be a filename but is: "$loopVar
done
unset input

dir 命令表明,目录中实际上有文件,并且 inputCSV 在其中有.,但loopVar 等于串联并且不会遍历目录。这是我的控制台输出:

Dir result: 
Ejemplo.csv batch.sh sqlFolder
inputCSV contains .
This should be a filename but is: ./*.csv

我正在使用Linux version 3.10.0-123.4.4.el7.x86_64 (builder@kbuilder.dev.centos.org) (gcc version 4.8.2 20140120 (Red Hat 4.8.2-16) (GCC) ) #1 SMP Fri Jul 25 05:07:12 UTC 2014

我做错了什么?

【问题讨论】:

    标签: bash for-loop sh


    【解决方案1】:

    带引号的通配符不会被 shell 扩展。另一方面,如果变量包含空格,最好引用变量以防止对它们进行分词:

    for loopVar in "$inputCSV"/*.csv
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-08
      • 2016-08-18
      • 2017-08-20
      • 1970-01-01
      • 1970-01-01
      • 2019-06-17
      • 1970-01-01
      相关资源
      最近更新 更多