Using source command in script but defining the input file in the command line of a terminal
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
New contributor
user324432 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
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
New contributor
user324432 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
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
New contributor
user324432 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
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
New contributor
user324432 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
user324432 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited Dec 5 at 17:18
Tomasz
8,85852963
8,85852963
New contributor
user324432 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked Dec 5 at 17:10
user324432
1
1
New contributor
user324432 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
user324432 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
user324432 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
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 toreadthe 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 |
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 toreadthe 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 toreadthe 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,6282423
2,6282423
1
Another idea would be toreadthe 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 toreadthe 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
8,85852963
8,85852963
add a comment |
add a comment |
user324432 is a new contributor. Be nice, and check out our Code of Conduct.
user324432 is a new contributor. Be nice, and check out our Code of Conduct.
user324432 is a new contributor. Be nice, and check out our Code of Conduct.
user324432 is a new contributor. Be nice, and check out our Code of Conduct.
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