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

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

问题描述

我是 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 循环结构......这在 C、C++ 等中是可能的,但我如何完成以下某些事情.代码显然是错误的.

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脚本for循环中的两个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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