Linux bash脚本for循环

时间:2016-03-01 02:31:43

标签: linux bash batch-file sh

我有1.txt 2.txt和script.php

1.txt:
a
b
c
d
2.txt
www
rrr
ttt
yyy

我希望bash文件在Linux中执行此命令:

./script.php -n a -j www>>n_j.txt
./script.php -n a -j rrr>>n_j.txt
./script.php -n a -j ttt>>n_j.txt
./script.php -n a -j yyy>>n_j.txt
./script.php -n b -j www>>n_j.txt
./script.php -n b -j rrr>>n_j.txt
./script.php -n b -j ttt>>n_j.txt
.
.

我有一个使用wondows cmd的bat代码。我想要一个类似的代码来使用Linux命令行

@ECHO OFF


FOR /F "tokens=1 delims= " %%I IN (1.txt) DO FOR /F "tokens=1 delims= " %%E IN (2.txt) DO echo %%I %%E>n_j.txt & echo name_job: %%I %%E & FOR /F "tokens=*" %%S IN ('script.php -n %%I -j %%E') DO echo %%S>>names\n_j.txt

2 个答案:

答案 0 :(得分:1)

只需在while循环中传输文件,看看这是否有帮助。

#!/bin/bash
while read arg1
do
   while read arg2
   do
     ./script.php -n $arg1 -j $arg2 >>n_j.txt
   done<2.txt
done<1.txt

答案 1 :(得分:0)

#!/bin/bash

arr1=""
arr2=""
mapfile -t arr1 <1.txt
mapfile -t arr2 <2.txt

for i in ${!arr1[@]}; do
        for j in ${!arr2[@]}; do
                ./script.php -n "${arr1[i]}" -j "${arr2[j]}" >>n_j.txt
        done
done

mapfile -t将文件转换为数组,其中文件的每一行都是数组中的新索引(-t删除尾随换行符)。 for循环遍历数组的每个索引,外部循环遍历第一个文件中的行,内部循环遍历第二个文件中的行,使用当前数组索引调用script.php。