0

I want to run the following commands from a bash script:

sudo cat << EOF>> /etc/profile

export LD_LIBRARY_PATH=/lib:/lib64:/usr/lib:/usr/lib64:/usr/local/lib:/usr/local/lib64
export JAVA_HOME=/opt/jdk1.6.0_22/
export PATH=${JAVA_HOME}/bin:/opt/tomcat/bin:${PATH}

EOF

sudo source /etc/profile

But a get a following errors:

/home/ak/init-script.sh: line 40: /etc/profile: Permission denied
sudo: source: command not found

Any help about how to achieve what i want?

Antonis

2 Answers 2

3

You get "Permission denied" error, when trying to write to /etc/profile. This is because sudo is applied only to the first command - cat, but process writing to /etc/profile has the privileges of your normal user.

This is why this file does not get created. There is a workaround, but I don't think you need it, since if you just need to export some variables, you can do it directly from your shell:

export LD_LIBRARY_PATH=/lib:/lib64:/usr/lib:/usr/lib64:/usr/local/lib:/usr/local/lib64
export JAVA_HOME=/opt/jdk1.6.0_22/
export PATH=${JAVA_HOME}/bin:/opt/tomcat/bin:${PATH}

If you need to load settings from /etc/profile to your shell, you can just run source /etc/profile without sudo, it is not needed there.

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

Comments

3

source is a builtin bash command, not a program you can run with sudo. You have to use sudo on the entire script

Comments

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.