Shell-Script Bash











up vote
1
down vote

favorite












I have this loop in the shell script, but it seems I don't fully understand what it does:



especially gawk -v



for ((k=$1; $k<3 + 3 ; k=$k + $3))
do
echo $k | gawk -v s=4 '{print $1*s}'
done


Assume the arguments we have are 2 10 4










share|improve this question




















  • 2




    I'd be happy to answer none school work questions. Could you elaborate on in what context this is used and how it fits into a larger picture? You specify three arguments - but only $1 and $3 is used..
    – Claus Andersen
    yesterday












  • @ClausAndersen gawk -v means that we are assigning the valuse of s to 4 but how about the {print $1*s}
    – fatima
    yesterday










  • This is not a question, just a statement.
    – ctrl-alt-delor
    yesterday










  • What shell is it for?
    – ctrl-alt-delor
    yesterday






  • 1




    Did you run the script and see what it does?
    – RudiC
    yesterday















up vote
1
down vote

favorite












I have this loop in the shell script, but it seems I don't fully understand what it does:



especially gawk -v



for ((k=$1; $k<3 + 3 ; k=$k + $3))
do
echo $k | gawk -v s=4 '{print $1*s}'
done


Assume the arguments we have are 2 10 4










share|improve this question




















  • 2




    I'd be happy to answer none school work questions. Could you elaborate on in what context this is used and how it fits into a larger picture? You specify three arguments - but only $1 and $3 is used..
    – Claus Andersen
    yesterday












  • @ClausAndersen gawk -v means that we are assigning the valuse of s to 4 but how about the {print $1*s}
    – fatima
    yesterday










  • This is not a question, just a statement.
    – ctrl-alt-delor
    yesterday










  • What shell is it for?
    – ctrl-alt-delor
    yesterday






  • 1




    Did you run the script and see what it does?
    – RudiC
    yesterday













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I have this loop in the shell script, but it seems I don't fully understand what it does:



especially gawk -v



for ((k=$1; $k<3 + 3 ; k=$k + $3))
do
echo $k | gawk -v s=4 '{print $1*s}'
done


Assume the arguments we have are 2 10 4










share|improve this question















I have this loop in the shell script, but it seems I don't fully understand what it does:



especially gawk -v



for ((k=$1; $k<3 + 3 ; k=$k + $3))
do
echo $k | gawk -v s=4 '{print $1*s}'
done


Assume the arguments we have are 2 10 4







shell-script awk gawk






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited yesterday









ctrl-alt-delor

10.4k41955




10.4k41955










asked yesterday









fatima

62




62








  • 2




    I'd be happy to answer none school work questions. Could you elaborate on in what context this is used and how it fits into a larger picture? You specify three arguments - but only $1 and $3 is used..
    – Claus Andersen
    yesterday












  • @ClausAndersen gawk -v means that we are assigning the valuse of s to 4 but how about the {print $1*s}
    – fatima
    yesterday










  • This is not a question, just a statement.
    – ctrl-alt-delor
    yesterday










  • What shell is it for?
    – ctrl-alt-delor
    yesterday






  • 1




    Did you run the script and see what it does?
    – RudiC
    yesterday














  • 2




    I'd be happy to answer none school work questions. Could you elaborate on in what context this is used and how it fits into a larger picture? You specify three arguments - but only $1 and $3 is used..
    – Claus Andersen
    yesterday












  • @ClausAndersen gawk -v means that we are assigning the valuse of s to 4 but how about the {print $1*s}
    – fatima
    yesterday










  • This is not a question, just a statement.
    – ctrl-alt-delor
    yesterday










  • What shell is it for?
    – ctrl-alt-delor
    yesterday






  • 1




    Did you run the script and see what it does?
    – RudiC
    yesterday








2




2




I'd be happy to answer none school work questions. Could you elaborate on in what context this is used and how it fits into a larger picture? You specify three arguments - but only $1 and $3 is used..
– Claus Andersen
yesterday






I'd be happy to answer none school work questions. Could you elaborate on in what context this is used and how it fits into a larger picture? You specify three arguments - but only $1 and $3 is used..
– Claus Andersen
yesterday














@ClausAndersen gawk -v means that we are assigning the valuse of s to 4 but how about the {print $1*s}
– fatima
yesterday




@ClausAndersen gawk -v means that we are assigning the valuse of s to 4 but how about the {print $1*s}
– fatima
yesterday












This is not a question, just a statement.
– ctrl-alt-delor
yesterday




This is not a question, just a statement.
– ctrl-alt-delor
yesterday












What shell is it for?
– ctrl-alt-delor
yesterday




What shell is it for?
– ctrl-alt-delor
yesterday




1




1




Did you run the script and see what it does?
– RudiC
yesterday




Did you run the script and see what it does?
– RudiC
yesterday










