Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C++ - Passing a Command to the CLI, Linux

Options
  • 09-10-2014 11:10am
    #1
    Moderators, Computer Games Moderators, Technology & Internet Moderators Posts: 19,240 Mod ✭✭✭✭


    Just having an issue with an app. I'm brushing up on C++, by redeveloping a few work scripts from Bash to C++, in an attempt to sway them towards a redevelopment in the future. I will say though, I'm having a slight issue passing one particular command to the CLI, and just to point out, I'm running these C++ scripts on RHEL v6.3.
    public : void check_Files()
    {
    string izscan = "/opt/mapbase/tools/izscan";
    string lots = izscan + "/lots";
    string log = izscan + "/log";
    string lotsdad = izscan + "/lotsdad";
    string maps = izscan + "/maps";
    string temp3 = izscan + "/temp3";
    string files;

    if(system(NULL))
    {
    system("cd /opt/mapbase/tools/izscan/"); // This doesn't work.
    system("ls /opt/mapbase/tools/izscan/"); // This works fine.

    }
    }

    I've highlighted the lines of code where I'm having an issue with a comment relating to what does and does not work. Is there something I'm over looking when it comes to passing the CD command in Linux?


Comments

  • Registered Users Posts: 23,212 ✭✭✭✭Tom Dunne


    At a guess, the cd command is redundant - what's the output of it? The ls command, by contrast has an output - the list of files.


  • Moderators, Computer Games Moderators, Technology & Internet Moderators Posts: 19,240 Mod ✭✭✭✭L.Jenkins


    I've checked with an ls and pwd after the cd command and it lists the contents of the current directory I'm in and not the working directory. If I do an ls /target_dir/ the ls works fine.


  • Registered Users Posts: 1,311 ✭✭✭Procasinator


    When you call system, it creates a child shell that runs your command. Changes that run in this child process are not reflected in your programs environment. So effectively, the process is like (implementation dependant obviously):

    /bin/sh -c cd /opt/mapbase/tools/izscan/
    /bin/sh -c ls /opt/mapbase/tools/izscan/

    The CD command is actually working, but it's only changing the directory of that sh environment before exiting back to your program. Your program still has the same working directory.

    If you want all systems calls to be run from a different working directory, change the directory of the program itself (see chdir for instance).

    Side point: What benefit, besides the learning opportunity, are you getting converting these kind of scripts to C++?


  • Moderators, Computer Games Moderators, Technology & Internet Moderators Posts: 19,240 Mod ✭✭✭✭L.Jenkins


    Thanks Pro for the insight, much appreciated. I've been redeveloping a few scripts to run as an application, so the functionality of a number of scripts could be rolled into 1 C++ script. This would reduce the need to chase our tails looking around a number of directories, to find different scripts if they need to be modified in the future.

    The payoff might be nominal now, but would make life easier, especially in the future if all the current developers just up and leave one day and someone else needs to do an analysis of the entire application quickly to see how it works.


  • Registered Users Posts: 2,022 ✭✭✭Colonel Panic


    Is there such a thing as a C++ script? What's the benefit of a compiled binary spitting out shell commands over bash scripts, which are designed to glue things together in Linux?

    Why not sort out and document the scripts?


  • Advertisement
  • Moderators, Computer Games Moderators, Technology & Internet Moderators Posts: 19,240 Mod ✭✭✭✭L.Jenkins


    Meh, just habit calling it a script. I compile it on RHEL and run it in the same way as a bash script. What ever strikes your fancy though whether it be a script, file or application.


  • Registered Users Posts: 2,022 ✭✭✭Colonel Panic


    It's not about what strikes my fancy! It's not a script!

    I do think your approach here is the Wrong Thing though, especially if all you're going to do is call system with shell commands in lieu of the bash script.


  • Registered Users Posts: 1,311 ✭✭✭Procasinator


    Yeah, I agree regarding the wrong approach. Unless there is more to the "scripts" :P than running commands, you seem to be making life harder with no gain.


  • Closed Accounts Posts: 2,267 ✭✭✭h57xiucj2z946q


    I think this is more suitable for to be left in a suite of scripts as opposed to compiling binaries. If you wanna move away from shell script, look at python. You can create a suite of scripts and/or modules to have it organised, launch it from main script via passed in arguments?

    You will loose portability, if that is a concern, when using pre-compiled binaries.

    But this is only my 2 cents if this is for production, as opposed to using this as c++ learning exercise.


  • Moderators, Computer Games Moderators, Technology & Internet Moderators Posts: 19,240 Mod ✭✭✭✭L.Jenkins


    It is a learning exercise more so. I can develop it in Python handy enough.


  • Advertisement
  • Registered Users Posts: 159 ✭✭magooly


    // cd to confdir directory (so that include paths can be relative)
    char curdir[PATH_MAX];
    char *confdir;
    getcwd(curdir, PATH_MAX);
    confdir = "/opt/mapbase/tools/izscan/"";
    chdir(dirname(confdir));

    ls can be done with popen() too.


  • Registered Users Posts: 6,150 ✭✭✭Talisman


    Itzy wrote: »
    system("cd /opt/mapbase/tools/izscan/"); // This doesn't work.

    I've highlighted the lines of code where I'm having an issue with a comment relating to what does and does not work. Is there something I'm over looking when it comes to passing the CD command in Linux?
    Use the chdir() function to change the directory.


  • Moderators, Computer Games Moderators, Technology & Internet Moderators Posts: 19,240 Mod ✭✭✭✭L.Jenkins


    Thanks, shall try it out.


Advertisement