通过使用for循环,bash脚本不适用于许多文件 [英] Bash script is not working on many files by using for loop

查看:83
本文介绍了通过使用for循环,bash脚本不适用于许多文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的脚本:

#!/bin/bash
SOL_atom_index=116101
number=1
SOL_mol_index=SOL$number
index=1
for index in {1..100}
do
    for SOL_index in {116101..136845}
    do
            sed -i "s/$SOL_atom_index/$SOL_mol_index/g;s/$(($SOL_atom_index+1))/$SOL_mol_index/g;s/$(($SOL_atom_index+2))/$SOL_mol_index/g" eq2_8_new_$index.ndx
            SOL_atom_index=$(($SOL_atom_index+3))
        number=$(($number+1))
        SOL_mol_index=SOL$number
    done
done

我有很多文本文件,它们的名称分别是:eq2_8_new_1.ndx,eq2_8_new_2.ndx,...... eq2_8_new_100.ndx

I have many text files which names are: eq2_8_new_1.ndx, eq2_8_new_2.ndx, ...... eq2_8_new_100.ndx

我尝试在所有文件上使用我的脚本.我使用变量"index"和for循环,因此我希望这部分代码可以在每个文件上使用

I try to use my script on all files. I use variable "index" and for loop, so I expect that this part of code will work on every file

for SOL_index in {116101..136845}
    do
            sed -i "s/$SOL_atom_index/$SOL_mol_index/g;s/$(($SOL_atom_index+1))/$SOL_mol_index/g;s/$(($SOL_atom_index+2))/$SOL_mol_index/g" eq2_8_new_$index.ndx
            SOL_atom_index=$(($SOL_atom_index+3))
        number=$(($number+1))
        SOL_mol_index=SOL$number
    done

您看到了吗?在这里,我有一个sed命令,该命令将在特定文件上运行(在该行的末尾有eq2_8_new_ $ index.ndx

Did you see this? Here I have a sed command which will work on specific file (at the end of the line I have eq2_8_new_$index.ndx

            sed -i "s/$SOL_atom_index/$SOL_mol_index/g;s/$(($SOL_atom_index+1))/$SOL_mol_index/g;s/$(($SOL_atom_index+2))/$SOL_mol_index/g" eq2_8_new_$index.ndx

因此,如果我使用for循环(用于{1..100}中的索引),我希望这将适用于从eq2_8_new_1.ndx到eq2_8_new_100.ndx的每个文本文件 但是我的脚本仅更改了第一个文件(eq2_8_new_1.ndx,而其他文件中没有更改).

So if I use for loop (for index in {1..100}) I expect that this will work on every text file from eq2_8_new_1.ndx to eq2_8_new_100.ndx BUT my script change only the first file (eq2_8_new_1.ndx and the is no changes in otherfiles).

推荐答案

对通配符使用ls,例如:

for eqfile in `ls -l /*.ndx`
   do
     sed -i "s/$SOL_atom_index/$SOL_mol_index/g;s/$(($SOL_atom_index+1))/$SOL_mol_index/g;s/$(($SOL_atom_index+2))/$SOL_mol_index/g" $eqfile 
   done

这篇关于通过使用for循环,bash脚本不适用于许多文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