读取和写入具有相同文件扩展名的多个文件

读取和写入具有相同文件扩展名的多个文件

我有三个不同的文件集,分别是 10234.su、10234.ph 和 10234.th。我有一个 shell 脚本,可以使用它从 10234.su 中提取一些数据,并可以写入 10234.ph 和 10234.th,但它一次只能用于一个文件。我需要进行修改,可以自动一一读取文件(xxx.su)并适当写入(xxx.ph 和 xxx.th)。所有三个文件始终具有相同的名称。示例 - 10234.su、10238.su、10242.su 等。我有很多不同名称的 .su 文件,我想自动读取其中一个。

我的脚本如下 -

rm hdrfile binary 

for i in *.su; do 
sugethw <"$i" output=geom key=cdp >hdrfile
a2b <hdrfile n1=994 >binary
echo "$i -> sugethw <"$i" output=geom key=cdp >hdrfile a2b <hdrfile n1=994 >binary"

for j in *.ph; do 
sushw <"$j" infile=binary key=cdp >test1.su
rm "$j"
mv test.su "$j"
echo "$j -> sushw <"$j" infile=binary key=cdp >test.su rm "$j" mv test.su "$j""

for k in *.th; do
sushw <"$k" infile=binary key=cdp >test2.su
rm "$k"
mv test.su "$k"
echo "$k -> sushw <"$k" infile=binary key=cdp >test.su rm "$k" mv test.su "$k""

rm hdrfile binary
done
done
done 

谢谢!

答案1

尝试以下 bash shell 脚本。

经过测试,它作用于所有文件,您必须删除我的echo行和行,并用,和cp取消注释您的实际操作行。sugethwa2bsushw

我认为您不需要在移动之前删除文件,因为mv除非您使用mv -i.

#!/bin/bash

for filnam in *.su
do
 tmp=${filnam%%.su}
 i="$filnam"
 j="${tmp}.ph"  # guessing they need not be nested
 k="${tmp}.th"  # guessing they need not be nested

 echo "$tmp.hdr" > hdrfile  # replacing lines with commands unknown to me
 echo "$tmp.bin" > binary   # replacing lines with commands unknown to me
 #sugethw <"$i" output=geom key=cdp >hdrfile
 #a2b <hdrfile n1=994 >binary
 echo "$i -> sugethw <\"$i\" output=geom key=cdp >hdrfile a2b <hdrfile n1=994 >binary"

 cp binary test1.su  # replacing line with command unknown to me
 #sushw <"$j" infile=binary key=cdp >test1.su
 #rm "$j"
 mv test1.su "$j"
 echo "$j -> sushw <\"$j\" infile=binary key=cdp >test.su rm \"$j\" mv test.su \"$j\""

 cp binary test2.su  # replacing line with command unknown to me
 #sushw <"$k" infile=binary key=cdp >test2.su
 #rm "$k"
 mv test2.su "$k"
 echo "$k -> sushw <\"$k\" infile=binary key=cdp >test.su rm \"$k\" mv test.su \"$k\""
 echo "---"
done 

rm hdrfile binary

相关内容