How to pass in multiple arguments to execute with .sh script












1















I have a list.txt file including a list of log file.



For example



    server_log1
server_log2
........
server_log50


I have another shell script used to download these logs. It worked like this



./script.sh serverlog1


I want to make it automatically that means it can automatically pass in each log file's name in list.txt to be executed.



Is that possible?
I tried



#!/bin/bash
for i in `cat /home/ec2-user/list.txt` ; do
sh ./workaround.sh $i
done


But it didn't work










share|improve this question




















  • 1





    sidenote: don't use file extensions for executables (scripts etc). If you re-write in python, C, or what ever, then you don't want to have to change the file name, as it will cause you to change every script that uses it.

    – ctrl-alt-delor
    Feb 22 '18 at 9:14
















1















I have a list.txt file including a list of log file.



For example



    server_log1
server_log2
........
server_log50


I have another shell script used to download these logs. It worked like this



./script.sh serverlog1


I want to make it automatically that means it can automatically pass in each log file's name in list.txt to be executed.



Is that possible?
I tried



#!/bin/bash
for i in `cat /home/ec2-user/list.txt` ; do
sh ./workaround.sh $i
done


But it didn't work










share|improve this question




















  • 1





    sidenote: don't use file extensions for executables (scripts etc). If you re-write in python, C, or what ever, then you don't want to have to change the file name, as it will cause you to change every script that uses it.

    – ctrl-alt-delor
    Feb 22 '18 at 9:14














1












1








1


1






I have a list.txt file including a list of log file.



For example



    server_log1
server_log2
........
server_log50


I have another shell script used to download these logs. It worked like this



./script.sh serverlog1


I want to make it automatically that means it can automatically pass in each log file's name in list.txt to be executed.



Is that possible?
I tried



#!/bin/bash
for i in `cat /home/ec2-user/list.txt` ; do
sh ./workaround.sh $i
done


But it didn't work










share|improve this question
















I have a list.txt file including a list of log file.



For example



    server_log1
server_log2
........
server_log50


I have another shell script used to download these logs. It worked like this



./script.sh serverlog1


I want to make it automatically that means it can automatically pass in each log file's name in list.txt to be executed.



Is that possible?
I tried



#!/bin/bash
for i in `cat /home/ec2-user/list.txt` ; do
sh ./workaround.sh $i
done


But it didn't work







linux shell-script arguments






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 23 '18 at 3:19







The One

















asked Feb 22 '18 at 7:28









The OneThe One

1,24062032




1,24062032








  • 1





    sidenote: don't use file extensions for executables (scripts etc). If you re-write in python, C, or what ever, then you don't want to have to change the file name, as it will cause you to change every script that uses it.

    – ctrl-alt-delor
    Feb 22 '18 at 9:14














  • 1





    sidenote: don't use file extensions for executables (scripts etc). If you re-write in python, C, or what ever, then you don't want to have to change the file name, as it will cause you to change every script that uses it.

    – ctrl-alt-delor
    Feb 22 '18 at 9:14








1




1





sidenote: don't use file extensions for executables (scripts etc). If you re-write in python, C, or what ever, then you don't want to have to change the file name, as it will cause you to change every script that uses it.

– ctrl-alt-delor
Feb 22 '18 at 9:14





sidenote: don't use file extensions for executables (scripts etc). If you re-write in python, C, or what ever, then you don't want to have to change the file name, as it will cause you to change every script that uses it.

– ctrl-alt-delor
Feb 22 '18 at 9:14










4 Answers
4






active

oldest

votes


















1














The easiest method for reading arguments can be described as follows;



Each argument is referenced and parsed by the $IFS or currently defined internal file separator. The default character is a space.



For example, take the following; # ./script.sh arg1 arg2



The argument list in that example is arg1 = $1 and arg2 = $2 which can be rewritten as arg1 arg2 = $@.



