The $@ variable is not working in a for loop, trying to iterate through a users list

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












PROBLEM:



I can't get the $@ variable to do what I want in a for loop, the loop only finds one name in the file while looping, it should loop through all the arguments and write them to the file USERS.txt each on its own line.



Here is the file:



something78
something79
something7
dagny
oli
bjarni
toti
stefan_hlynur
jessie



Here is the test code:



#!/bin/bash

prepare_USERS()


prepare_USERS "$@"

#for user in "$@"
#do
# echo "$user" >> USERS.txt
#done

for user in USERS.txt
do
printf "%s" $user
done


Here are the arguments I pass:



./somethingDELETEme.sh jessie henry allison jason


CURRENT output:



$./somethingDELETEme.sh jessie henry allison jason
jessie henry allison jason
jessie


EXPECTED output:



The loop loops through all names from the argument list and writes it to the file USERS.txt.



QUESTION:



I have used this variable ($@) before and never had this problem.



Why is the loop not iterating through all names in the argument list ($@) and how is the right way coding this?



HERE IS THE REAL CODE:



prepare_USERS()
echo "writing to USERS.txt failed"; exit 127
done










share|improve this question



























    up vote
    0
    down vote

    favorite












    PROBLEM:



    I can't get the $@ variable to do what I want in a for loop, the loop only finds one name in the file while looping, it should loop through all the arguments and write them to the file USERS.txt each on its own line.



    Here is the file:



    something78
    something79
    something7
    dagny
    oli
    bjarni
    toti
    stefan_hlynur
    jessie



    Here is the test code:



    #!/bin/bash

    prepare_USERS()


    prepare_USERS "$@"

    #for user in "$@"
    #do
    # echo "$user" >> USERS.txt
    #done

    for user in USERS.txt
    do
    printf "%s" $user
    done


    Here are the arguments I pass:



    ./somethingDELETEme.sh jessie henry allison jason


    CURRENT output:



    $./somethingDELETEme.sh jessie henry allison jason
    jessie henry allison jason
    jessie


    EXPECTED output:



    The loop loops through all names from the argument list and writes it to the file USERS.txt.



    QUESTION:



    I have used this variable ($@) before and never had this problem.



    Why is the loop not iterating through all names in the argument list ($@) and how is the right way coding this?



    HERE IS THE REAL CODE:



    prepare_USERS()
    echo "writing to USERS.txt failed"; exit 127
    done










    share|improve this question

























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      PROBLEM:



      I can't get the $@ variable to do what I want in a for loop, the loop only finds one name in the file while looping, it should loop through all the arguments and write them to the file USERS.txt each on its own line.



      Here is the file:



      something78
      something79
      something7
      dagny
      oli
      bjarni
      toti
      stefan_hlynur
      jessie



      Here is the test code:



      #!/bin/bash

      prepare_USERS()


      prepare_USERS "$@"

      #for user in "$@"
      #do
      # echo "$user" >> USERS.txt
      #done

      for user in USERS.txt
      do
      printf "%s" $user
      done


      Here are the arguments I pass:



      ./somethingDELETEme.sh jessie henry allison jason


      CURRENT output:



      $./somethingDELETEme.sh jessie henry allison jason
      jessie henry allison jason
      jessie


      EXPECTED output:



      The loop loops through all names from the argument list and writes it to the file USERS.txt.



      QUESTION:



      I have used this variable ($@) before and never had this problem.



      Why is the loop not iterating through all names in the argument list ($@) and how is the right way coding this?



      HERE IS THE REAL CODE:



      prepare_USERS()
      echo "writing to USERS.txt failed"; exit 127
      done










      share|improve this question















      PROBLEM:



      I can't get the $@ variable to do what I want in a for loop, the loop only finds one name in the file while looping, it should loop through all the arguments and write them to the file USERS.txt each on its own line.



      Here is the file:



      something78
      something79
      something7
      dagny
      oli
      bjarni
      toti
      stefan_hlynur
      jessie



      Here is the test code:



      #!/bin/bash

      prepare_USERS()


      prepare_USERS "$@"

      #for user in "$@"
      #do
      # echo "$user" >> USERS.txt
      #done

      for user in USERS.txt
      do
      printf "%s" $user
      done


      Here are the arguments I pass:



      ./somethingDELETEme.sh jessie henry allison jason


      CURRENT output:



      $./somethingDELETEme.sh jessie henry allison jason
      jessie henry allison jason
      jessie


      EXPECTED output:



      The loop loops through all names from the argument list and writes it to the file USERS.txt.



      QUESTION:



      I have used this variable ($@) before and never had this problem.



      Why is the loop not iterating through all names in the argument list ($@) and how is the right way coding this?



      HERE IS THE REAL CODE:



      prepare_USERS()
      echo "writing to USERS.txt failed"; exit 127
      done







      bash shell-script variable for programming






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 3 mins ago









      muru

      34.1k578147




      34.1k578147










      asked 10 mins ago









      somethingSomething

      1,59993055




      1,59993055




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote













          The problem is with the incorrect usage of exit 127 in your for loop, which is exiting after the first for-loop iteration. You need to group the echo statement and the exit as a compound block under .. to prevent this.



          echo "$user" >> USERS.txt || echo "writing to USERS.txt failed"; exit 127; 


          Without this grouping what happens is the || defined applies only for the echo command and always run the exit command no matter if write to fail passed or failed.





          share




















            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: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            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%2f476193%2fthe-variable-is-not-working-in-a-for-loop-trying-to-iterate-through-a-users%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            2
            down vote













            The problem is with the incorrect usage of exit 127 in your for loop, which is exiting after the first for-loop iteration. You need to group the echo statement and the exit as a compound block under .. to prevent this.



            echo "$user" >> USERS.txt || echo "writing to USERS.txt failed"; exit 127; 


            Without this grouping what happens is the || defined applies only for the echo command and always run the exit command no matter if write to fail passed or failed.





            share
























              up vote
              2
              down vote













              The problem is with the incorrect usage of exit 127 in your for loop, which is exiting after the first for-loop iteration. You need to group the echo statement and the exit as a compound block under .. to prevent this.



              echo "$user" >> USERS.txt || echo "writing to USERS.txt failed"; exit 127; 


              Without this grouping what happens is the || defined applies only for the echo command and always run the exit command no matter if write to fail passed or failed.





              share






















                up vote
                2
                down vote










                up vote
                2
                down vote









                The problem is with the incorrect usage of exit 127 in your for loop, which is exiting after the first for-loop iteration. You need to group the echo statement and the exit as a compound block under .. to prevent this.



                echo "$user" >> USERS.txt || echo "writing to USERS.txt failed"; exit 127; 


                Without this grouping what happens is the || defined applies only for the echo command and always run the exit command no matter if write to fail passed or failed.





                share












                The problem is with the incorrect usage of exit 127 in your for loop, which is exiting after the first for-loop iteration. You need to group the echo statement and the exit as a compound block under .. to prevent this.



                echo "$user" >> USERS.txt || echo "writing to USERS.txt failed"; exit 127; 


                Without this grouping what happens is the || defined applies only for the echo command and always run the exit command no matter if write to fail passed or failed.






                share











                share


                share










                answered 2 mins ago









                Inian

                2,998822




                2,998822



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f476193%2fthe-variable-is-not-working-in-a-for-loop-trying-to-iterate-through-a-users%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    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?