nikita0509
nikita0509

Reputation: 31

making file list and running commands on same file in directory

I have 50 files in a directory with .ar extension.

I have an idea of making a list with these file names, read each file, go back to the directory and run the following 2 commands on each file. $i is the 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

Using *.ar does not work as it just keeps over-writing the first file and gives no proper output. Can someone please help with a bash script.

The bash script I used without making a list and directly running in the directory is

#!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

Please help, I have been trying for a while.

Upvotes: 0

Views: 470

Answers (1)

Nic3500
Nic3500

Reputation: 8601

You want to process each file, one at a time. The safest way to do this is to use find ... -print0 with a while read .... Like this:

#!/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

This method (and so many more!) is documented here: http://mywiki.wooledge.org/BashFAQ/001

Upvotes: 2

Related Questions