Linux bash script for loop

匿名 (未验证) 提交于 2019-12-03 00:45:01

问题:

i have 1.txt 2.txt and script.php

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

i want bash file to do this command in 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 . . 

i have a bat code working with wondows cmd..i wanna a code like it to work with Linux command line

@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 

回答1:

#!/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 converts the file to an array, where each line of the file is a new index in the array (-t removes the trailing newline). The for loops iterate over each index of the array, the outer loop going through the lines in the first file, the inner loop going through the lines in the second file, calling script.php with the current array indices.



回答2:

Simply pipe the file in while loop, see if this could help.

#!/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 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!