的bash shell脚本循环两个变量 [英] bash shell script two variables in for loop

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

问题描述

我是新来的shell脚本。所以恳请大家多多包涵,如果我的疑问是太傻了。

I am new to shell scripting. so kindly bear with me if my doubt is too silly.

我在2个不同的目录和一个可执行从每个目录需要一个图像并对其进行处理,以产生一个新的图像PNG图像。

I have png images in 2 different directories and an executable which takes an images from each directory and processes them to generate a new image.

我要寻找一个for循环结构可以采取两个变量simultaneously..this在C是可能的,C ++等,但我怎么完成以下的东西。在code显然是错误的。

I am looking for a for loop construct which can take two variables simultaneously..this is possible in C, C++ etc but how do I accomplish something of the following. The code is obviously wrong.

#!/bin/sh

im1_dir=~/prev1/*.png  
im2_dir=~/prev3/*.png
index=0

for i,j in $im1_dir $im2_dir  # i iterates in im1_dir and j iterates in im2_dir 
do
  run_black.sh $i $j  
done

谢谢!

推荐答案

如果您是依赖于这两个目录基于一个语言环境匹配的排序顺序(如您的尝试),那么一个数组应该工作。

If you are depending on the two directories to match up based on a locale sorted order (like your attempt), then an array should work.

im1_files=(~/prev1/*.png)
im2_files=(~/prev3/*.png)

for ((i=0;i<=${#im1_files[@]};i++)); do
   run_black.sh "${im1_files[i]}" "${im2_files[i]}"
done

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

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