Another note is the use of a list of logs, how often does that change? My assumption is daily. Why not use the directory output as the array of your iterative loop? For example;



for i in $(ls /path/to/logs); do
./workaround.sh $i;
done


Or better yet, move on to use of functions in bash to eliminate clutter.



function process_file()
{
# transfer file code/command
}

function iterate_dir()
{
local -a dir=($(ls $1))
for file in ${dir[@]}; do
process_file $file
done
}

iterate_dir /path/to/log/for


While these are merely suggestions to improve your shell scripting knowledge I must know if there is an error you are getting and would also need to know the details of each scripts code and or functionality. Making the use of the -x argument helps debug scripting as well.



If you are simply transferring logs you may wish to do away with the scripts all together and make use of rsync, rsyslog or syslog as they all are much more suited for the task in question.






share|improve this answer

































    2














    inside the script : you need a read loop like while read ; do ......... ; done < filename to treat lines as $REPLY variable...



    for example



    while read 
    do
    mv $REPLY $REPLY.old
    done < liste.txt


    will rename any filename from inside liste.txt to filename.old



    you have the structure now you can adapt to your needs depending on what you mean by " in each log file's name in list.txt to be executed." :)






    share|improve this answer


























    • Can you please check my latest code?

      – The One
      Feb 22 '18 at 7:48



















    2














    xargs -n 1 ./script.sh <list.txt


    In this example, xargs will execute ./script.sh multiple times with a single argument read from its standard input.



    The standard input comes from the file list.txt via a simple shell input redirection. This assumes that the list.txt file contains one argument to use with the script per line.



    The ./script.sh script will be executed once for each line in the list.txt file.





    About the -n flag to xargs




    -n number



    Invoke utility using as many standard input arguments as
    possible, up to number (a positive decimal integer) arguments
    maximum.






    Another solution would be to allow the script to read directly from list.txt, or to take multiple command line argument (the entries from list.txt), and to download the files in one or several batches. But since we don't know what the mechanics of the script is, I can't make any detailed explanation of how to go about doing this.






    share|improve this answer


























    • You might want to explain the syntax you use a bit in your answer, since this seems like a bit of a newbie asking. Otherwise great answer, I'd say this is probably the best way to do it VS while read

      – Centimane
      Feb 22 '18 at 12:24











    • @Centimane Are you referring to the redirection, or to the semantics of -n 1? There's not much of syntax to explain...

      – Kusalananda
      Feb 22 '18 at 12:26











    • I would do both, but maybe that's just me. I'd quote the man page for the -n option and a sentence or two on the redirection.

      – Centimane
      Feb 22 '18 at 12:36



















    1














    (Note: I personally think Kusalananda's approach is the best in this specific scenario, so I'll just add some explanatory information and recommendations)





    Don't read lines with for



    Using that approach you:




    • Rely on word splitting to make things work.

    • Are subject of unintended side effects like globbing.

    • Slurp the entire file into memory all at once (may be a problem with big files).


    The while + read approach is preferable but it is not a bullet-proof solution:



    while IFS= read -r line; do
    # Your code here
    done < file


    Quote your variables



    Write "$i" instead of $i. Unquoted variables are probably the main source of bugs and security holes in shell scripts.



    Related questions:




    • Why does my shell script choke on whitespace or other special characters?

    • Security implications of forgetting to quote a variable in bash/POSIX shells


    Use $(...) instead of `...`



    $(...) is POSIX-compliant and can be nested easier.





    You could also modify your script to support multiple arguments and even a --batch option.



    If you don't know where to start, I have a sample/template script which supports those features (it uses Bash-specific syntax, though).



    Example of usage:



    $ cat list.txt
    list_item_1
    list_item_2
    list_item_3
    $ script --batch list.txt item_1 item_2
    Operands:
    1: [list_item_1]
    2: [list_item_2]
    3: [list_item_3]
    4: [item_1]
    5: [item_2]





    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',
      autoActivateHeartbeat: false,
      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%2f425828%2fhow-to-pass-in-multiple-arguments-to-execute-with-sh-script%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      The easiest method for reading arguments can be described as follows;



      Each argument is referenced and parsed by the $IFS or currently defined internal file separator. The default character is a space.



      For example, take the following; # ./script.sh arg1 arg2



      The argument list in that example is arg1 = $1 and arg2 = $2 which can be rewritten as arg1 arg2 = $@.



      Another note is the use of a list of logs, how often does that change? My assumption is daily. Why not use the directory output as the array of your iterative loop? For example;



      for i in $(ls /path/to/logs); do
      ./workaround.sh $i;
      done


      Or better yet, move on to use of functions in bash to eliminate clutter.



      function process_file()
      {
      # transfer file code/command
      }

      function iterate_dir()
      {
      local -a dir=($(ls $1))
      for file in ${dir[@]}; do
      process_file $file
      done
      }

      iterate_dir /path/to/log/for


      While these are merely suggestions to improve your shell scripting knowledge I must know if there is an error you are getting and would also need to know the details of each scripts code and or functionality. Making the use of the -x argument helps debug scripting as well.



      If you are simply transferring logs you may wish to do away with the scripts all together and make use of rsync, rsyslog or syslog as they all are much more suited for the task in question.






      share|improve this answer






























        1














        The easiest method for reading arguments can be described as follows;



        Each argument is referenced and parsed by the $IFS or currently defined internal file separator. The default character is a space.



        For example, take the following; # ./script.sh arg1 arg2



        The argument list in that example is arg1 = $1 and arg2 = $2 which can be rewritten as arg1 arg2 = $@.



        Another note is the use of a list of logs, how often does that change? My assumption is daily. Why not use the directory output as the array of your iterative loop? For example;



        for i in $(ls /path/to/logs); do
        ./workaround.sh $i;
        done


        Or better yet, move on to use of functions in bash to eliminate clutter.



        function process_file()
        {
        # transfer file code/command
        }

        function iterate_dir()
        {
        local -a dir=($(ls $1))
        for file in ${dir[@]}; do
        process_file $file
        done
        }

        iterate_dir /path/to/log/for


        While these are merely suggestions to improve your shell scripting knowledge I must know if there is an error you are getting and would also need to know the details of each scripts code and or functionality. Making the use of the -x argument helps debug scripting as well.



        If you are simply transferring logs you may wish to do away with the scripts all together and make use of rsync, rsyslog or syslog as they all are much more suited for the task in question.






        share|improve this answer




























          1












          1








          1







          The easiest method for reading arguments can be described as follows;



          Each argument is referenced and parsed by the $IFS or currently defined internal file separator. The default character is a space.



          For example, take the following; # ./script.sh arg1 arg2



          The argument list in that example is arg1 = $1 and arg2 = $2 which can be rewritten as arg1 arg2 = $@.



          Another note is the use of a list of logs, how often does that change? My assumption is daily. Why not use the directory output as the array of your iterative loop? For example;



          for i in $(ls /path/to/logs); do
          ./workaround.sh $i;
          done


          Or better yet, move on to use of functions in bash to eliminate clutter.



          function process_file()
          {
          # transfer file code/command
          }

          function iterate_dir()
          {
          local -a dir=($(ls $1))
          for file in ${dir[@]}; do
          process_file $file
          done
          }

          iterate_dir /path/to/log/for


          While these are merely suggestions to improve your shell scripting knowledge I must know if there is an error you are getting and would also need to know the details of each scripts code and or functionality. Making the use of the -x argument helps debug scripting as well.



          If you are simply transferring logs you may wish to do away with the scripts all together and make use of rsync, rsyslog or syslog as they all are much more suited for the task in question.






          share|improve this answer















          The easiest method for reading arguments can be described as follows;



          Each argument is referenced and parsed by the $IFS or currently defined internal file separator. The default character is a space.



          For example, take the following; # ./script.sh arg1 arg2



          The argument list in that example is arg1 = $1 and arg2 = $2 which can be rewritten as arg1 arg2 = $@.



          Another note is the use of a list of logs, how often does that change? My assumption is daily. Why not use the directory output as the array of your iterative loop? For example;



          for i in $(ls /path/to/logs); do
          ./workaround.sh $i;
          done


          Or better yet, move on to use of functions in bash to eliminate clutter.



          function process_file()
          {
          # transfer file code/command
          }

          function iterate_dir()
          {
          local -a dir=($(ls $1))
          for file in ${dir[@]}; do
          process_file $file
          done
          }

          iterate_dir /path/to/log/for


          While these are merely suggestions to improve your shell scripting knowledge I must know if there is an error you are getting and would also need to know the details of each scripts code and or functionality. Making the use of the -x argument helps debug scripting as well.



          If you are simply transferring logs you may wish to do away with the scripts all together and make use of rsync, rsyslog or syslog as they all are much more suited for the task in question.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 19 mins ago









          alo Malbarez

          36547




          36547










          answered Feb 22 '18 at 11:32









          jas-jas-

          72238




          72238

























              2














              inside the script : you need a read loop like while read ; do ......... ; done < filename to treat lines as $REPLY variable...



              for example



              while read 
              do
              mv $REPLY $REPLY.old
              done < liste.txt


              will rename any filename from inside liste.txt to filename.old



              you have the structure now you can adapt to your needs depending on what you mean by " in each log file's name in list.txt to be executed." :)






              share|improve this answer


























              • Can you please check my latest code?

                – The One
                Feb 22 '18 at 7:48
















              2














              inside the script : you need a read loop like while read ; do ......... ; done < filename to treat lines as $REPLY variable...



              for example



              while read 
              do
              mv $REPLY $REPLY.old
              done < liste.txt


              will rename any filename from inside liste.txt to filename.old



              you have the structure now you can adapt to your needs depending on what you mean by " in each log file's name in list.txt to be executed." :)






              share|improve this answer


























              • Can you please check my latest code?

                – The One
                Feb 22 '18 at 7:48














              2












              2








              2







              inside the script : you need a read loop like while read ; do ......... ; done < filename to treat lines as $REPLY variable...



              for example



              while read 
              do
              mv $REPLY $REPLY.old
              done < liste.txt


              will rename any filename from inside liste.txt to filename.old



              you have the structure now you can adapt to your needs depending on what you mean by " in each log file's name in list.txt to be executed." :)






              share|improve this answer















              inside the script : you need a read loop like while read ; do ......... ; done < filename to treat lines as $REPLY variable...



              for example



              while read 
              do
              mv $REPLY $REPLY.old
              done < liste.txt


              will rename any filename from inside liste.txt to filename.old



              you have the structure now you can adapt to your needs depending on what you mean by " in each log file's name in list.txt to be executed." :)







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Feb 22 '18 at 7:47

























              answered Feb 22 '18 at 7:33









              francois Pfrancois P

              954214




              954214













              • Can you please check my latest code?

                – The One
                Feb 22 '18 at 7:48



















              • Can you please check my latest code?

                – The One
                Feb 22 '18 at 7:48

















              Can you please check my latest code?

              – The One
              Feb 22 '18 at 7:48





              Can you please check my latest code?

              – The One
              Feb 22 '18 at 7:48











              2














              xargs -n 1 ./script.sh <list.txt


              In this example, xargs will execute ./script.sh multiple times with a single argument read from its standard input.



              The standard input comes from the file list.txt via a simple shell input redirection. This assumes that the list.txt file contains one argument to use with the script per line.



              The ./script.sh script will be executed once for each line in the list.txt file.





              About the -n flag to xargs




              -n number



              Invoke utility using as many standard input arguments as
              possible, up to number (a positive decimal integer) arguments
              maximum.






              Another solution would be to allow the script to read directly from list.txt, or to take multiple command line argument (the entries from list.txt), and to download the files in one or several batches. But since we don't know what the mechanics of the script is, I can't make any detailed explanation of how to go about doing this.






              share|improve this answer


























              • You might want to explain the syntax you use a bit in your answer, since this seems like a bit of a newbie asking. Otherwise great answer, I'd say this is probably the best way to do it VS while read

                – Centimane
                Feb 22 '18 at 12:24











              • @Centimane Are you referring to the redirection, or to the semantics of -n 1? There's not much of syntax to explain...

                – Kusalananda
                Feb 22 '18 at 12:26











              • I would do both, but maybe that's just me. I'd quote the man page for the -n option and a sentence or two on the redirection.

                – Centimane
                Feb 22 '18 at 12:36
















              2














              xargs -n 1 ./script.sh <list.txt


              In this example, xargs will execute ./script.sh multiple times with a single argument read from its standard input.



              The standard input comes from the file list.txt via a simple shell input redirection. This assumes that the list.txt file contains one argument to use with the script per line.



              The ./script.sh script will be executed once for each line in the list.txt file.





              About the -n flag to xargs




              -n number



              Invoke utility using as many standard input arguments as
              possible, up to number (a positive decimal integer) arguments
              maximum.






              Another solution would be to allow the script to read directly from list.txt, or to take multiple command line argument (the entries from list.txt), and to download the files in one or several batches. But since we don't know what the mechanics of the script is, I can't make any detailed explanation of how to go about doing this.






              share|improve this answer


























              • You might want to explain the syntax you use a bit in your answer, since this seems like a bit of a newbie asking. Otherwise great answer, I'd say this is probably the best way to do it VS while read

                – Centimane
                Feb 22 '18 at 12:24











              • @Centimane Are you referring to the redirection, or to the semantics of -n 1? There's not much of syntax to explain...

                – Kusalananda
                Feb 22 '18 at 12:26











              • I would do both, but maybe that's just me. I'd quote the man page for the -n option and a sentence or two on the redirection.

                – Centimane
                Feb 22 '18 at 12:36














              2












              2








              2







              xargs -n 1 ./script.sh <list.txt


              In this example, xargs will execute ./script.sh multiple times with a single argument read from its standard input.



              The standard input comes from the file list.txt via a simple shell input redirection. This assumes that the list.txt file contains one argument to use with the script per line.



              The ./script.sh script will be executed once for each line in the list.txt file.





              About the -n flag to xargs




              -n number



              Invoke utility using as many standard input arguments as
              possible, up to number (a positive decimal integer) arguments
              maximum.






              Another solution would be to allow the script to read directly from list.txt, or to take multiple command line argument (the entries from list.txt), and to download the files in one or several batches. But since we don't know what the mechanics of the script is, I can't make any detailed explanation of how to go about doing this.






              share|improve this answer















              xargs -n 1 ./script.sh <list.txt


              In this example, xargs will execute ./script.sh multiple times with a single argument read from its standard input.



              The standard input comes from the file list.txt via a simple shell input redirection. This assumes that the list.txt file contains one argument to use with the script per line.



              The ./script.sh script will be executed once for each line in the list.txt file.





              About the -n flag to xargs




              -n number



              Invoke utility using as many standard input arguments as
              possible, up to number (a positive decimal integer) arguments
              maximum.






              Another solution would be to allow the script to read directly from list.txt, or to take multiple command line argument (the entries from list.txt), and to download the files in one or several batches. But since we don't know what the mechanics of the script is, I can't make any detailed explanation of how to go about doing this.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Feb 22 '18 at 12:39

























              answered Feb 22 '18 at 11:48









              KusalanandaKusalananda

              133k17253416




              133k17253416













              • You might want to explain the syntax you use a bit in your answer, since this seems like a bit of a newbie asking. Otherwise great answer, I'd say this is probably the best way to do it VS while read

                – Centimane
                Feb 22 '18 at 12:24











              • @Centimane Are you referring to the redirection, or to the semantics of -n 1? There's not much of syntax to explain...

                – Kusalananda
                Feb 22 '18 at 12:26











              • I would do both, but maybe that's just me. I'd quote the man page for the -n option and a sentence or two on the redirection.

                – Centimane
                Feb 22 '18 at 12:36



















              • You might want to explain the syntax you use a bit in your answer, since this seems like a bit of a newbie asking. Otherwise great answer, I'd say this is probably the best way to do it VS while read

                – Centimane
                Feb 22 '18 at 12:24











              • @Centimane Are you referring to the redirection, or to the semantics of -n 1? There's not much of syntax to explain...

                – Kusalananda
                Feb 22 '18 at 12:26











              • I would do both, but maybe that's just me. I'd quote the man page for the -n option and a sentence or two on the redirection.

                – Centimane
                Feb 22 '18 at 12:36

















              You might want to explain the syntax you use a bit in your answer, since this seems like a bit of a newbie asking. Otherwise great answer, I'd say this is probably the best way to do it VS while read

              – Centimane
              Feb 22 '18 at 12:24





              You might want to explain the syntax you use a bit in your answer, since this seems like a bit of a newbie asking. Otherwise great answer, I'd say this is probably the best way to do it VS while read

              – Centimane
              Feb 22 '18 at 12:24













              @Centimane Are you referring to the redirection, or to the semantics of -n 1? There's not much of syntax to explain...

              – Kusalananda
              Feb 22 '18 at 12:26





              @Centimane Are you referring to the redirection, or to the semantics of -n 1? There's not much of syntax to explain...

              – Kusalananda
              Feb 22 '18 at 12:26













              I would do both, but maybe that's just me. I'd quote the man page for the -n option and a sentence or two on the redirection.

              – Centimane
              Feb 22 '18 at 12:36





              I would do both, but maybe that's just me. I'd quote the man page for the -n option and a sentence or two on the redirection.

              – Centimane
              Feb 22 '18 at 12:36











              1














              (Note: I personally think Kusalananda's approach is the best in this specific scenario, so I'll just add some explanatory information and recommendations)





              Don't read lines with for



              Using that approach you:




              • Rely on word splitting to make things work.

              • Are subject of unintended side effects like globbing.

              • Slurp the entire file into memory all at once (may be a problem with big files).


              The while + read approach is preferable but it is not a bullet-proof solution:



              while IFS= read -r line; do
              # Your code here
              done < file


              Quote your variables



              Write "$i" instead of $i. Unquoted variables are probably the main source of bugs and security holes in shell scripts.



              Related questions:




              • Why does my shell script choke on whitespace or other special characters?

              • Security implications of forgetting to quote a variable in bash/POSIX shells


              Use $(...) instead of `...`



              $(...) is POSIX-compliant and can be nested easier.





              You could also modify your script to support multiple arguments and even a --batch option.



              If you don't know where to start, I have a sample/template script which supports those features (it uses Bash-specific syntax, though).



              Example of usage:



              $ cat list.txt
              list_item_1
              list_item_2
              list_item_3
              $ script --batch list.txt item_1 item_2
              Operands:
              1: [list_item_1]
              2: [list_item_2]
              3: [list_item_3]
              4: [item_1]
              5: [item_2]





              share|improve this answer




























                1














                (Note: I personally think Kusalananda's approach is the best in this specific scenario, so I'll just add some explanatory information and recommendations)





                Don't read lines with for



                Using that approach you:




                • Rely on word splitting to make things work.

                • Are subject of unintended side effects like globbing.

                • Slurp the entire file into memory all at once (may be a problem with big files).


                The while + read approach is preferable but it is not a bullet-proof solution:



                while IFS= read -r line; do
                # Your code here
                done < file


                Quote your variables



                Write "$i" instead of $i. Unquoted variables are probably the main source of bugs and security holes in shell scripts.



                Related questions:




                • Why does my shell script choke on whitespace or other special characters?

                • Security implications of forgetting to quote a variable in bash/POSIX shells


                Use $(...) instead of `...`



                $(...) is POSIX-compliant and can be nested easier.





                You could also modify your script to support multiple arguments and even a --batch option.



                If you don't know where to start, I have a sample/template script which supports those features (it uses Bash-specific syntax, though).



                Example of usage:



                $ cat list.txt
                list_item_1
                list_item_2
                list_item_3
                $ script --batch list.txt item_1 item_2
                Operands:
                1: [list_item_1]
                2: [list_item_2]
                3: [list_item_3]
                4: [item_1]
                5: [item_2]





                share|improve this answer


























                  1












                  1








                  1







                  (Note: I personally think Kusalananda's approach is the best in this specific scenario, so I'll just add some explanatory information and recommendations)





                  Don't read lines with for



                  Using that approach you:




                  • Rely on word splitting to make things work.

                  • Are subject of unintended side effects like globbing.

                  • Slurp the entire file into memory all at once (may be a problem with big files).


                  The while + read approach is preferable but it is not a bullet-proof solution:



                  while IFS= read -r line; do
                  # Your code here
                  done < file


                  Quote your variables



                  Write "$i" instead of $i. Unquoted variables are probably the main source of bugs and security holes in shell scripts.



                  Related questions:




                  • Why does my shell script choke on whitespace or other special characters?

                  • Security implications of forgetting to quote a variable in bash/POSIX shells


                  Use $(...) instead of `...`



                  $(...) is POSIX-compliant and can be nested easier.





                  You could also modify your script to support multiple arguments and even a --batch option.



                  If you don't know where to start, I have a sample/template script which supports those features (it uses Bash-specific syntax, though).



                  Example of usage:



                  $ cat list.txt
                  list_item_1
                  list_item_2
                  list_item_3
                  $ script --batch list.txt item_1 item_2
                  Operands:
                  1: [list_item_1]
                  2: [list_item_2]
                  3: [list_item_3]
                  4: [item_1]
                  5: [item_2]





                  share|improve this answer













                  (Note: I personally think Kusalananda's approach is the best in this specific scenario, so I'll just add some explanatory information and recommendations)





                  Don't read lines with for



                  Using that approach you:




                  • Rely on word splitting to make things work.

                  • Are subject of unintended side effects like globbing.

                  • Slurp the entire file into memory all at once (may be a problem with big files).


                  The while + read approach is preferable but it is not a bullet-proof solution:



                  while IFS= read -r line; do
                  # Your code here
                  done < file


                  Quote your variables



                  Write "$i" instead of $i. Unquoted variables are probably the main source of bugs and security holes in shell scripts.



                  Related questions:




                  • Why does my shell script choke on whitespace or other special characters?

                  • Security implications of forgetting to quote a variable in bash/POSIX shells


                  Use $(...) instead of `...`



                  $(...) is POSIX-compliant and can be nested easier.





                  You could also modify your script to support multiple arguments and even a --batch option.



                  If you don't know where to start, I have a sample/template script which supports those features (it uses Bash-specific syntax, though).



                  Example of usage:



                  $ cat list.txt
                  list_item_1
                  list_item_2
                  list_item_3
                  $ script --batch list.txt item_1 item_2
                  Operands:
                  1: [list_item_1]
                  2: [list_item_2]
                  3: [list_item_3]
                  4: [item_1]
                  5: [item_2]






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Feb 22 '18 at 13:45









                  nxnevnxnev

                  2,9072423




                  2,9072423






























                      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.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f425828%2fhow-to-pass-in-multiple-arguments-to-execute-with-sh-script%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 rewrite equation of hyperbola in standard form

                      No ethernet ip address in my vocore2