Shell script to assign variable to an environment variable specified in command line argument?
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I have this script that assigns name of the second argument to the environment variable that is specified in first command line argument. I got stuck at assigning and exporting the environment variable:
# assigns first argument to environmentVariable variable
environmentVariable=$1
# assigns second argument to new_path variable
newPath=$2
# calls function locate_field to check if new_path exists in environmentVariable
locate_field $environmentVariable $newPath
# gets return value from locate_field
returnCode=$?
# environment variable is not empty
if [[ "$returnCode" -eq 0 ]]; then
#adds new path and exporting the environment variable
export $environmentVariable=$(eval echo '$'$environmentVariable):$newPath
# environment variable is empty
elif [[ "$returnCode" -eq 2 ]]; then
#adds new path and exporting the environment variable
export $environmentVariable=$newPath
else
echo "$newPath already exists in the environment variable."
fi
How can I fix the the export statements on both conditions to make it work? Here is the locate_field() function.
function locate_field()
#parameters passed to the function
envVariable=$(eval echo '$'$1)
pathToBeAdded=$2
#flag variable found
found=0
#when given environment variable value is empty
list_length=$#envVariable
if [[ "$list_length" -eq 0 ]]; then
found=2
fi
old_value=IFS
IFS=:
for each_path in $envVariable
do
if [[ "$each_path" == "$pathToBeAdded" ]]; then
found=1
fi
done
IFS=$old_value
echo $found
return $found
linux bash shell-script shell
New contributor
add a comment |Â
up vote
1
down vote
favorite
I have this script that assigns name of the second argument to the environment variable that is specified in first command line argument. I got stuck at assigning and exporting the environment variable:
# assigns first argument to environmentVariable variable
environmentVariable=$1
# assigns second argument to new_path variable
newPath=$2
# calls function locate_field to check if new_path exists in environmentVariable
locate_field $environmentVariable $newPath
# gets return value from locate_field
returnCode=$?
# environment variable is not empty
if [[ "$returnCode" -eq 0 ]]; then
#adds new path and exporting the environment variable
export $environmentVariable=$(eval echo '$'$environmentVariable):$newPath
# environment variable is empty
elif [[ "$returnCode" -eq 2 ]]; then
#adds new path and exporting the environment variable
export $environmentVariable=$newPath
else
echo "$newPath already exists in the environment variable."
fi
How can I fix the the export statements on both conditions to make it work? Here is the locate_field() function.
function locate_field()
#parameters passed to the function
envVariable=$(eval echo '$'$1)
pathToBeAdded=$2
#flag variable found
found=0
#when given environment variable value is empty
list_length=$#envVariable
if [[ "$list_length" -eq 0 ]]; then
found=2
fi
old_value=IFS
IFS=:
for each_path in $envVariable
do
if [[ "$each_path" == "$pathToBeAdded" ]]; then
found=1
fi
done
IFS=$old_value
echo $found
return $found
linux bash shell-script shell
New contributor
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have this script that assigns name of the second argument to the environment variable that is specified in first command line argument. I got stuck at assigning and exporting the environment variable:
# assigns first argument to environmentVariable variable
environmentVariable=$1
# assigns second argument to new_path variable
newPath=$2
# calls function locate_field to check if new_path exists in environmentVariable
locate_field $environmentVariable $newPath
# gets return value from locate_field
returnCode=$?
# environment variable is not empty
if [[ "$returnCode" -eq 0 ]]; then
#adds new path and exporting the environment variable
export $environmentVariable=$(eval echo '$'$environmentVariable):$newPath
# environment variable is empty
elif [[ "$returnCode" -eq 2 ]]; then
#adds new path and exporting the environment variable
export $environmentVariable=$newPath
else
echo "$newPath already exists in the environment variable."
fi
How can I fix the the export statements on both conditions to make it work? Here is the locate_field() function.
function locate_field()
#parameters passed to the function
envVariable=$(eval echo '$'$1)
pathToBeAdded=$2
#flag variable found
found=0
#when given environment variable value is empty
list_length=$#envVariable
if [[ "$list_length" -eq 0 ]]; then
found=2
fi
old_value=IFS
IFS=:
for each_path in $envVariable
do
if [[ "$each_path" == "$pathToBeAdded" ]]; then
found=1
fi
done
IFS=$old_value
echo $found
return $found
linux bash shell-script shell
New contributor
I have this script that assigns name of the second argument to the environment variable that is specified in first command line argument. I got stuck at assigning and exporting the environment variable:
# assigns first argument to environmentVariable variable
environmentVariable=$1
# assigns second argument to new_path variable
newPath=$2
# calls function locate_field to check if new_path exists in environmentVariable
locate_field $environmentVariable $newPath
# gets return value from locate_field
returnCode=$?
# environment variable is not empty
if [[ "$returnCode" -eq 0 ]]; then
#adds new path and exporting the environment variable
export $environmentVariable=$(eval echo '$'$environmentVariable):$newPath
# environment variable is empty
elif [[ "$returnCode" -eq 2 ]]; then
#adds new path and exporting the environment variable
export $environmentVariable=$newPath
else
echo "$newPath already exists in the environment variable."
fi
How can I fix the the export statements on both conditions to make it work? Here is the locate_field() function.
function locate_field()
#parameters passed to the function
envVariable=$(eval echo '$'$1)
pathToBeAdded=$2
#flag variable found
found=0
#when given environment variable value is empty
list_length=$#envVariable
if [[ "$list_length" -eq 0 ]]; then
found=2
fi
old_value=IFS
IFS=:
for each_path in $envVariable
do
if [[ "$each_path" == "$pathToBeAdded" ]]; then
found=1
fi
done
IFS=$old_value
echo $found
return $found
linux bash shell-script shell
linux bash shell-script shell
New contributor
New contributor
New contributor
asked 5 mins ago
user315606
6
6
New contributor
New contributor
add a comment |Â
add a comment |Â
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
user315606 is a new contributor. Be nice, and check out our Code of Conduct.
user315606 is a new contributor. Be nice, and check out our Code of Conduct.
user315606 is a new contributor. Be nice, and check out our Code of Conduct.
user315606 is a new contributor. Be nice, and check out our Code of Conduct.
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%2f475207%2fshell-script-to-assign-variable-to-an-environment-variable-specified-in-command%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