在目录中的同一文件上制作文件列表和 运行 命令

making file list and running commands on same file in directory

我在一个扩展名为 .ar 的目录中有 50 个文件。

我想用这些文件名制作一个列表,读取每个文件,返回目录并对每个文件 运行 以下 2 个命令。 $i 是 filename.ar

paz -r -L -e clean $i
psrplot -pF -j CDTp  -j 'C max'  -N2,1 -D $i.ps/cps -c set=pub -c psd=0 $i $i.clean

使用 *.ar 不起作用,因为它只是不断覆盖第一个文件并且没有给出正确的输出。有人可以帮忙写一个 bash 脚本吗?

我用的bash脚本没有做列表,直接在目录运行ning是

#!env bash

for i in $@
do
        outfile=$(basename $i).txt
    echo $i
        paz -r -L -e clean $i
        psrplot -pF -j CDTp  -j 'C max'  -N2,1 -D $i.ps/cps -c set=pub -c psd=0 $i $i.clean
 done

请帮忙,我已经试了一段时间了。

您想处理每个文件,一次一个。最安全的方法是使用 find ... -print0while read ...。像这样:

#!/bin/bash
#
ardir="/data"

# Basic validation
if [[ ! -d "$ardir" ]]
then
    echo "ERROR: the directory ($ardir) does not exist."
    exit 1
fi

# Process each file
find "$ardir" -type f -name "*.ar" -print0 | while IFS= read -r -d '' arfile
do
    echo "DEBUG file=$arfile"

    paz -r -L -e clean $arfile
    psrplot -pF -j CDTp  -j 'C max'  -N2,1 -D $arfile.ps/cps -c set=pub -c psd=0 $arfile $arfile.clean
done

此处记录了此方法(以及更多方法!):http://mywiki.wooledge.org/BashFAQ/001