shell code change variable on call of code
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
i wrote little shell code but i need to change variable while calling code how to do it
Example
./shell.sh --varaible="New Value"
should output
New Value,Some Value
shell.sh
variable="Old Value"
variable2="Some Value"
echo "$variable,$variable2"
shell
add a comment |Â
up vote
0
down vote
favorite
i wrote little shell code but i need to change variable while calling code how to do it
Example
./shell.sh --varaible="New Value"
should output
New Value,Some Value
shell.sh
variable="Old Value"
variable2="Some Value"
echo "$variable,$variable2"
shell
1
I'm not sure if the code formatting I put in is accurate to what you intended - you can edit the question yourself to correct it.
â Michael Homer
Feb 17 at 9:06
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
i wrote little shell code but i need to change variable while calling code how to do it
Example
./shell.sh --varaible="New Value"
should output
New Value,Some Value
shell.sh
variable="Old Value"
variable2="Some Value"
echo "$variable,$variable2"
shell
i wrote little shell code but i need to change variable while calling code how to do it
Example
./shell.sh --varaible="New Value"
should output
New Value,Some Value
shell.sh
variable="Old Value"
variable2="Some Value"
echo "$variable,$variable2"
shell
edited Feb 17 at 9:05
Michael Homer
42.5k6108148
42.5k6108148
asked Feb 17 at 9:04
evenom
31
31
1
I'm not sure if the code formatting I put in is accurate to what you intended - you can edit the question yourself to correct it.
â Michael Homer
Feb 17 at 9:06
add a comment |Â
1
I'm not sure if the code formatting I put in is accurate to what you intended - you can edit the question yourself to correct it.
â Michael Homer
Feb 17 at 9:06
1
1
I'm not sure if the code formatting I put in is accurate to what you intended - you can edit the question yourself to correct it.
â Michael Homer
Feb 17 at 9:06
I'm not sure if the code formatting I put in is accurate to what you intended - you can edit the question yourself to correct it.
â Michael Homer
Feb 17 at 9:06
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
3
down vote
accepted
It is not possible to change the value of a variable in a script from the command line if the script is explicitly setting the value of that variable.
You may, however, set the value of the variable in such a way that it takes the command line arguments into account.
The arguments given on the command line will be available to the shell script in the positional parameters $1
, $2
, $3
etc.
To set a variable to the first positional parameter, you would use
variable="$1"
So, your script might look like
#!/bin/sh
variable="$1"
variable2="Some Value"
echo "$variable,$variable2" # or printf '%s,%sn' "$variable" "$variable2"
... and would be called with
$ ./script.sh "New Value"
The output would be
New Value,Some Value
To provide a default value for a variable, you may do
variable="$1:-Old Value"
Using this in the above script, the output will say Old Value,Some Value
if the script is not called with any command line arguments.
The following short script does proper command line parsing of three command line flags, -a
, -b
and -c
. The flags -a
and -b
takes an argument while -c
does not. It uses three variables, var_a
, var_b
and var_c
, that have default values that may be overridden by using the command line flags.
At the end, the script displays the values of these variables, and also displays what else might have been given on the command line.
#!/bin/sh
var_a=12
var_b="yellow"
var_c=0
while getopts 'a:b:c' opt; do
case "$opt" in
a) var_a=$OPTARG ;;
b) var_b=$OPTARG ;;
c) var_c=1 ;;
*) echo 'command line parsing error' >&2
exit 1
esac
done
shift $(( OPTIND - 1 ))
printf 'var_a = %snvar_b = %snvar_c = %sn'
"$var_a" "$var_b" "$var_c"
if [ "$#" -gt 0 ]; then
echo 'Other operands:'
printf '%sn' "$@"
fi
Running it a few times:
$ ./script.sh
var_a = 12
var_b = yellow
var_c = 0
$ ./script.sh -b "green tea" "hello world"
var_a = 12
var_b = green tea
var_c = 0
Other operands:
hello world
$ ./script.sh -c my other car is a cdr
var_a = 12
var_b = yellow
var_c = 1
Other operands:
my
other
car
is
a
cdr
$ ./script.sh -a -d
var_a = -d
var_b = yellow
var_c = 0
$ ./script.sh -a
./script.sh[15]: -`a' requires argument
command line parsing error
$ ./script.sh -a "boo!" "help!" -c
var_a = boo!
var_b = yellow
var_c = 0
Other operands:
help!
-c
Thanks a lot, got where i needed with your help. Great advise.
â evenom
Feb 21 at 21:01
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
It is not possible to change the value of a variable in a script from the command line if the script is explicitly setting the value of that variable.
You may, however, set the value of the variable in such a way that it takes the command line arguments into account.
The arguments given on the command line will be available to the shell script in the positional parameters $1
, $2
, $3
etc.
To set a variable to the first positional parameter, you would use
variable="$1"
So, your script might look like
#!/bin/sh
variable="$1"
variable2="Some Value"
echo "$variable,$variable2" # or printf '%s,%sn' "$variable" "$variable2"
... and would be called with
$ ./script.sh "New Value"
The output would be
New Value,Some Value
To provide a default value for a variable, you may do
variable="$1:-Old Value"
Using this in the above script, the output will say Old Value,Some Value
if the script is not called with any command line arguments.
The following short script does proper command line parsing of three command line flags, -a
, -b
and -c
. The flags -a
and -b
takes an argument while -c
does not. It uses three variables, var_a
, var_b
and var_c
, that have default values that may be overridden by using the command line flags.
At the end, the script displays the values of these variables, and also displays what else might have been given on the command line.
#!/bin/sh
var_a=12
var_b="yellow"
var_c=0
while getopts 'a:b:c' opt; do
case "$opt" in
a) var_a=$OPTARG ;;
b) var_b=$OPTARG ;;
c) var_c=1 ;;
*) echo 'command line parsing error' >&2
exit 1
esac
done
shift $(( OPTIND - 1 ))
printf 'var_a = %snvar_b = %snvar_c = %sn'
"$var_a" "$var_b" "$var_c"
if [ "$#" -gt 0 ]; then
echo 'Other operands:'
printf '%sn' "$@"
fi
Running it a few times:
$ ./script.sh
var_a = 12
var_b = yellow
var_c = 0
$ ./script.sh -b "green tea" "hello world"
var_a = 12
var_b = green tea
var_c = 0
Other operands:
hello world
$ ./script.sh -c my other car is a cdr
var_a = 12
var_b = yellow
var_c = 1
Other operands:
my
other
car
is
a
cdr
$ ./script.sh -a -d
var_a = -d
var_b = yellow
var_c = 0
$ ./script.sh -a
./script.sh[15]: -`a' requires argument
command line parsing error
$ ./script.sh -a "boo!" "help!" -c
var_a = boo!
var_b = yellow
var_c = 0
Other operands:
help!
-c
Thanks a lot, got where i needed with your help. Great advise.
â evenom
Feb 21 at 21:01
add a comment |Â
up vote
3
down vote
accepted
It is not possible to change the value of a variable in a script from the command line if the script is explicitly setting the value of that variable.
You may, however, set the value of the variable in such a way that it takes the command line arguments into account.
The arguments given on the command line will be available to the shell script in the positional parameters $1
, $2
, $3
etc.
To set a variable to the first positional parameter, you would use
variable="$1"
So, your script might look like
#!/bin/sh
variable="$1"
variable2="Some Value"
echo "$variable,$variable2" # or printf '%s,%sn' "$variable" "$variable2"
... and would be called with
$ ./script.sh "New Value"
The output would be
New Value,Some Value
To provide a default value for a variable, you may do
variable="$1:-Old Value"
Using this in the above script, the output will say Old Value,Some Value
if the script is not called with any command line arguments.
The following short script does proper command line parsing of three command line flags, -a
, -b
and -c
. The flags -a
and -b
takes an argument while -c
does not. It uses three variables, var_a
, var_b
and var_c
, that have default values that may be overridden by using the command line flags.
At the end, the script displays the values of these variables, and also displays what else might have been given on the command line.
#!/bin/sh
var_a=12
var_b="yellow"
var_c=0
while getopts 'a:b:c' opt; do
case "$opt" in
a) var_a=$OPTARG ;;
b) var_b=$OPTARG ;;
c) var_c=1 ;;
*) echo 'command line parsing error' >&2
exit 1
esac
done
shift $(( OPTIND - 1 ))
printf 'var_a = %snvar_b = %snvar_c = %sn'
"$var_a" "$var_b" "$var_c"
if [ "$#" -gt 0 ]; then
echo 'Other operands:'
printf '%sn' "$@"
fi
Running it a few times:
$ ./script.sh
var_a = 12
var_b = yellow
var_c = 0
$ ./script.sh -b "green tea" "hello world"
var_a = 12
var_b = green tea
var_c = 0
Other operands:
hello world
$ ./script.sh -c my other car is a cdr
var_a = 12
var_b = yellow
var_c = 1
Other operands:
my
other
car
is
a
cdr
$ ./script.sh -a -d
var_a = -d
var_b = yellow
var_c = 0
$ ./script.sh -a
./script.sh[15]: -`a' requires argument
command line parsing error
$ ./script.sh -a "boo!" "help!" -c
var_a = boo!
var_b = yellow
var_c = 0
Other operands:
help!
-c
Thanks a lot, got where i needed with your help. Great advise.
â evenom
Feb 21 at 21:01
add a comment |Â
up vote
3
down vote
accepted
up vote
3
down vote
accepted
It is not possible to change the value of a variable in a script from the command line if the script is explicitly setting the value of that variable.
You may, however, set the value of the variable in such a way that it takes the command line arguments into account.
The arguments given on the command line will be available to the shell script in the positional parameters $1
, $2
, $3
etc.
To set a variable to the first positional parameter, you would use
variable="$1"
So, your script might look like
#!/bin/sh
variable="$1"
variable2="Some Value"
echo "$variable,$variable2" # or printf '%s,%sn' "$variable" "$variable2"
... and would be called with
$ ./script.sh "New Value"
The output would be
New Value,Some Value
To provide a default value for a variable, you may do
variable="$1:-Old Value"
Using this in the above script, the output will say Old Value,Some Value
if the script is not called with any command line arguments.
The following short script does proper command line parsing of three command line flags, -a
, -b
and -c
. The flags -a
and -b
takes an argument while -c
does not. It uses three variables, var_a
, var_b
and var_c
, that have default values that may be overridden by using the command line flags.
At the end, the script displays the values of these variables, and also displays what else might have been given on the command line.
#!/bin/sh
var_a=12
var_b="yellow"
var_c=0
while getopts 'a:b:c' opt; do
case "$opt" in
a) var_a=$OPTARG ;;
b) var_b=$OPTARG ;;
c) var_c=1 ;;
*) echo 'command line parsing error' >&2
exit 1
esac
done
shift $(( OPTIND - 1 ))
printf 'var_a = %snvar_b = %snvar_c = %sn'
"$var_a" "$var_b" "$var_c"
if [ "$#" -gt 0 ]; then
echo 'Other operands:'
printf '%sn' "$@"
fi
Running it a few times:
$ ./script.sh
var_a = 12
var_b = yellow
var_c = 0
$ ./script.sh -b "green tea" "hello world"
var_a = 12
var_b = green tea
var_c = 0
Other operands:
hello world
$ ./script.sh -c my other car is a cdr
var_a = 12
var_b = yellow
var_c = 1
Other operands:
my
other
car
is
a
cdr
$ ./script.sh -a -d
var_a = -d
var_b = yellow
var_c = 0
$ ./script.sh -a
./script.sh[15]: -`a' requires argument
command line parsing error
$ ./script.sh -a "boo!" "help!" -c
var_a = boo!
var_b = yellow
var_c = 0
Other operands:
help!
-c
It is not possible to change the value of a variable in a script from the command line if the script is explicitly setting the value of that variable.
You may, however, set the value of the variable in such a way that it takes the command line arguments into account.
The arguments given on the command line will be available to the shell script in the positional parameters $1
, $2
, $3
etc.
To set a variable to the first positional parameter, you would use
variable="$1"
So, your script might look like
#!/bin/sh
variable="$1"
variable2="Some Value"
echo "$variable,$variable2" # or printf '%s,%sn' "$variable" "$variable2"
... and would be called with
$ ./script.sh "New Value"
The output would be
New Value,Some Value
To provide a default value for a variable, you may do
variable="$1:-Old Value"
Using this in the above script, the output will say Old Value,Some Value
if the script is not called with any command line arguments.
The following short script does proper command line parsing of three command line flags, -a
, -b
and -c
. The flags -a
and -b
takes an argument while -c
does not. It uses three variables, var_a
, var_b
and var_c
, that have default values that may be overridden by using the command line flags.
At the end, the script displays the values of these variables, and also displays what else might have been given on the command line.
#!/bin/sh
var_a=12
var_b="yellow"
var_c=0
while getopts 'a:b:c' opt; do
case "$opt" in
a) var_a=$OPTARG ;;
b) var_b=$OPTARG ;;
c) var_c=1 ;;
*) echo 'command line parsing error' >&2
exit 1
esac
done
shift $(( OPTIND - 1 ))
printf 'var_a = %snvar_b = %snvar_c = %sn'
"$var_a" "$var_b" "$var_c"
if [ "$#" -gt 0 ]; then
echo 'Other operands:'
printf '%sn' "$@"
fi
Running it a few times:
$ ./script.sh
var_a = 12
var_b = yellow
var_c = 0
$ ./script.sh -b "green tea" "hello world"
var_a = 12
var_b = green tea
var_c = 0
Other operands:
hello world
$ ./script.sh -c my other car is a cdr
var_a = 12
var_b = yellow
var_c = 1
Other operands:
my
other
car
is
a
cdr
$ ./script.sh -a -d
var_a = -d
var_b = yellow
var_c = 0
$ ./script.sh -a
./script.sh[15]: -`a' requires argument
command line parsing error
$ ./script.sh -a "boo!" "help!" -c
var_a = boo!
var_b = yellow
var_c = 0
Other operands:
help!
-c
edited Feb 17 at 9:58
answered Feb 17 at 9:09
Kusalananda
103k13202318
103k13202318
Thanks a lot, got where i needed with your help. Great advise.
â evenom
Feb 21 at 21:01
add a comment |Â
Thanks a lot, got where i needed with your help. Great advise.
â evenom
Feb 21 at 21:01
Thanks a lot, got where i needed with your help. Great advise.
â evenom
Feb 21 at 21:01
Thanks a lot, got where i needed with your help. Great advise.
â evenom
Feb 21 at 21:01
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f424742%2fshell-code-change-variable-on-call-of-code%23new-answer', 'question_page');
);
Post as a guest
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
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
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
1
I'm not sure if the code formatting I put in is accurate to what you intended - you can edit the question yourself to correct it.
â Michael Homer
Feb 17 at 9:06