从目录列表中的拟合文件中提取信息,但是在通过脚本编写时遇到问题

时间:2017-05-15 03:56:38

标签: bash unix terminal idl-programming-language

我正在编写脚本并需要cd遍历一堆子目录,但我无法让shell提交到cd,更不用说执行了其余的脚本正确。我对类似的问题进行了仔细研究,但没有一个人能够正确地回答我的问题 - 制作一个功能和采购剧本并没有奏效。我对终端还是比较新的,我现在很失落。

#!/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 $2}' > fuv_exptime #recognizes this command but
                   # can't execute properly because it's in the wrong directory
   done

2 个答案:

答案 0 :(得分:0)

正如问题的评论中已经说明的那样,有点难以猜测,脚本应该做什么。无论如何,假设目录更改部分是唯一的问题,我尝试以这种方式修复脚本:

#!/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 $2}' > fuv_exptime
       cd $scriptdir 
   done

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

答案 1 :(得分:0)

如果我理解你正在尝试做什么,这应该有效:

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