0

So I am trying to set environment variables using my shell Script. The script takes some inputs from the user and then i have to set those inputs in the environment variable. I am using two shell Script for the same but i am getting permission denied errors.

Script 1

DEFAULT_NAME="sample"
read -p "Enter your Name: [$DEFAULT_NAME]: " USER_NAME
if [ -z "$USER_NAME" ]
then
    USER_NAME=$DEFAULT_NAME
else
    USER_NAME=$USER_NAME
fi
source setEnv.sh

Script 2

echo -e "export NAME=${USER_NAME}" >> /etc/profile.d/nameenv.sh
2
  • 2
    Your 2nd script has to run as root, since /etc/profile.d/nameenv.sh can only be written by root. Commented Jul 5, 2021 at 8:33
  • As indicated by the comments to @David's answer this appears to be an example of the XY Problem. No one can figure out what problem you're trying to solve. Note that in UNIX like operating systems (e.g., Linux) environment variables are private to each process and inherited from their parent process. Commented Jul 6, 2021 at 2:28

1 Answer 1

1

First, the if condition in Script 1 is wrong, you probably want to test $USER_NAME instead. If you are using bash, you can replace the whole if statement with:

USER_NAME=${USER_NAME:-$DEFAULT_NAME}

In Script 2 are you sure that you want to append a new line to /etc/profile.d/nameenv.sh, every time you execute the script? The last declaration will hide the preceding ones.

Finally, note that you need root privilege to write in /etc/profile.d. Are you running the script as a privileged user?

[Edit] Trying to guess what you are trying to do here. If you need that USER_NAME is redefined for the user's current session (and not system-wide), just replace the last line in Script 1 with:

export USER_NAME

and remove Script 2. If you want to make it permanent (again, for the current user only), modify Script 2 to write the variable declaration in ~/.bash_profile instead.

Sign up to request clarification or add additional context in comments.

6 Comments

I have edited the script 1 now coming on to the script 2 i want whenever the user runs script1 the environment variable gets updated. Also is there a way we can do this without being the root user or any other method help would be appreciated.
Some context is needed here. What are you trying to achieve? Files in /etc/profile.d/ are not user-specific. Do you really want any user to be able to redefine USER_NAME for everyone in the system?
So basically the username is just an example lets say i have to switch DatabaseID or something like that .
The problem of course is not the variable name, but who (and when) should be affected by the change. Were Script 2 allowed to do that echo, all the users (including root) at the next login would have the new value in their environment. Is this really what you want to do? Definitely, only root should be able to do that...
Yes you are right only root user should be able to do so. Also is there any way by which the environment variables changes are reflected without logging in again.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.