循环Linux的bash脚本 [英] Linux bash script for loop

查看:119
本文介绍了循环Linux的bash脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的1.txt 2.txt和运行script.php

 的1.txt:
一个
b
C
ð
2.txt
万维网
存款准备金率
TTT
YYY

我想bash的文件做Linux中的这个命令:

  ./的script.php -n -j一个WWW>> n_j.txt
./script.php -n -j一个RRR>> n_j.txt
./script.php -n -j一个TTT>> n_j.txt
./script.php -n -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


我有一只蝙蝠code。与WONDOWS工作cmd..i想一个code就像它与Linux命令行的工作

  @ECHO OFF
FOR / F令牌= 1 delims =%% I IN(1.txt的)DO FOR / F令牌= 1 delims =%% E在(2.txt)DO呼应%%我%% E> n_j。 TXT和放大器;回声name_job:%%我%% E&安培; FOR / F令牌= *%% S IN('的script.php -n %%我-j %% E')DO回声%% S>>名\\ n_j.txt


解决方案

 #!/斌/庆典ARR1 =
ARR2 =
映射文件-t ARR1<的1.txt
映射文件-t ARR2< 2.txt因为我在$ {ARR1 [@]!};做
        为$ j中{!ARR2 [@]};做
                ./script.php -n$ {ARR1 [I]}-j$ {ARR2 [J]}>> n_j.txt
        DONE
DONE

映射文件-t 文件转换成一个数组,文件的每一行是数组中一个新的指数(-t删除尾随的换行符)。在阵列中的每个指标,外环通过线路中的第一个文件下去,内环通过第二个文件中的行会,呼吁script.php的当前数组索引。

的for循环迭代

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

解决方案

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

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

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