Using source command in script but defining the input file in the command line of a terminal
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
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
add a comment |
up vote
0
down vote
favorite
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
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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
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
bash shell-script scripting source
edited Dec 5 at 17:18
Tomasz
9,16852964
9,16852964
asked Dec 5 at 17:10
user324432
1
1
add a comment |
add a comment |
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
1
Another idea would be toread
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
add a comment |
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.
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
1
Another idea would be toread
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
add a comment |
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
1
Another idea would be toread
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
add a comment |
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
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
edited Dec 5 at 17:42
answered Dec 5 at 17:32
nxnev
2,7122423
2,7122423
1
Another idea would be toread
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
add a comment |
1
Another idea would be toread
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Dec 5 at 17:18
Tomasz
9,16852964
9,16852964
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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