Skip identical values in nestled for loops
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I want to create directories in bash by iterating over values from multiple loops (here for simplicity just 2) while skipping identical values. A demonstrating example looks like this:
for i in 1 2 3; do for j in 1 2 3; do mkdir $i$j; done, done
This gives me folders named 11, 12, 13, 21, 22, 23, 31, 32, 33
, but I want to have only folders with different digits, so 11
, 22
and 33
should not exist. Is there a convenient for loop option to skip identical values?
Alternatively one could also create all folders and delete those with multiple values afterwards, but this seems very inefficient if there are multiple loops with many entries.
bash for
add a comment |
up vote
0
down vote
favorite
I want to create directories in bash by iterating over values from multiple loops (here for simplicity just 2) while skipping identical values. A demonstrating example looks like this:
for i in 1 2 3; do for j in 1 2 3; do mkdir $i$j; done, done
This gives me folders named 11, 12, 13, 21, 22, 23, 31, 32, 33
, but I want to have only folders with different digits, so 11
, 22
and 33
should not exist. Is there a convenient for loop option to skip identical values?
Alternatively one could also create all folders and delete those with multiple values afterwards, but this seems very inefficient if there are multiple loops with many entries.
bash for
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I want to create directories in bash by iterating over values from multiple loops (here for simplicity just 2) while skipping identical values. A demonstrating example looks like this:
for i in 1 2 3; do for j in 1 2 3; do mkdir $i$j; done, done
This gives me folders named 11, 12, 13, 21, 22, 23, 31, 32, 33
, but I want to have only folders with different digits, so 11
, 22
and 33
should not exist. Is there a convenient for loop option to skip identical values?
Alternatively one could also create all folders and delete those with multiple values afterwards, but this seems very inefficient if there are multiple loops with many entries.
bash for
I want to create directories in bash by iterating over values from multiple loops (here for simplicity just 2) while skipping identical values. A demonstrating example looks like this:
for i in 1 2 3; do for j in 1 2 3; do mkdir $i$j; done, done
This gives me folders named 11, 12, 13, 21, 22, 23, 31, 32, 33
, but I want to have only folders with different digits, so 11
, 22
and 33
should not exist. Is there a convenient for loop option to skip identical values?
Alternatively one could also create all folders and delete those with multiple values afterwards, but this seems very inefficient if there are multiple loops with many entries.
bash for
bash for
asked Nov 29 at 10:30
Andreas
253
253
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
Note: The loops below run from 1 to 9 using brace expansions. Use 1..3
or 1 2 3
to do exactly as in the question.
Compare $i
and $j
to make sure that they are different before creating the directory:
for i in 1..9; do
for j in 1..9; do
[ "$i" -ne "$j" ] && mkdir "$i$j"
done
done
The -ne
test tests for arithmetic inequality. If you are looping over strings, use !=
instead. If the test is true ($i
and $j
are different), the directory is created with mkdir
.
[ "$i" -ne "$j" ] && mkdir "$i$j"
is a short-cut way of writing
if [ "$i" -ne "$j" ]; then
mkdir "$i$j"
fi
To delete all directories that have names like 11
, 22
etc.:
for i in 1..9; do
rmdir "$i$i"
done
This assumes that the directories are empty. Use rm -rf "$i$i"
if they are not empty.
Great! This solves it, however you made a small mistake, the last " is missing in this expression: [ "$i" -ne "$j ], please correct it
– Andreas
Nov 29 at 10:45
@Andreas Doh! Thanks. Fixed.
– Kusalananda
Nov 29 at 10:46
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Note: The loops below run from 1 to 9 using brace expansions. Use 1..3
or 1 2 3
to do exactly as in the question.
Compare $i
and $j
to make sure that they are different before creating the directory:
for i in 1..9; do
for j in 1..9; do
[ "$i" -ne "$j" ] && mkdir "$i$j"
done
done
The -ne
test tests for arithmetic inequality. If you are looping over strings, use !=
instead. If the test is true ($i
and $j
are different), the directory is created with mkdir
.
[ "$i" -ne "$j" ] && mkdir "$i$j"
is a short-cut way of writing
if [ "$i" -ne "$j" ]; then
mkdir "$i$j"
fi
To delete all directories that have names like 11
, 22
etc.:
for i in 1..9; do
rmdir "$i$i"
done
This assumes that the directories are empty. Use rm -rf "$i$i"
if they are not empty.
Great! This solves it, however you made a small mistake, the last " is missing in this expression: [ "$i" -ne "$j ], please correct it
– Andreas
Nov 29 at 10:45
@Andreas Doh! Thanks. Fixed.
– Kusalananda
Nov 29 at 10:46
add a comment |
up vote
1
down vote
accepted
Note: The loops below run from 1 to 9 using brace expansions. Use 1..3
or 1 2 3
to do exactly as in the question.
Compare $i
and $j
to make sure that they are different before creating the directory:
for i in 1..9; do
for j in 1..9; do
[ "$i" -ne "$j" ] && mkdir "$i$j"
done
done
The -ne
test tests for arithmetic inequality. If you are looping over strings, use !=
instead. If the test is true ($i
and $j
are different), the directory is created with mkdir
.
[ "$i" -ne "$j" ] && mkdir "$i$j"
is a short-cut way of writing
if [ "$i" -ne "$j" ]; then
mkdir "$i$j"
fi
To delete all directories that have names like 11
, 22
etc.:
for i in 1..9; do
rmdir "$i$i"
done
This assumes that the directories are empty. Use rm -rf "$i$i"
if they are not empty.
Great! This solves it, however you made a small mistake, the last " is missing in this expression: [ "$i" -ne "$j ], please correct it
– Andreas
Nov 29 at 10:45
@Andreas Doh! Thanks. Fixed.
– Kusalananda
Nov 29 at 10:46
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Note: The loops below run from 1 to 9 using brace expansions. Use 1..3
or 1 2 3
to do exactly as in the question.
Compare $i
and $j
to make sure that they are different before creating the directory:
for i in 1..9; do
for j in 1..9; do
[ "$i" -ne "$j" ] && mkdir "$i$j"
done
done
The -ne
test tests for arithmetic inequality. If you are looping over strings, use !=
instead. If the test is true ($i
and $j
are different), the directory is created with mkdir
.
[ "$i" -ne "$j" ] && mkdir "$i$j"
is a short-cut way of writing
if [ "$i" -ne "$j" ]; then
mkdir "$i$j"
fi
To delete all directories that have names like 11
, 22
etc.:
for i in 1..9; do
rmdir "$i$i"
done
This assumes that the directories are empty. Use rm -rf "$i$i"
if they are not empty.
Note: The loops below run from 1 to 9 using brace expansions. Use 1..3
or 1 2 3
to do exactly as in the question.
Compare $i
and $j
to make sure that they are different before creating the directory:
for i in 1..9; do
for j in 1..9; do
[ "$i" -ne "$j" ] && mkdir "$i$j"
done
done
The -ne
test tests for arithmetic inequality. If you are looping over strings, use !=
instead. If the test is true ($i
and $j
are different), the directory is created with mkdir
.
[ "$i" -ne "$j" ] && mkdir "$i$j"
is a short-cut way of writing
if [ "$i" -ne "$j" ]; then
mkdir "$i$j"
fi
To delete all directories that have names like 11
, 22
etc.:
for i in 1..9; do
rmdir "$i$i"
done
This assumes that the directories are empty. Use rm -rf "$i$i"
if they are not empty.
edited Nov 29 at 10:46
answered Nov 29 at 10:33
Kusalananda
119k16223364
119k16223364
Great! This solves it, however you made a small mistake, the last " is missing in this expression: [ "$i" -ne "$j ], please correct it
– Andreas
Nov 29 at 10:45
@Andreas Doh! Thanks. Fixed.
– Kusalananda
Nov 29 at 10:46
add a comment |
Great! This solves it, however you made a small mistake, the last " is missing in this expression: [ "$i" -ne "$j ], please correct it
– Andreas
Nov 29 at 10:45
@Andreas Doh! Thanks. Fixed.
– Kusalananda
Nov 29 at 10:46
Great! This solves it, however you made a small mistake, the last " is missing in this expression: [ "$i" -ne "$j ], please correct it
– Andreas
Nov 29 at 10:45
Great! This solves it, however you made a small mistake, the last " is missing in this expression: [ "$i" -ne "$j ], please correct it
– Andreas
Nov 29 at 10:45
@Andreas Doh! Thanks. Fixed.
– Kusalananda
Nov 29 at 10:46
@Andreas Doh! Thanks. Fixed.
– Kusalananda
Nov 29 at 10:46
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f484872%2fskip-identical-values-in-nestled-for-loops%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown