【问题标题】:Expand a variable containing a prefix that is a name of the directory containing all files by indicate the extension of files扩展包含前缀的变量,该前缀是包含所有文件的目录的名称,通过指示文件的扩展名
【发布时间】:2020-02-23 17:11:42
【问题描述】:

我有一个目录 temp_sources,其中包含 Fortran .f90 文件。

我尝试扩展通配符*.f90 以打印以temp_sources 目录为前缀的所有Fortran90 文件。

总而言之,我想存储在一个变量中:

temp_sources/file1.f90
temp_sources/file2.f90
temp_sources/file3.f90
...
temp_sources/file11.f90

我尝试在一个带有 2 个参数的脚本中进行操作(第一个用于目录,即temp_sources,第二个用于*.90

dir1="$dir1"
# Adding a slash for directory if not present
[[ "$dir1" != */ ]] && dir1=$dir1"/"
# Try to expand the files *.f90
files=( "$dir1${@:2:$#-2}" )
# Try to print the list with directory name as prefix
echo "$files"

但仅在打印中(带有最后一个回声):temp_sources/*.f90

它不会将.f90 文件列表扩展为temp_sources 目录作为前缀。

我不知道如何用这个目录名获取这个列表。

【问题讨论】:

    标签: bash shell wildcard


    【解决方案1】:

    试试

    files=( "$dir1"${@:2:$#-2} )
    

    您希望 shell 处理 * 的扩展,因此不应将其括在括号中。

    【讨论】:

    • 谢谢,它有效!只是最后一个精度:为什么我们不使用 $2 作为第二个参数而不是 ${@:2:$#-2} ?也许是因为我有义务通过使用$2 来引用*.f90,即在命令行中输入'*.f90'
    • 我不知道,我只是模仿你的命令,但我不知道你为什么要使用那种复杂的语法。这将处理的不仅仅是一个参数,而且它可能会在这个命令中失败,只是因为它可能不会像你期望的那样做。我解决问题的方法会有所不同:两个参数,文件夹和扩展名。参数中没有星号,并且在代码中相应地添加你的星号:files=( "$1"/*"$2" )
    猜你喜欢
    • 2014-07-11
    • 2011-03-24
    • 2017-03-14
    • 1970-01-01
    • 1970-01-01
    • 2016-01-28
    • 1970-01-01
    • 1970-01-01
    • 2011-10-13
    相关资源
    最近更新 更多