Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用bash脚本将另一个脚本应用于目录(如矩阵)中的每个文件_Bash_Shell_Matrix_Scripting_File Handling - Fatal编程技术网

使用bash脚本将另一个脚本应用于目录(如矩阵)中的每个文件

使用bash脚本将另一个脚本应用于目录(如矩阵)中的每个文件,bash,shell,matrix,scripting,file-handling,Bash,Shell,Matrix,Scripting,File Handling,假设我的目录中有五个文件 文件:1、2、3、4和5 我有一个脚本,它将执行一个数学过程来比较两个文件,我想在目录中的每个文件上使用这个脚本,下面的示例 将文件1与2、3、4和5进行比较 将文件2与3、4和5进行比较 将文件3与4和5进行比较 比较文件4和文件5 文件遵循此名称方案文件名_V0001.txt 如何编写一个简单的bash脚本来实现这一点 for a in * do start=0 for b in * do if [ "$start" = 1 ] then

假设我的目录中有五个文件

文件:1、2、3、4和5

我有一个脚本,它将执行一个数学过程来比较两个文件,我想在目录中的每个文件上使用这个脚本,下面的示例

将文件1与2、3、4和5进行比较

将文件2与3、4和5进行比较

将文件3与4和5进行比较

比较文件4和文件5

文件遵循此名称方案文件名_V0001.txt

如何编写一个简单的bash脚本来实现这一点

for a in *
do
  start=0
  for b in *
  do
    if [ "$start" = 1 ]
    then
      echo "Comparing $a with $b ..."
      diff "$a" "$b"
    elif [ "$a" = "$b" ]
    then
      start=1
    fi
  done
done

当然,文件的任何名称方案都可以在glob模式中给出,例如。g<代码>用于文件名中的脚本。\u V*.txt

script\u v1.sh:

#!/bin/bash
compare="/path/to/my/compare_tool"

for i in {1..5}; do
    for j in $(seq $((i+1)) 5); do
        echo "Comparing $i and $j"
        compare filename_V000$i.txt filename_V000$j.txt > result_$i_$j.txt
    done
done
$ > bash script_v1.sh
Comparing 1 and 2
Comparing 1 and 3
Comparing 1 and 4
Comparing 1 and 5
Comparing 2 and 3
Comparing 2 and 4
Comparing 2 and 5
Comparing 3 and 4
Comparing 3 and 5
Comparing 4 and 5
$ >
#!/bin/bash
compare="/path/to/my/compare_tool"

for i in {1..100}; do
    for j in $(seq $((i+1)) 100); do
        fi="Filename_V$(printf "%04d" $i).txt"
        fj="Filename_V$(printf "%04d" $j).txt"
        if [[ -f "$fi" && -f "$fj" ]]; then
            echo "Comparing $fi and $fj"
            compare "$fi" "$fj" > result_$i_$j.txt
        fi
    done
done
$ > bash script_v2.sh
Comparing Filename_V0001.txt and Filename_V0002.txt
...
Comparing Filename_V0001.txt and Filename_V0100.txt
Comparing Filename_V0002.txt and Filename_V0003.txt
...
Comparing Filename_V0002.txt and Filename_V0100.txt
Comparing Filename_V0003.txt and Filename_V0004.txt
...
Comparing Filename_V0099.txt and Filename_V0100.txt
$ >
结果\u v1:

#!/bin/bash
compare="/path/to/my/compare_tool"

for i in {1..5}; do
    for j in $(seq $((i+1)) 5); do
        echo "Comparing $i and $j"
        compare filename_V000$i.txt filename_V000$j.txt > result_$i_$j.txt
    done
done
$ > bash script_v1.sh
Comparing 1 and 2
Comparing 1 and 3
Comparing 1 and 4
Comparing 1 and 5
Comparing 2 and 3
Comparing 2 and 4
Comparing 2 and 5
Comparing 3 and 4
Comparing 3 and 5
Comparing 4 and 5
$ >
#!/bin/bash
compare="/path/to/my/compare_tool"

for i in {1..100}; do
    for j in $(seq $((i+1)) 100); do
        fi="Filename_V$(printf "%04d" $i).txt"
        fj="Filename_V$(printf "%04d" $j).txt"
        if [[ -f "$fi" && -f "$fj" ]]; then
            echo "Comparing $fi and $fj"
            compare "$fi" "$fj" > result_$i_$j.txt
        fi
    done
