1

I am trying a call a shell script from a cpp program and passing some variables to the script. The script simply copies a file from one directory to another. I want to pass the name of the file, the source directory and the destination directory to shell script from the cpp program. I get the error omitting directory "/" when I try. Please how can I fix this

C++ Code:

std::string spath="/home/henry/work/gcu/build/lib/hardware_common/";

std::string dpath="/home/henry/work/gcu/dll/";

std::string filename="libhardware_common.a";

std::system("/home/henry/work/gcu/build/binaries/bin/copy.sh spath dpath filename");

Shell Script code:

SPATH=${spath}

DPATH=${dpath}

FILE=${filename}

cp ${SPATH}/${FILE} ${DPATH}/${FILE} 
2
  • spath and dpath and not substituted in your C++ code. They will be the literals spath and dpath. Maybe you should use a std::ostringstream and insert them into a command to use with std::system. You should probably show more of your shell code, too. I think there are some things wrong with it but more contrext is probably needed. Commented Jul 19, 2018 at 12:34
  • [teach-me] You have some misconceptions about strings and variable names in C and in shell. "dpath" string in C does not magically become "/home/henry/work/gcu/dll/" when you pass it to system(). Nor can shell access command line arguments by their C variable names - look into $1, $2and so on in shell. Commented Jul 19, 2018 at 12:38

1 Answer 1

2

Your C++ code and the shell script are not in the same scope. In other words, variables in C++ will not be visible in your script, and when passed to the script, the variables will be renamed as $1, $2, and so on.

To fix it, you can change your code to the following:

std::string spath = "/home/henry/work/gcu/build/lib/hardware_common/";

std::string dpath = "/home/henry/work/gcu/dll/";

std::string filename = "libhardware_common.a";

std::string shell = "/home/henry/work/gcu/build/binaries/bin/copy.sh"

std::system(shell + " " + spath + " " + dpath + " " + filename);

In this way, the spath will be substituted by the value of it, which is then passed to your script.

In your script, you can use:

cp $1/$2 $3/$2

or if you prefer:

SPATH=$1

DPATH=$2

FILE=$3

cp ${SPATH}/${FILE} ${DPATH}/${FILE}

The script will never know the variable name in the C++ code. When the script is called, the parameters will be replaced by $1, $2...

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

2 Comments

Just a caveat: if any of the spath, dpath and filename have any spaces in them, the code in the answer will break. More elaborate approach would be needed then.
@Arkadiy: Or asterisk, or dollar sign, or backticks... It is worth noting that this sort of thing is pretty rare in production code, because it is a likely source of security holes. (At the very least, whoever is reviewing your code will need to spend some time proving that it's not.)

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.