从目录列表中的适合文件中提取信息,但无法通过脚本进行 cd-ing

Extract information from fits files in a list of directories but having trouble cd-ing via scripting

我正在编写一个脚本,需要 cd 遍历一堆子目录,但我无法让 shell 提交到 cd,更不用说了正确执行脚本的其余部分。我仔细研究了类似的问题,但其中 none 正确地回答了我——创建一个函数并获取脚本没有用。我对终端还比较陌生,现在我很迷茫。

#!/bin/bash
. ./exptime.sh #without a #, this yields a segmentation fault

function exptime() {
   #make an array of directories
   filedir=( $(find ~/Documents/Images -maxdepth 1 -type d) ) 
   alias cdall 'cd ${filedir[*]}' #terminal has trouble recognizing the alias

   for filedirs in ${filedir[*]}
   do
       cdall
       ftlist "fuv.fits[1]" T column=3 rows=1 | grep "[0-9]" |
         awk '{print }' > fuv_exptime #recognizes this command but
                   # can't execute properly because it's in the wrong directory
   done

正如问题的评论中所述,很难猜测脚本应该做什么。无论如何,假设目录更改部分是唯一的问题,我会尝试以这种方式修复脚本:

#!/bin/bash
. ./exptime.sh #without a #, this yields a segmentation fault

function exptime() {
   #make an array of directories
   filedirs=( $(find ~/Documents/Images -maxdepth 1 -type d) )
   scriptdir=$(pwd)

   for filedir in ${filedirs[*]}
   do
       cd $filedir
       ftlist "fuv.fits[1]" T column=3 rows=1 | grep "[0-9]" | \
         awk '{print }' > fuv_exptime
       cd $scriptdir 
   done

换句话说,去掉 'cdall',并在 for 循环期间 cd 进入每个目录。在 ftlist 调用之后,cd 回到您调用脚本的目录,您在 for 循环之前将其保存在变量 'scriptdir' 中。希望这有帮助。

如果我理解正确你想做什么,这应该有效:

for dname in "$HOME"/Documents/Images/*/; do
    ftlist "$dname/fuv.fits[1]" T column=3 rows=1
done | awk '/[[:digit:]]/ { print  }' > fuv_exptime

这将遍历 ~/Documents/Images 的所有子目录并使用输入文件的完整路径运行 ftlist 命令。

输出将进入单个文件 fuv_exptime。请注意,grep 和 awk 步骤可以合并为一个 awk 命令。

如果您希望每个子目录中都有一个单独的输出文件 fuv_exptime,请更改为以下内容:

for dname in "$HOME"/Documents/Images/*/; do
    ftlist "$dname/fuv.fits[1]" T column=3 rows=1 |
        awk '/[[:digit:]]/ { print  }' > "$dname"/fuv_exptime
done