BASH arrays and variables of variables in C++


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting BASH arrays and variables of variables in C++
# 8  
Old 12-01-2013
Apart from changing lines 4 and 10 to "#!/bin/bash --posix" and cleaning up a bit, including removal of cout...
Code:
// 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...
Code:
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...

Last edited by wisecracker; 12-01-2013 at 04:11 PM.. Reason: See above...
# 9  
Old 12-01-2013
Code:
mute@thedoctor:~/temp/frad$ ./makecode < script > code.cpp
mute@thedoctor:~/temp/frad$ g++ code.cpp
mute@thedoctor:~/temp/frad$ ./a.out
VAR0 = good
VAR1 = excellent
mute@thedoctor:~/temp/frad$ cat makecode
#!/bin/sh

cat << '__EOF__'
#include <iostream>
#include <cstdlib>
#include <unistd.h>
using namespace std;

#define SHELL "/bin/bash"

const char shellcode[] = {
"\
__EOF__

sed -e 's/"/\\"/g' -e 's/$/\\n\\/'

cat << '__EOF__'
"
};

int main() {
        return execl(SHELL, SHELL, "-c", shellcode, NULL);
}
__EOF__

# 10  
Old 12-01-2013
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).

Regards,
Alister
These 3 Users Gave Thanks to alister For This Post:
# 11  
Old 12-09-2013
The solution with
Code:
"$"

works as a dream. It is not which command is used, just
Code:
\$

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?
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing awk variables to bash variables

Trying to do so echo "111:222:333" |awk -F: '{system("export TESTO=" $2)}'But it doesn't work (2 Replies)
Discussion started by: urello
2 Replies

2. Shell Programming and Scripting

Running a script with multiple variables like 25 variables.

Hi All, i have a requirement where i have to run a script with at least 25 arguements and position of arguements can also change. the unapropriate way is like below. can we achieve this in more good and precise way?? #!/bin/ksh ##script is sample.ksh age=$1 gender=$2 class=$3 . . .... (3 Replies)
Discussion started by: Lakshman_Gupta
3 Replies

3. Shell Programming and Scripting

functions and variables in bash

I have a bash script with some functions as below and am wondering if I can use the variables declared in setup in the other functions and in the rest of the bash script. setup(){ none=0; low=1; medium=2; high=3; debug=4 var1="red" var2="fred" } create_basemap() { ... (7 Replies)
Discussion started by: kristinu
7 Replies

4. Shell Programming and Scripting

Creating variables in bash

I am writing some scripts using bash and am wondering if there is a better way to perform the following set of formatting variables. s1=" " s2=" " s3=" " s4=" " s5=" " s6=" " s7=" " s8=" " frmt_titl="${bYl}%s${nClor}\n" frmt1_titl="${s1}$frmt_titl"... (10 Replies)
Discussion started by: kristinu
10 Replies

5. UNIX for Dummies Questions & Answers

File Field Replacement, Assigning Fields to Variables, Lists/Arrays?

Okay, I've made threads on extracting fields and comparing strings in separate files in .csv's. I've written the following code with intentions of learning more. I just want this one question answered: How can I assign fields from a file(comma separated) to variables? My goal is to check... (0 Replies)
Discussion started by: chickeneaterguy
0 Replies

6. UNIX for Dummies Questions & Answers

awk arrays and variables

I have two arrays values aname = first aname = last I would like to assign a variable to both arrays seperated by a comma fname=(aname","aname) that example does not work but that's something I would like to accomplish. Is it possible to assign a printf output to a variable. ... (2 Replies)
Discussion started by: Morph797
2 Replies

7. UNIX for Dummies Questions & Answers

Problem assigning variables to arrays

Hi All, I have a problem assigning variables to script.I have a script in which i have a while loop now i have to assign some values obtained to an array which will be used later in the script.Can anyone help how to do that. At present my scrot looks like: co=0 pco=0 co=`cat /tmp/highcpu... (4 Replies)
Discussion started by: usha rao
4 Replies

8. Programming

How to convert byteArray variables to HexaString variables for Linux?

Hello everybody, I am having problem in converting byte array variables to Hexa String variables for Linux. I have done, converting byte array variables to Hexa String variables for Windows but same function doesn't work for linux. Is there any difference in OS ? The code for Windows is given... (2 Replies)
Discussion started by: ritesh_163
2 Replies

9. Shell Programming and Scripting

Bash variables

Ummm can anybody help me with this one? Its prob quite simple. I bascially have a file name say J1x2x3x7.dat Im using the file name as a variable in a bash script. Want I want to do is extract most of the file name and make it a new variable expect with say one of the number now a... (2 Replies)
Discussion started by: RichieFondel
2 Replies
Login or Register to Ask a Question