Last active
July 11, 2024 19:24
-
-
Save GrennKren/f5de9123d3cffaaf53a60cd3ea84172f to your computer and use it in GitHub Desktop.
Linux, increment variable always getting reset inside sub loop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
main="test_subtitution" | |
a="$main/folder_a" | |
b="$main/folder_b" | |
mkdir -p $a $b | |
touch $a/file_1 $a/file_5 $a/file_9 $a/file_10 $a/file_8 | |
touch $b/file_1 $b/file_5 $b/file_9 $b/file_10 $b/file_8 | |
###################### | |
echo "ls | while; do index++" | |
index=1 | |
ls $a | while read i; do echo "$index"_"$(basename $i)"; index=$(($index + 1)); done | |
# This will get | |
# 1_file_a | |
# 2_file_b | |
# 3_file_c | |
# 4_file_d | |
# 5_file_e | |
echo | |
###################### | |
echo "find | while read; do index++" | |
index=1 | |
find $main/* -type f | while read i | |
do | |
echo "$index"_"$(basename $i)" | |
index=$(($index + 1)) | |
done | |
# will get | |
# 1_file_1 | |
# 2_file_10 | |
# 3_file_5 | |
# 4_file_8 | |
# 5_file_9 | |
# 6_file_1 | |
# 7_file_10 | |
# 8_file_5 | |
# 9_file_8 | |
# 10_file_9 | |
echo | |
####################### | |
echo "ls -R | while; do index++" | |
index=1 | |
ls -R $main | while read i; do echo "$index"_"$(basename $i)"; index=$(($index + 1)); done | |
# Will get | |
# 1_test_subtitution: | |
# 2_folder_a | |
# 3_folder_b | |
# basename: missing operand | |
# Try 'basename --help' for more information. | |
# 4_ | |
# 5_folder_a: | |
# 6_file_1 | |
# 7_file_10 | |
# 8_file_5 | |
# 9_file_8 | |
# 10_file_9 | |
# basename: missing operand | |
# Try 'basename --help' for more information. | |
# 11_ | |
# 12_folder_b: | |
# 13_file_1 | |
# 14_file_10 | |
# 15_file_5 | |
# 16_file_8 | |
# 17_file_9 | |
echo | |
####################### | |
echo "find -type d | while; do ls | while; do index++" | |
index=1 | |
find $main/* -type d | while read i | |
do | |
ls $i | while read x | |
do | |
echo "$index"_"$(basename $x)" | |
index=$(($index + 1)) | |
done | |
done | |
# will get | |
# 1_file_1 | |
# 2_file_10 | |
# 3_file_5 | |
# 4_file_8 | |
# 5_file_9 | |
# 1_file_1 | |
# 2_file_10 | |
# 3_file_5 | |
# 4_file_8 | |
# 5_file_9 | |
echo | |
####################### (the solution) | |
echo "find -type d | while; do while; do index++ < <( )" | |
index=1 | |
find $main/* -type d | while read i | |
do | |
while read x | |
do | |
echo "$index"_"$(basename $x)" | |
index=$(($index + 1)) | |
done < <(ls $i) | |
done | |
# Will get | |
# 1_file_1 | |
# 2_file_10 | |
# 3_file_5 | |
# 4_file_8 | |
# 5_file_9 | |
# 6_file_1 | |
# 7_file_10 | |
# 8_file_5 | |
# 9_file_8 | |
# 10_file_9 | |
echo | |
####################### (same, but I just adding -v parameter to the ls command) | |
echo "find -type d | while; do while; do index++ < <(ls -v)" | |
index=1 | |
find $main/* -type d | while read i | |
do | |
while read x | |
do | |
echo "$index"_"$(basename $x)" | |
index=$(($index + 1)) | |
done < <(ls -v $i) | |
done | |
# Will get | |
# 1_file_1 | |
# 2_file_5 | |
# 3_file_8 | |
# 4_file_9 | |
# 5_file_10 | |
# 6_file_1 | |
# 7_file_5 | |
# 8_file_8 | |
# 9_file_9 | |
# 10_file_10 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment