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}
spathanddpathand not substituted in your C++ code. They will be the literalsspathanddpath. Maybe you should use astd::ostringstreamand insert them into a command to use withstd::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.[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 tosystem(). Nor can shell access command line arguments by their C variable names - look into$1,$2and so on in shell.