error message: warning: unknown escape sequence: '\$' [enabled by default]
Please excuse my ignorance ... Is there a solution? What are the limitations of the aforementioned C++. Calling a Bash script from C++ is not the optimum solution for my case. The script needs to be inside the .cpp
We can embed bash code in C++ with the system command or the define shellscript approach. When the bash code is a bit more advanced, for examples arrays or evaluation of variables of variables, errors appear.
An example of what I want to do:
Var1=good
Var2=excellent
I wish to echo Var1 and Var2 by using a two variable approach $var and $k. var=Var is stable and index "k" increases. This can be easily done in Bash:
var=VAR
k=0
VAR0=good
VAR1=excellent
for ((k=0; k < 2 ;k++))
do
eval "echo "\$$var$k""
done
Last login: Sun Dec 1 18:09:26 on ttys000
AMIGA:barrywalker~> chmod 755 var_to_newvar.sh
AMIGA:barrywalker~> ./var_to_newvar.sh
#!/bin/bash --posix
var=VAR
k=0
VAR0=good
VAR1=excellent
for k in $( seq 0 1 )
do
eval echo "$"$var$k
done
good
excellent
AMIGA:barrywalker~> _
Of course this is generated by another shell script but it is a demo to try and overcome
any C++ warnings, errors and special directives...
As we have no idea of C++ code then the system() function seems the less problematic.
Apart from changing lines 4 and 10 to "#!/bin/bash --posix" and cleaning up a bit, including removal of cout...
// Notice ! and --posix added to lines 4 AND 10
// Also cleaned it up a bit...
#define SHELLSCRIPT1 "\
#!/bin/bash --posix\n \
echo \"hello\"\n \
"
// Not working
#define test1 "\
#!/bin/bash --posix\n \
array=(a1 a2 a3)\n \
echo ${array[*]}\n \
"
#include <iostream>
#include <cstdlib>
using namespace std;
int main ()
{
// working cout removed
system(SHELLSCRIPT1);
// works on macbook pro cout removed
system(test1);
return 0;
}
Results...
Last login: Sun Dec 1 19:44:37 on ttys000
AMIGA:barrywalker~> g++ test.cpp
AMIGA:barrywalker~> ./a.out
hello
a1 a2 a3
AMIGA:barrywalker~> _
EDIT:
BTW cout removed because of RCs of 0 being printed to the window...
The shebang, #!/bin/bash ... is irrelevant; the system(3) library function will use /bin/sh . The OP's /bin/sh does not support the necessary features (arrays, indirect vars).
works as a dream. It is not which command is used, just
\$
is not accepted
The parenthesis used in a array declaration remain a problem. Should they be quoted, unquoted, backslashed?
---------- Post updated at 06:06 AM ---------- Previous update was at 05:38 AM ----------
Is there a work around the array issue? I need an 1d array. The only alternative are variable of variables , which work (the problem solved in this forum by wisecracker)
---------- Post updated at 06:32 AM ---------- Previous update was at 06:06 AM ----------
on ubuntu with g++ it does not work. If it works on Mac OS X perhaps is a matter of compiler. If yes is there a suitable c++ compiler on linux?