0

I have a remote unix shell which I log on often to checkout files with but the system keep resetting my locals setting when I logon to it. I was planning to write the code to execute a list of commands when I log on.

#include<iostream>
#include<stdlib.h>

int main(){
 char javah[]="JAVA_HOME=/appl/usr/jdk/jdk1.6.0_20";
 char anth[]="ANT_HOME=/appl/usr/ant/instances/1.8.2";
 char path[]="PATH=$ANT_HOME/bin:$PATH";

    system("bash");
    system("cd");
    system("cd insurancePPC.11");
    system("0x0C");
    system("ls");

    putenv(javah);
    putenv(anth);
    putenv(path);
    std::cout << "JAVA_HOME=" << getenv("JAVA_HOME");
    std::cout << "\n";
    std::cout << "ANT_HOME=" << getenv("ANT_HOME");
    std::cout << "\n";
    std::cout << "PATH=" << getenv("PATH");
    std::cout << "\n";

    system("cd tools");
    std::cout << "command executed successfully...\n";
    return 0;
}

Can anyone tell me why this wasn't working as expected?

7
  • How do you plan to run this program on login? From, say, ~/.profile? Why not just put the commands in ~/.profile directly? Commented Aug 20, 2015 at 13:58
  • What does not work? Program does not output the changed environment variables? Commented Aug 20, 2015 at 13:59
  • @chepner because profile is wiped daily Commented Aug 20, 2015 at 14:00
  • 2
    Then are you planning on running this C program manually every time you log in? You can just as easily write a shell script to run manually. In fact, you'll need to do that, since your C program cannot affect your login shell's environment, only its own. The best this program can do is generate a shell script to source, but then you might as well just write that shell script in the first place. Commented Aug 20, 2015 at 14:01
  • @Sandro I have added export but it still wasn't able to work properly. Commented Aug 20, 2015 at 14:03

2 Answers 2

2

cd is a built in command of the shell and only affects the current process (i.e. the current running shell.)

When you run system("cd insurancePPC.11"); it starts a new shell, that new shell changes the directory to insurancePPC.11 and exits. Your own process is unaffected by that cd command.

You are much better off writing these commands in a text file and run it as a shell script via the source command.

Create a file named myenv.sh with this content:

JAVA_HOME=/appl/usr/jdk/jdk1.6.0_20
export JAVA_HOME
ANT_HOME=/appl/usr/ant/instances/1.8.2
export ANT_HOME
PATH=$ANT_HOME/bin:$PATH
export PATH
cd
cd insurancePPC.11
ls

echo JAVA_HOME=$JAVA_HOME
echo ANT_HOME=$ANT_HOME
echo PATH=$PATH
cd tools

And from your command line run source myenv.sh Or if your shell supports it, use the shorthand . myenv.sh

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

Comments

2

There's no need to write a C program here. Just save the following as mysettings.sh:

export JAVA_HOME=/appl/usr/jdk/jdk1.6.0_20
export ANT_HOME=/appl/usr/ant/instances/1.8.2
PATH=$ANT_HOME/bin:$PATH
cd tools

When you log in, run

. mysettings.sh

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.