2 Answers
2






active

oldest

votes

















up vote
3
down vote













I can only imagine this is a homework question.



Let's break this down. Firstly you have a loop which starts at $1 and increments by $3 and will stop at 6. So if you pass in 2 10 4, then the loop will start at 2, and increment by 4. It will immediately stop because 2 + 4 = 6.



So the following just print's 2. With the arguments 2 10 4.



for ((k=$1; $k<3 + 3 ; k=$k + $3))
do
echo $k
done




Each time round the loop, you process the output using gawk -v s=4 '{print $1*s}'. This is a very small "awk" program. It sets a variable s=4. Then prints $1*s (ie: it calculates 2 * 4 and just prints 8.






share|improve this answer






























    up vote
    1
    down vote













    The awk bit just prints the current value of $k (this is $1 in the awk code as it is read from the input) times 4 (this is the value of the awk variable s, as set on the command line).



    It would be shorter to do



    printf '%dn' "$(( 4*k ))"


    The loop goes from whatever the first argument is to 5 in steps of the third argument. The second argument does not make any difference.



    Therefore, the whole thing could be simplified down to



    seq "$(( 4*$1 ))" "$(( 4*$3 ))" 20


    The three arguments to GNU seq are "start, increment, and end". This is for the output, and the output will always be four times the current value of the loop variable. The loop starts at $1, so the output starts at four times that. The loop increments by $3, so we increment by four times that. The loop ends with $k at a maximum of 5 (one less than 3+3), so the output ends at 4*5.



    Or, if you want to do that seq call as a bash loop instead:



    for (( k = 4*$1; k <= 20; k += 4*$3 )); do
    printf '%dn' "$k"
    done


    And, as you see,



    for (( k = $1; k <= 5; k += $3 )); do
    printf '%dn' "$(( 4*k ))"
    done


    is not far from that.






    share|improve this answer























      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%2f487373%2fshell-script-bash%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      3
      down vote













      I can only imagine this is a homework question.



      Let's break this down. Firstly you have a loop which starts at $1 and increments by $3 and will stop at 6. So if you pass in 2 10 4, then the loop will start at 2, and increment by 4. It will immediately stop because 2 + 4 = 6.



      So the following just print's 2. With the arguments 2 10 4.



      for ((k=$1; $k<3 + 3 ; k=$k + $3))
      do
      echo $k
      done




      Each time round the loop, you process the output using gawk -v s=4 '{print $1*s}'. This is a very small "awk" program. It sets a variable s=4. Then prints $1*s (ie: it calculates 2 * 4 and just prints 8.






      share|improve this answer



























        up vote
        3
        down vote













        I can only imagine this is a homework question.



        Let's break this down. Firstly you have a loop which starts at $1 and increments by $3 and will stop at 6. So if you pass in 2 10 4, then the loop will start at 2, and increment by 4. It will immediately stop because 2 + 4 = 6.



        So the following just print's 2. With the arguments 2 10 4.



        for ((k=$1; $k<3 + 3 ; k=$k + $3))
        do
        echo $k
        done




        Each time round the loop, you process the output using gawk -v s=4 '{print $1*s}'. This is a very small "awk" program. It sets a variable s=4. Then prints $1*s (ie: it calculates 2 * 4 and just prints 8.






        share|improve this answer

























          up vote
          3
          down vote










          up vote
          3
          down vote









          I can only imagine this is a homework question.



          Let's break this down. Firstly you have a loop which starts at $1 and increments by $3 and will stop at 6. So if you pass in 2 10 4, then the loop will start at 2, and increment by 4. It will immediately stop because 2 + 4 = 6.



          So the following just print's 2. With the arguments 2 10 4.



          for ((k=$1; $k<3 + 3 ; k=$k + $3))
          do
          echo $k
          done




          Each time round the loop, you process the output using gawk -v s=4 '{print $1*s}'. This is a very small "awk" program. It sets a variable s=4. Then prints $1*s (ie: it calculates 2 * 4 and just prints 8.






          share|improve this answer














          I can only imagine this is a homework question.



          Let's break this down. Firstly you have a loop which starts at $1 and increments by $3 and will stop at 6. So if you pass in 2 10 4, then the loop will start at 2, and increment by 4. It will immediately stop because 2 + 4 = 6.



          So the following just print's 2. With the arguments 2 10 4.



          for ((k=$1; $k<3 + 3 ; k=$k + $3))
          do
          echo $k
          done




          Each time round the loop, you process the output using gawk -v s=4 '{print $1*s}'. This is a very small "awk" program. It sets a variable s=4. Then prints $1*s (ie: it calculates 2 * 4 and just prints 8.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 19 hours ago

























          answered yesterday









          couling

          263210




          263210
























              up vote
              1
              down vote













              The awk bit just prints the current value of $k (this is $1 in the awk code as it is read from the input) times 4 (this is the value of the awk variable s, as set on the command line).



              It would be shorter to do



              printf '%dn' "$(( 4*k ))"


              The loop goes from whatever the first argument is to 5 in steps of the third argument. The second argument does not make any difference.



              Therefore, the whole thing could be simplified down to



              seq "$(( 4*$1 ))" "$(( 4*$3 ))" 20


              The three arguments to GNU seq are "start, increment, and end". This is for the output, and the output will always be four times the current value of the loop variable. The loop starts at $1, so the output starts at four times that. The loop increments by $3, so we increment by four times that. The loop ends with $k at a maximum of 5 (one less than 3+3), so the output ends at 4*5.



              Or, if you want to do that seq call as a bash loop instead:



              for (( k = 4*$1; k <= 20; k += 4*$3 )); do
              printf '%dn' "$k"
              done


              And, as you see,



              for (( k = $1; k <= 5; k += $3 )); do
              printf '%dn' "$(( 4*k ))"
              done


              is not far from that.






              share|improve this answer



























                up vote
                1
                down vote













                The awk bit just prints the current value of $k (this is $1 in the awk code as it is read from the input) times 4 (this is the value of the awk variable s, as set on the command line).



                It would be shorter to do



                printf '%dn' "$(( 4*k ))"


                The loop goes from whatever the first argument is to 5 in steps of the third argument. The second argument does not make any difference.



                Therefore, the whole thing could be simplified down to



                seq "$(( 4*$1 ))" "$(( 4*$3 ))" 20


                The three arguments to GNU seq are "start, increment, and end". This is for the output, and the output will always be four times the current value of the loop variable. The loop starts at $1, so the output starts at four times that. The loop increments by $3, so we increment by four times that. The loop ends with $k at a maximum of 5 (one less than 3+3), so the output ends at 4*5.



                Or, if you want to do that seq call as a bash loop instead:



                for (( k = 4*$1; k <= 20; k += 4*$3 )); do
                printf '%dn' "$k"
                done


                And, as you see,



                for (( k = $1; k <= 5; k += $3 )); do
                printf '%dn' "$(( 4*k ))"
                done


                is not far from that.






                share|improve this answer

























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  The awk bit just prints the current value of $k (this is $1 in the awk code as it is read from the input) times 4 (this is the value of the awk variable s, as set on the command line).



                  It would be shorter to do



                  printf '%dn' "$(( 4*k ))"


                  The loop goes from whatever the first argument is to 5 in steps of the third argument. The second argument does not make any difference.



                  Therefore, the whole thing could be simplified down to



                  seq "$(( 4*$1 ))" "$(( 4*$3 ))" 20


                  The three arguments to GNU seq are "start, increment, and end". This is for the output, and the output will always be four times the current value of the loop variable. The loop starts at $1, so the output starts at four times that. The loop increments by $3, so we increment by four times that. The loop ends with $k at a maximum of 5 (one less than 3+3), so the output ends at 4*5.



                  Or, if you want to do that seq call as a bash loop instead:



                  for (( k = 4*$1; k <= 20; k += 4*$3 )); do
                  printf '%dn' "$k"
                  done


                  And, as you see,



                  for (( k = $1; k <= 5; k += $3 )); do
                  printf '%dn' "$(( 4*k ))"
                  done


                  is not far from that.






                  share|improve this answer














                  The awk bit just prints the current value of $k (this is $1 in the awk code as it is read from the input) times 4 (this is the value of the awk variable s, as set on the command line).



                  It would be shorter to do



                  printf '%dn' "$(( 4*k ))"


                  The loop goes from whatever the first argument is to 5 in steps of the third argument. The second argument does not make any difference.



                  Therefore, the whole thing could be simplified down to



                  seq "$(( 4*$1 ))" "$(( 4*$3 ))" 20


                  The three arguments to GNU seq are "start, increment, and end". This is for the output, and the output will always be four times the current value of the loop variable. The loop starts at $1, so the output starts at four times that. The loop increments by $3, so we increment by four times that. The loop ends with $k at a maximum of 5 (one less than 3+3), so the output ends at 4*5.



                  Or, if you want to do that seq call as a bash loop instead:



                  for (( k = 4*$1; k <= 20; k += 4*$3 )); do
                  printf '%dn' "$k"
                  done


                  And, as you see,



                  for (( k = $1; k <= 5; k += $3 )); do
                  printf '%dn' "$(( 4*k ))"
                  done


                  is not far from that.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited yesterday

























                  answered yesterday









                  Kusalananda

                  119k16225367




                  119k16225367






























                      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%2f487373%2fshell-script-bash%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

                      Plaza Victoria

                      Puebla de Zaragoza

                      Musa