Using source command in script but defining the input file in the command line of a terminal

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











up vote
0
down vote

favorite
1












I have a script at the moment that reads in variables and then operates on them like the following;



#!bin/bash
a=10
b=15
c=20

d=a*b+c
echo $d


However I would like to split this up into an input file containing:



a=10
b=15
c=20


and a script which does the operation



#!/bin/bash
d=a*b+c
echo $d


And will be called up something like this.



./script.sh < input.in


Now I have done a bit of digging and I have tried doing a simple.



./script.sh < input.in


such that it returns the answer 170



but this doesn't work. After further digging, it seems that in the script in need to use the command "source" But I am unsure how to do it in this case.



Can it be done? What is the best way to do this?










share|improve this question



























    up vote
    0
    down vote

    favorite
    1












    I have a script at the moment that reads in variables and then operates on them like the following;



    #!bin/bash
    a=10
    b=15
    c=20

    d=a*b+c
    echo $d


    However I would like to split this up into an input file containing:



    a=10
    b=15
    c=20


    and a script which does the operation



    #!/bin/bash
    d=a*b+c
    echo $d


    And will be called up something like this.



    ./script.sh < input.in


    Now I have done a bit of digging and I have tried doing a simple.



    ./script.sh < input.in


    such that it returns the answer 170



    but this doesn't work. After further digging, it seems that in the script in need to use the command "source" But I am unsure how to do it in this case.



    Can it be done? What is the best way to do this?










    share|improve this question

























      up vote
      0
      down vote

      favorite
      1









      up vote
      0
      down vote

      favorite
      1






      1





      I have a script at the moment that reads in variables and then operates on them like the following;



      #!bin/bash
      a=10
      b=15
      c=20

      d=a*b+c
      echo $d


      However I would like to split this up into an input file containing:



      a=10
      b=15
      c=20


      and a script which does the operation



      #!/bin/bash
      d=a*b+c
      echo $d


      And will be called up something like this.



      ./script.sh < input.in


      Now I have done a bit of digging and I have tried doing a simple.



      ./script.sh < input.in


      such that it returns the answer 170



      but this doesn't work. After further digging, it seems that in the script in need to use the command "source" But I am unsure how to do it in this case.



      Can it be done? What is the best way to do this?










      share|improve this question















      I have a script at the moment that reads in variables and then operates on them like the following;



      #!bin/bash
      a=10
      b=15
      c=20

      d=a*b+c
      echo $d


      However I would like to split this up into an input file containing:



      a=10
      b=15
      c=20


      and a script which does the operation



      #!/bin/bash
      d=a*b+c
      echo $d


      And will be called up something like this.



      ./script.sh < input.in


      Now I have done a bit of digging and I have tried doing a simple.



      ./script.sh < input.in


      such that it returns the answer 170



      but this doesn't work. After further digging, it seems that in the script in need to use the command "source" But I am unsure how to do it in this case.



      Can it be done? What is the best way to do this?







      bash shell-script scripting source






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 5 at 17:18









      Tomasz

      9,16852964




      9,16852964










      asked Dec 5 at 17:10









      user324432

      1




      1




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          3
          down vote















          From help source:



          source: source filename [arguments]
          Execute commands from a file in the current shell.

          Read and execute commands from FILENAME in the current shell. The
          entries in $PATH are used to find the directory containing FILENAME.
          If any ARGUMENTS are supplied, they become the positional parameters
          when FILENAME is executed.

          Exit Status:
          Returns the status of the last command executed in FILENAME; fails if
          FILENAME cannot be read.


          So, in the script, you only need to add this line:



          source input.in


          or this one (the POSIX version):



          . input.in


          To pass the input file at runtime, you use positional parameters:



          source "$1"




          . "$1"


          Also note that d=a*b+c won't work unless d has the "integer" attribute:



          declare -i d
          d=a*b+c


          or you use arithmetic expansion to perform the operation:



          d=$((a*b+c))


          Example:



          #!/bin/bash
          source "$1"
          d=$((a*b+c))
          echo "$d"




          $ ./script.sh input.in
          170





          share|improve this answer


















          • 1




            Another idea would be to read the values of the variables in the script, and then to pass (on standard input) a file containing only those values.
            – Kusalananda
            Dec 5 at 17:56


















          up vote
          0
          down vote













          This is a very basic operation. Just source or . a file name, and the result is as if those lines were included in your primary script.



          The syntax is simply this:



          source file_name


          or



          . file_name


          There are some nuances, but that's more advanced. See man bash | grep source for more details.






          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%2f486197%2fusing-source-command-in-script-but-defining-the-input-file-in-the-command-line-o%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















            From help source:



            source: source filename [arguments]
            Execute commands from a file in the current shell.

            Read and execute commands from FILENAME in the current shell. The
            entries in $PATH are used to find the directory containing FILENAME.
            If any ARGUMENTS are supplied, they become the positional parameters
            when FILENAME is executed.

            Exit Status:
            Returns the status of the last command executed in FILENAME; fails if
            FILENAME cannot be read.


            So, in the script, you only need to add this line:



            source input.in


            or this one (the POSIX version):



            . input.in


            To pass the input file at runtime, you use positional parameters:



            source "$1"




            . "$1"


            Also note that d=a*b+c won't work unless d has the "integer" attribute:



            declare -i d
            d=a*b+c


            or you use arithmetic expansion to perform the operation:



            d=$((a*b+c))


            Example:



            #!/bin/bash
            source "$1"
            d=$((a*b+c))
            echo "$d"




            $ ./script.sh input.in
            170





            share|improve this answer


















            • 1




              Another idea would be to read the values of the variables in the script, and then to pass (on standard input) a file containing only those values.
              – Kusalananda
              Dec 5 at 17:56















            up vote
            3
            down vote















            From help source:



            source: source filename [arguments]
            Execute commands from a file in the current shell.

            Read and execute commands from FILENAME in the current shell. The
            entries in $PATH are used to find the directory containing FILENAME.
            If any ARGUMENTS are supplied, they become the positional parameters
            when FILENAME is executed.

            Exit Status:
            Returns the status of the last command executed in FILENAME; fails if
            FILENAME cannot be read.


            So, in the script, you only need to add this line:



            source input.in


            or this one (the POSIX version):



            . input.in


            To pass the input file at runtime, you use positional parameters:



            source "$1"




            . "$1"


            Also note that d=a*b+c won't work unless d has the "integer" attribute:



            declare -i d
            d=a*b+c


            or you use arithmetic expansion to perform the operation:



            d=$((a*b+c))


            Example:



            #!/bin/bash
            source "$1"
            d=$((a*b+c))
            echo "$d"




            $ ./script.sh input.in
            170





            share|improve this answer


















            • 1




              Another idea would be to read the values of the variables in the script, and then to pass (on standard input) a file containing only those values.
              – Kusalananda
              Dec 5 at 17:56













            up vote
            3
            down vote










            up vote
            3
            down vote











            From help source:



            source: source filename [arguments]
            Execute commands from a file in the current shell.

            Read and execute commands from FILENAME in the current shell. The
            entries in $PATH are used to find the directory containing FILENAME.
            If any ARGUMENTS are supplied, they become the positional parameters
            when FILENAME is executed.

            Exit Status:
            Returns the status of the last command executed in FILENAME; fails if
            FILENAME cannot be read.


            So, in the script, you only need to add this line:



            source input.in


            or this one (the POSIX version):



            . input.in


            To pass the input file at runtime, you use positional parameters:



            source "$1"




            . "$1"


            Also note that d=a*b+c won't work unless d has the "integer" attribute:



            declare -i d
            d=a*b+c


            or you use arithmetic expansion to perform the operation:



            d=$((a*b+c))


            Example:



            #!/bin/bash
            source "$1"
            d=$((a*b+c))
            echo "$d"




            $ ./script.sh input.in
            170





            share|improve this answer
















            From help source:



            source: source filename [arguments]
            Execute commands from a file in the current shell.

            Read and execute commands from FILENAME in the current shell. The
            entries in $PATH are used to find the directory containing FILENAME.
            If any ARGUMENTS are supplied, they become the positional parameters
            when FILENAME is executed.

            Exit Status:
            Returns the status of the last command executed in FILENAME; fails if
            FILENAME cannot be read.


            So, in the script, you only need to add this line:



            source input.in


            or this one (the POSIX version):



            . input.in


            To pass the input file at runtime, you use positional parameters:



            source "$1"




            . "$1"


            Also note that d=a*b+c won't work unless d has the "integer" attribute:



            declare -i d
            d=a*b+c


            or you use arithmetic expansion to perform the operation:



            d=$((a*b+c))


            Example:



            #!/bin/bash
            source "$1"
            d=$((a*b+c))
            echo "$d"




            $ ./script.sh input.in
            170






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Dec 5 at 17:42

























            answered Dec 5 at 17:32









            nxnev

            2,7122423




            2,7122423







            • 1




              Another idea would be to read the values of the variables in the script, and then to pass (on standard input) a file containing only those values.
              – Kusalananda
              Dec 5 at 17:56













            • 1




              Another idea would be to read the values of the variables in the script, and then to pass (on standard input) a file containing only those values.
              – Kusalananda
              Dec 5 at 17:56








            1




            1




            Another idea would be to read the values of the variables in the script, and then to pass (on standard input) a file containing only those values.
            – Kusalananda
            Dec 5 at 17:56





            Another idea would be to read the values of the variables in the script, and then to pass (on standard input) a file containing only those values.
            – Kusalananda
            Dec 5 at 17:56













            up vote
            0
            down vote













            This is a very basic operation. Just source or . a file name, and the result is as if those lines were included in your primary script.



            The syntax is simply this:



            source file_name


            or



            . file_name


            There are some nuances, but that's more advanced. See man bash | grep source for more details.






            share|improve this answer
























              up vote
              0
              down vote













              This is a very basic operation. Just source or . a file name, and the result is as if those lines were included in your primary script.



              The syntax is simply this:



              source file_name


              or



              . file_name


              There are some nuances, but that's more advanced. See man bash | grep source for more details.






              share|improve this answer






















                up vote
                0
                down vote










                up vote
                0
                down vote









                This is a very basic operation. Just source or . a file name, and the result is as if those lines were included in your primary script.



                The syntax is simply this:



                source file_name


                or



                . file_name


                There are some nuances, but that's more advanced. See man bash | grep source for more details.






                share|improve this answer












                This is a very basic operation. Just source or . a file name, and the result is as if those lines were included in your primary script.



                The syntax is simply this:



                source file_name


                or



                . file_name


                There are some nuances, but that's more advanced. See man bash | grep source for more details.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Dec 5 at 17:18









                Tomasz

                9,16852964




                9,16852964



























                    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%2f486197%2fusing-source-command-in-script-but-defining-the-input-file-in-the-command-line-o%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?

                    Bahrain

                    Postfix configuration issue with fips on centos 7; mailgun relay