Skip identical values in nestled for loops

The name of the pictureThe name of the pictureThe name of the pictureClash 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.










share|improve this question

























    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.










    share|improve this question























      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.










      share|improve this question













      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 29 at 10:30









      Andreas

      253




      253




















          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.






          share|improve this answer






















          • 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











          Your Answer








          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "106"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          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

























          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.






          share|improve this answer






















          • 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















          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.






          share|improve this answer






















          • 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













          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.






          share|improve this answer














          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.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          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

















          • 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


















          draft saved

          draft discarded
















































          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.




          draft saved


          draft discarded














          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





















































          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






          Popular posts from this blog

          How to check contact read email or not when send email to Individual?

          Displaying single band from multi-band raster using QGIS

          How many registers does an x86_64 CPU actually have?