Linux bash 循环脚本
Linux bash script for loop
我有 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
.
.
我有一个与 windows 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
#!/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。
只需在 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.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
.
.
我有一个与 windows 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
#!/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。
只需在 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