done
$ > bash script_v2.sh
Comparing Filename_V0001.txt and Filename_V0002.txt
...
Comparing Filename_V0001.txt and Filename_V0100.txt
Comparing Filename_V0002.txt and Filename_V0003.txt
...
Comparing Filename_V0002.txt and Filename_V0100.txt
Comparing Filename_V0003.txt and Filename_V0004.txt
...
Comparing Filename_V0099.txt and Filename_V0100.txt
$ >
script\u v2.sh:

#!/bin/bash
compare="/path/to/my/compare_tool"

for i in {1..5}; do
    for j in $(seq $((i+1)) 5); do
        echo "Comparing $i and $j"
        compare filename_V000$i.txt filename_V000$j.txt > result_$i_$j.txt
    done
done
$ > bash script_v1.sh
Comparing 1 and 2
Comparing 1 and 3
Comparing 1 and 4
Comparing 1 and 5
Comparing 2 and 3
Comparing 2 and 4
Comparing 2 and 5
Comparing 3 and 4
Comparing 3 and 5
Comparing 4 and 5
$ >
#!/bin/bash
compare="/path/to/my/compare_tool"

for i in {1..100}; do
    for j in $(seq $((i+1)) 100); do
        fi="Filename_V$(printf "%04d" $i).txt"
        fj="Filename_V$(printf "%04d" $j).txt"
        if [[ -f "$fi" && -f "$fj" ]]; then
            echo "Comparing $fi and $fj"
            compare "$fi" "$fj" > result_$i_$j.txt
        fi
    done
done
$ > bash script_v2.sh
Comparing Filename_V0001.txt and Filename_V0002.txt
...
Comparing Filename_V0001.txt and Filename_V0100.txt
Comparing Filename_V0002.txt and Filename_V0003.txt
...
Comparing Filename_V0002.txt and Filename_V0100.txt
Comparing Filename_V0003.txt and Filename_V0004.txt
...
Comparing Filename_V0099.txt and Filename_V0100.txt
$ >
结果\u v2:

#!/bin/bash
compare="/path/to/my/compare_tool"

for i in {1..5}; do
    for j in $(seq $((i+1)) 5); do
        echo "Comparing $i and $j"
        compare filename_V000$i.txt filename_V000$j.txt > result_$i_$j.txt
    done
done
$ > bash script_v1.sh
Comparing 1 and 2
Comparing 1 and 3
Comparing 1 and 4
Comparing 1 and 5
Comparing 2 and 3
Comparing 2 and 4
Comparing 2 and 5
Comparing 3 and 4
Comparing 3 and 5
Comparing 4 and 5
$ >
#!/bin/bash
compare="/path/to/my/compare_tool"

for i in {1..100}; do
    for j in $(seq $((i+1)) 100); do
        fi="Filename_V$(printf "%04d" $i).txt"
        fj="Filename_V$(printf "%04d" $j).txt"
        if [[ -f "$fi" && -f "$fj" ]]; then
            echo "Comparing $fi and $fj"
            compare "$fi" "$fj" > result_$i_$j.txt
        fi
    done
done
$ > bash script_v2.sh
Comparing Filename_V0001.txt and Filename_V0002.txt
...
Comparing Filename_V0001.txt and Filename_V0100.txt
Comparing Filename_V0002.txt and Filename_V0003.txt
...
Comparing Filename_V0002.txt and Filename_V0100.txt
Comparing Filename_V0003.txt and Filename_V0004.txt
...
Comparing Filename_V0099.txt and Filename_V0100.txt
$ >
假设:

  • 从文件所在的路径调用脚本

一种相当普遍的方法:

#!/bin/sh
for comp1; do
  shift
  for comp2; do
    echo "Comparing '$comp1' with '$comp2'"
    do_compare "$comp1" "$comp2"
  done
done
然后,您可以使用要比较的文件列表调用该脚本:

do_all_compares Filename_V*.txt

x的
相当于“$@”
中x的
。列表是在执行
for
语句时计算的,因此循环开始时发生的
shift
只会影响用于内部比较的列表。

我非常喜欢这种方法,它非常有效,谢谢。我将如何修改它,使其能够处理100范围内的数字;因此,我可以让它查看标记为Filename_v001.txt到Filename_V0100.txt的文件?@Chemist不是程序员:我又添加了一个示例。这很有意义,但当我在只有15个文件的目录上尝试时,我得到了一个错误,编号为0008和0009。我不知道为什么?一旦我在Filename_V(ect…).txt的数字前面加上“V”,它就工作得非常好!非常感谢。对,汤姆费内克。修正了这个问题。我会将glob保存到一个数组中,得到它的长度,然后像在其他编程语言中一样循环索引,以避免像Alfe的答案中那样的黑客攻击。