1

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

1 Answer 1

2

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
1
  • 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, 2012 at 6:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .