Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I am able to login as a different user using a shell script in Ubuntu, but I want to run a Java program in that user, but it halts there...

for example:

  1. login as a guest user with limited permission
  2. compile HelloWorld.java
  3. java HelloWorld.java

The script I wrote:

USER=sameek
su $USER
javac HelloWorld.java echo compiled

Kindly show me a path to solve this problem

share|improve this question

1 Answer 1

up vote 2 down vote accepted

The problem with your script is that the su $USER line invokes a subshell and so the next command doesn't execute until that subshell is exited.

You probably want to do something more like:

USER=sameek
USERCMDS=/tmp/usercommands.$$
echo "JAVA_HOME=..." >>$USERCMDS
echo "PATH=\${JAVA_HOME}:\$PATH" >> $USERCMDS
echo "javac HelloWorld.java" >> $USERCMDS
chmod a+x $USERCMDS
su $USER -c $USERCMDS
share|improve this answer
    
thank you sir but when i tried to run the script then i says javac command not found. i have set all the environment variables also added the java path in login.def i think its again reseting the path. –  ansh Jun 29 '12 at 6:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.