Skip to main content
replaced http://unix.stackexchange.com/ with https://unix.stackexchange.com/
Source Link

There's no built-in construct to iterate over two lists at the same time. A for statement iterates over one list.

If you're using a shell with arrays, don't store lists as newline-delimited strings, use arrays insteaddon't store lists as newline-delimited strings, use arrays instead.

If oldfilenames and newfilenames are arrays, you can iterate over the array index.

for ((i=0; i<${#oldfilenames[@]}; i++)); do
  x=${oldfilenames[$i]}; y=${newfilenames[$i]}; …
done

If you have to have newline-delimited strings, you can strip the first line, one line at a time.

nl='
'
oldfilenames=${oldfilenames%"$nl"}
while [ -n "$oldfilenames" ]; do
  x=${oldfilenames%%"$nl"*}; y=${newfilenames%%"$nl"*}
  oldfilenames=${oldfilenames#*"$nl"}; newfilenames=${newfilenames#*"$nl"};
  …
done

There's no built-in construct to iterate over two lists at the same time. A for statement iterates over one list.

If you're using a shell with arrays, don't store lists as newline-delimited strings, use arrays instead.

If oldfilenames and newfilenames are arrays, you can iterate over the array index.

for ((i=0; i<${#oldfilenames[@]}; i++)); do
  x=${oldfilenames[$i]}; y=${newfilenames[$i]}; …
done

If you have to have newline-delimited strings, you can strip the first line, one line at a time.

nl='
'
oldfilenames=${oldfilenames%"$nl"}
while [ -n "$oldfilenames" ]; do
  x=${oldfilenames%%"$nl"*}; y=${newfilenames%%"$nl"*}
  oldfilenames=${oldfilenames#*"$nl"}; newfilenames=${newfilenames#*"$nl"};
  …
done

There's no built-in construct to iterate over two lists at the same time. A for statement iterates over one list.

If you're using a shell with arrays, don't store lists as newline-delimited strings, use arrays instead.

If oldfilenames and newfilenames are arrays, you can iterate over the array index.

for ((i=0; i<${#oldfilenames[@]}; i++)); do
  x=${oldfilenames[$i]}; y=${newfilenames[$i]}; …
done

If you have to have newline-delimited strings, you can strip the first line, one line at a time.

nl='
'
oldfilenames=${oldfilenames%"$nl"}
while [ -n "$oldfilenames" ]; do
  x=${oldfilenames%%"$nl"*}; y=${newfilenames%%"$nl"*}
  oldfilenames=${oldfilenames#*"$nl"}; newfilenames=${newfilenames#*"$nl"};
  …
done
Source Link
Gilles 'SO- stop being evil'
  • 865.4k
  • 205
  • 1.8k
  • 2.3k

There's no built-in construct to iterate over two lists at the same time. A for statement iterates over one list.

If you're using a shell with arrays, don't store lists as newline-delimited strings, use arrays instead.

If oldfilenames and newfilenames are arrays, you can iterate over the array index.

for ((i=0; i<${#oldfilenames[@]}; i++)); do
  x=${oldfilenames[$i]}; y=${newfilenames[$i]}; …
done

If you have to have newline-delimited strings, you can strip the first line, one line at a time.

nl='
'
oldfilenames=${oldfilenames%"$nl"}
while [ -n "$oldfilenames" ]; do
  x=${oldfilenames%%"$nl"*}; y=${newfilenames%%"$nl"*}
  oldfilenames=${oldfilenames#*"$nl"}; newfilenames=${newfilenames#*"$nl"};
  …
done