Shell variable to c++ program as input


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell variable to c++ program as input
# 1  
Old 08-23-2010
Question Shell variable to c++ program as input

I have an Shell script which has few global variables
eg :

Code:
range=100;
echo "$range"

I want to use the same variable in my C++ program for example
Code:
 int main() 
 { cout << range << "\n"; }

i tried using this
Code:
int main(int argc, char *argv[])
{   cout << range << "\n"; }

but unsucessful, how should i proceed
# 2  
Old 08-23-2010
Because C/C++ code is a compiled language, it's not aware of any variables you didn't define yourself at compile-time. In this case we need to use a library call to get shell variables, and even then we can't get all shell variables, only ones your shell has exported.

In shell:

Code:
export range=100

In program:
Code:
#include <stdlib.h>

int main(int argc, char *argv[])
{
       const char *range=getenv("range");

        if(range == NULL)
        {
                cout << "Range not available\n";
        }
        else
        {
                cout << "Range is " << range << "\n";
        }

        return(0);
}

Or you could do this:

Code:
myprogram $range

Code:
int main(int argc, char *argv[])
{
        if(argc < 2)
        {
                cout << "No argument\n";
                return(1);
        }

        cout << "First argument " << argv[1] << "\n";
        return(0);
}

# 3  
Old 08-23-2010
its showing

Code:
In function 'int main(int, char**)':
test_file.cpp:24: error: invalid operands of types 'const char*' and 'int' to binary 'operator*'

# 4  
Old 08-23-2010
Did you #include <stdlib.h> ?
# 5  
Old 08-24-2010
The code runs good, but when i am using the below code (a part of it for further processing )
it is throwing me the error

Code:
int range_m = range*1000;

error: invalid operands of types 'const char*' and 'int' to binary 'operator*'

# 6  
Old 08-24-2010
Hi.

You're trying to assign a char * to an int.


A couple of easy options:
Code:
int range_m = atoi(range)*1000;

Or:
Code:
int range_m;
sscanf(range, "%d", &range_m);
range_m *= 1000;

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Help on passing an input variable to the voronota program (after third pipe)

Dear UNIX forum members, I am using macbook pro 13 (2015 edition) with MAC OS Mojave and am trying to write the shell script where when it is run through terminal it asks for an input (in the code below an input variable is domains) and then that input becomes capital letter or letters which... (3 Replies)
Discussion started by: Aurimas
3 Replies

2. Shell Programming and Scripting

Seeing input that you redirect to a program on the shell

Suppose I have a program that I've written that accepts input, ie this C++ program: #include <iostream> using namespace std; int main() { cout << "Enter something:" << endl; int x; cin >> x; cout << "You entered data" << endl; } Suppose that I have a text file,... (5 Replies)
Discussion started by: Chris J
5 Replies

3. Shell Programming and Scripting

Write a shell program with input

Hi, Here is my question: I want a shell script which I name as 'del', and can be used as del(string). when run del(string), it will delete several directories at different locations in my system,like: rm -fr /lustre/fs/scratch/user/$string rm -fr /home/user/$string rm -fr... (4 Replies)
Discussion started by: 1988PF
4 Replies

4. Shell Programming and Scripting

Passing a variable as input to another shell

I have a shell program that calls another shell program the following code works . chkTimeFormat.sh "10/9/12 17:51:19:783."|read c but when I am passing the the time in a variable like in the code below, the shell chkTimeFormat.sh is not returning proper value time="10/9/12... (9 Replies)
Discussion started by: swayam123
9 Replies

5. Shell Programming and Scripting

Shell script to input as if from command line to the java program

Hi, We are having a java server which can run on command line and once initiated, it will prompt options to enter from 0 to 5. The java program kickoff respective operation once number is entered between 0 to 5. However i want to always enter "1" and write another shell program wrapper to start... (4 Replies)
Discussion started by: surya5kn
4 Replies

6. UNIX for Dummies Questions & Answers

how to pass input from c program to shell script?

Hello.. I am developing a Graphical User Interface using GTK. As part of our project I need to take inputs from GTK entries and pass those inputs to shell script and use them in shell script. The problem which struck me is only limited number of inputs are getting passed to shell script. For now... (14 Replies)
Discussion started by: kalyanilinux
14 Replies

7. Shell Programming and Scripting

Can a shell variable be called in a cobol program

Hi All, I have a file which sets all the variables on unix , based on the hostname. Currently these variables are hardcoded in the cobol programs.I was wondering if unix variables can be used in Cobol programs ? Example : I have a variable $SHTEMP which is set based on the following : Prod... (2 Replies)
Discussion started by: nua7
2 Replies

8. Shell Programming and Scripting

input stored procedure to shell program

Hello, I have to call the stored procedure as argument from the unix shell program. Looks like unix doesnt like, can someone comment pls USERID=scott PASSWD=xxxxxx PLSQLCALL=$2 STDT=`sqlplus /nolog <<END >> $LOGFILE conn ${USERID}/${PASSWD}@${ORACLE_SID} whenever sqlerror exit failure... (9 Replies)
Discussion started by: tvanoop
9 Replies

9. Shell Programming and Scripting

Using Shell variable within awk program

I have a fixed width text file data.txt delimited by pipe. My requirement is to replace the second column values by *. Problem is that when tried access the shell variable in the awk program, value of shell variable is not printed instead only the name of the variable is printed. Please help to... (4 Replies)
Discussion started by: VijayakumarS
4 Replies

10. Shell Programming and Scripting

Launching a C program that needs input from a shell script

hi there, i need some help, i am trying to run a script to launch a C program and a Java program but before running both I want to get a user input and then invoke both programs with input received. In the programs the inputs are not command line arguments. This is the code, after the java... (4 Replies)
Discussion started by: momal
4 Replies
Login or Register to Ask a Question