Visit Our UNIX and Linux User Community


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++
# 1  
Old 12-01-2013
BASH arrays and variables of variables in C++

Sometimes it is handy to protect long scripts in C++.

The following syntax works fine for simple commands:

Code:
#define SHELLSCRIPT1 "\
#/bin/bash \n\
echo \"hello\" \n\
"
int main ()
{
cout <<system(SHELLSCRIPT1);
return 0;
}


Unfortunately for there are problems for:
1d arrays: array=(a1 a2 a3)

error message: sh: 12: Syntax error: "(" unexpected
and
variables of variables:
Code:
var=VAR
k=0
VAR0=good
echo $VAR0; echo blah
eval "echo "\$$var$k""



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

Thanks

Last edited by frad; 12-01-2013 at 11:00 AM..
# 2  
Old 12-01-2013
I am not sure what you are trying to do but is it this?
OSX 10.7.5, default bash terminal...
Code:
Last login: Sun Dec  1 15:55:50 on ttys000
AMIGA:barrywalker~> var=VAR
AMIGA:barrywalker~> k=0
AMIGA:barrywalker~> VAR0="Hello World."
AMIGA:barrywalker~> eval echo "$"$var$k
Hello World.
AMIGA:barrywalker~> _

This User Gave Thanks to wisecracker For This Post:
# 3  
Old 12-01-2013
Quote:
Originally Posted by frad
Sometimes it is handy to protect long scripts in C++.
What do you mean by "protect"? Embedding shell scripts within C++ is not a standard practice IMO.
# 4  
Old 12-01-2013
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:

Code:
var=VAR
k=0
VAR0=good
VAR1=excellent

Code:
for ((k=0; k < 2 ;k++))
do
eval "echo "\$$var$k""
done

This bash script I wish to embed in C++
# 5  
Old 12-01-2013
Attempting to steer clear of as many C++ warnings as possible:-
Longhand OSX 10.7.5, default bash terminal...
Code:
#!/bin/bash --posix
> /tmp/vars.sh
chmod 755 /tmp/vars.sh
echo '#!/bin/bash --posix' >> /tmp/vars.sh
echo 'var=VAR' >> /tmp/vars.sh
echo 'k=0' >> /tmp/vars.sh
echo 'VAR0=good' >> /tmp/vars.sh
echo 'VAR1=excellent' >> /tmp/vars.sh
echo 'for k in $( seq 0 1 )' >> /tmp/vars.sh
echo 'do' >> /tmp/vars.sh
echo '        eval echo "$"$var$k' >> /tmp/vars.sh
echo 'done' >> /tmp/vars.sh
cat /tmp/vars.sh
echo ""
/tmp/vars.sh
echo ""

Results:-
Code:
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.
# 6  
Old 12-01-2013
eval is evil

bash has got indirection
Code:
$ var=VAR
$ VAR0=good
$ VAR1=excellent
$ for ((k=0; k < 2 ;k++)); do nv="$var$k"; echo "$nv = ${!nv}"; done
VAR0 = good
VAR1 = excellent

# 7  
Old 12-01-2013
@ wisecracker I think following problem he is facing, in preprocessor directives


Code:
$ cat test.cpp

// working
#define SHELLSCRIPT1 "\
#/bin/bash \n\
echo \"hello\" \n\
"

// Not working
#define test1 "\
#/bin/bash \n\
array=(a1 a2 a3) \n \
echo ${array[*]} \n \
"

#include <iostream>
#include <cstdlib>
using namespace std;
int main ()
{
// working
cout <<system(SHELLSCRIPT1);

// whereas this is not working
cout <<system(test1);

return 0;
}

$ g++ test.cpp

$ ./a.out 
hello
sh: 2: Syntax error: "(" unexpected


Previous Thread | Next Thread
Test Your Knowledge in Computers #412
Difficulty: Medium
AT&T 3B computer systems included the 3B2, 3B5, 3B15, 3B20S, and 3B4000. These computers were named after the successful 3B20D. The 3B20S (simplex) ran using the UNIX operating system and was developed at Bell Labs and produced by WECo in 1982 for the general purpose internal Bell System use, and later the mini-computer market.
True or False?

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

Featured Tech Videos