how to pass input from c program to shell script?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers how to pass input from c program to shell script?
# 8  
Old 01-31-2012
no.. just post the method dadd code here and what argument you are passing to the shell script ?

what is the value of str ( before passing to the system method )

And post the shell script code also.
# 9  
Old 01-31-2012
GTK program is:

#include<gtk/gtk.h>
#include<stdio.h>
#include<stdlib.h>
gchar *chardadd1=NULL,*chardadd2=NULL;

void dadd(GtkWidget *widget, gpointer label)
{
char str[512];
int x;
sprintf(str,"sh st1.sh %s ",chardadd1);
x=system(str);
}
int main(int argc,char *argv[])
{
int i;char buf[128];


GtkWidget *window;
GtkWidget *tabledadd;
GtkWidget *labeldadd1;
GtkWidget *entrydadd1;
GtkWidget *buttondadd;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_window_set_title(GTK_WINDOW(window), "GtkEntry");

gtk_container_set_border_width(GTK_CONTAINER(window), 50);
entrydadd1 = gtk_entry_new();
tabledadd = gtk_table_new(3, 2, FALSE);
gtk_container_add(GTK_CONTAINER(window), tabledadd);
labeldadd1 = gtk_label_new("group name");
buttondadd=gtk_button_new_with_label("click me");
gtk_table_attach(GTK_TABLE(tabledadd), labeldadd1, 0, 1, 0, 1,
GTK_FILL | GTK_SHRINK, GTK_FILL | GTK_SHRINK, 5, 5);
gtk_table_attach(GTK_TABLE(tabledadd), entrydadd1, 1, 2, 0, 1,
GTK_FILL | GTK_SHRINK, GTK_FILL | GTK_SHRINK, 5, 5);

gtk_table_attach(GTK_TABLE(tabledadd), buttondadd, 0, 2, 2, 3,
GTK_FILL | GTK_SHRINK, GTK_FILL | GTK_SHRINK, 5, 5);

chardadd1=gtk_entry_get_text(GTK_ENTRY(entrydadd1));
gtk_widget_show(window);
gtk_widget_show(tabledadd);
gtk_widget_show(labeldadd1);
gtk_widget_show(entrydadd1);
gtk_widget_show(buttondadd);

//dadd will be called in the below statement
g_signal_connect(buttondadd, "clicked",G_CALLBACK(dadd), entrydadd1);

g_signal_connect(window, "destroy",G_CALLBACK(gtk_main_quit), NULL);
gtk_main();
return 0;
}


Shell script is:

IN=$1
c=`echo $IN | tr -dc ';' | wc -c`
b=(`awk -F';' '$1=$1' <<<$IN`)
i=0
while [ $i -le $c ];
do
d=(`echo ${b[i]} | tr '-' ' '`)
a=${d[0]}
b=${d[1]}

while [ $a -le $b ];
do
echo $a
a=`expr $a + 1`
done
i=`expr $i + 1`
done




The input that has to be given in text entry should be in the format
"1-10;20-30;40-50;60-70"

and this will be passed to shell script and it gets splitted there and the numbers from 1-10,20-30,40-50,60-70 (as per the given input) will be displayed.
The shell script alone is working fine, but if I give more inputs through the text entry then it's not getting passed form Gtk program to shell program Smilie
# 10  
Old 01-31-2012
make the colored changes in your shell script.

Code:
 
IN=$1
c=`echo $IN | tr -dc ';' | wc -c`
b=`awk -F';' '$1=$1' <<<$IN`
i=0
while [ $i -le $c ];
do
d=`echo ${b[i]} | tr '-' ' '`
a=${d[0]}
b=${d[1]}
while [ $a -le $b ];
do
echo $a
a=`expr $a + 1`
done
i=`expr $i + 1`
done

can you change this also.

Code:
sprintf(str,"sh st1.sh \"%s\"",chardadd1);

# 11  
Old 01-31-2012
It's returning errors if dont keep the braces.. Smilie
# 12  
Old 01-31-2012
ok, keep the brace.

Did you print the str ? (before you passing to the system ) to make sure what you exactly executing...
# 13  
Old 01-31-2012
It's (value of str) printing garbage values if I give large number of inputs..
# 14  
Old 01-31-2012
can you answer for my questions

1) post the value of str
2) give the small number of input.
3) what is the value of chardadd1 ? ( print after this line -- chardadd1=gtk_entry_get_text(GTK_ENTRY(entrydadd1))Smilie
4) And the same is printed inside the dadd method ?
5) inside the shell script, add echo $IN after the IN=$1 and check whether the correct input is passed or not.
6) what is the datatype of chardadd1 ?
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How pass the input parameter to a file in the script ?

OS version: RHEL 6.7 myTextFile.txt file is referred within Script1.sh script, I only execute Script1.sh and I want the input variable to be passed inside myTextFile.txt . Any idea how I can do this ? $ cat script1.sh cat myTextFile.txt $ cat myTextFile.txt $1 Requirement1.... (4 Replies)
Discussion started by: kraljic
4 Replies

2. Shell Programming and Scripting

Read input from file and pass it to sub shell

i have a scenario where in i have to monitor jobs which run in different servers, The job details(job name, host server, etc) are present in a dat file. I have written a script a script which reads the details from the dat file and calls a local method where the job monitoring logic is present.... (2 Replies)
Discussion started by: abubucker0
2 Replies

3. 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

4. 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

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. Shell Programming and Scripting

Call java program from shell and pass values

Hi All, Can anybody please help me with how can i call my java program from shell and also pass parameter along with it so that the program can interpret the value/int and update the database. Thanks in advance Neha (1 Reply)
Discussion started by: Neha Goyal
1 Replies

7. 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

8. Shell Programming and Scripting

Can we pass an array of strings from a Perl Program to a Shell Script?

Hi Folks, The subject is my question: Can we pass an array of strings from a Perl Program to a Shell Script? Please provide some sample code. Thanks ---------- Post updated at 11:52 PM ---------- Previous update was at 11:43 PM ---------- I got it. Its here:... (0 Replies)
Discussion started by: som.nitk
0 Replies

9. Programming

How to pass C array as input to Shell script

Hi, In the below C code , i want to pass the array to a unix shel script. my script should called as ex myscript 1,2,3 #include <stdio.h> int main() { int a={1,2,3}; } Thanks, Arun (1 Reply)
Discussion started by: arunkumar_mca
1 Replies

10. Shell Programming and Scripting

pass the input to invoking script

How to pass the input to the execution script ex: test1.sh contains #! usr/bin/sh read val echo $val test2.sh contains #! /usr/bin/sh ./test1.sh now I am calling test2.sh thro command line and I want to pass the input to the script test1.sh from test2.sh ..I mean not... (6 Replies)
Discussion started by: vastare
6 Replies
Login or Register to Ask a Question