Hi All,
I am running a shell script on centos 7 to get some variable values and want to declare them as environment variables so that i will use them in another shell script.
I am using below script i am able to define one environment variable but it's not allowing me to do for another variable.
What exactly is the issue? As always: pls describe the error meticulously, add error messages and misbehaviour.
What I can say from looking at your script is that shell variables are not expanded within single quotes, so neither $user_name nor $passwd values will be passed to the .*profile files. If that is by intention, make sure the vaiables are defined in those scripts.
~/.bash_profile is for a bash login shell.
At login ~/.profile is for all sh-style shells (sh,ash,dash,jsh,psh,ksh,bash,...), but bash runs ~/.bash_profile instead if present.
Some graphical desktops provide a login but do not run a login shell, then bash runs ~/.bashrc.
Some Linux distros might run ~/.bash_profile (or ~/.profile) from /etc/bash.bashrc that is run prior to ~/.bashrc.
when i executed the shell script it's executed successfully but when i am trying to print the environment variable it's returning empty value echo $NAME -- is giving empty value.
Can you please let me know what i need to change to set the environment variable ?.
See how the second instance sc_1 is sourced into the current shell by using . ./sc_1 (or source ./sc_1 if you prefer the clearer long-hand form). Any variables set will now be in the current shell. Think of this as reading the commands in the child script into the current process not spawning a new shell to run sc_1 which loses any environment vars after it finishes.
$NAME being empty is a strong indicator that $user_name is empty and the "command substitution" fails / results in an empty string. I guess its because the file called $abc doesn't exist. and the cat cats nothing.
It would help if people knew the entire scenario. Variables in which environment do you want to set? Your current session? Future sessions? Why the echo ing into the varying .profiles?
You're using find to search directories, nice.
But then you're using the variable without checking if it's has one , none or more results.
Further, I assume that you want the variable $user_name to actualy contain (just) the "<username>", and not the string "username + <username>".
This said, you might want to take care of that, afterwhich you might want to use something like this:
if [[ -n "$user_name" ]]
then export NAME=$user_name
else echo "<user_name> is empty"
# If it is within a script:
#exit 1
fi
As the string might become empty after removing the found string pattern from it's content ("username + <username>"), if it even finds anything - as in your current case.
This said, you might want to make sure where your find starts from.
I would also suggest to take care of multiple finds (tempfiles) within a a for loop.
This is my suggested handling for all dynamic (find, locate, *, ls, which, etc) variables.