shell code change variable on call of code

The name of the pictureThe name of the pictureThe name of the pictureClash 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"






share|improve this question


















  • 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














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"






share|improve this question


















  • 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












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"






share|improve this question














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"








share|improve this question













share|improve this question




share|improve this question








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












  • 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










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





share|improve this answer






















  • Thanks a lot, got where i needed with your help. Great advise.
    – evenom
    Feb 21 at 21:01











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: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
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%2f424742%2fshell-code-change-variable-on-call-of-code%23new-answer', 'question_page');

);

Post as a guest






























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





share|improve this answer






















  • Thanks a lot, got where i needed with your help. Great advise.
    – evenom
    Feb 21 at 21:01















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





share|improve this answer






















  • Thanks a lot, got where i needed with your help. Great advise.
    – evenom
    Feb 21 at 21:01













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





share|improve this answer














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






share|improve this answer














share|improve this answer



share|improve this answer








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

















  • 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













 

draft saved


draft discarded


























 


draft saved


draft discarded














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













































































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