Passing multiple C program output into a shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Passing multiple C program output into a shell
# 1  
Old 04-15-2013
Passing multiple C program output into a shell

Hi

I have the following C code
Code:
# cat test.c
#include <stdio.h>
main()
{
printf ("The output is : Power\n");
printf ("The output is : No Power\n");
}

The output of this C code is
Code:
# ./test
The output is : Power
The output is : No Power

Now i need to pass this outputs into a shell Script

Eg.,
From the output of C program from print statement "Power" should be grepped and assigned to "var1" of the shell script
From the output of C program from print statement "No Power" should be grepped and assigned to "var2" of the shell script

Please help Smilie

Last edited by Priya Amaresh; 04-15-2013 at 07:46 AM..
# 2  
Old 04-15-2013
Use this Smilie

Code:
#/bin/sh

test > variables.out 2> errorfile

. ./variables.out

echo $var1
echo $var2

exit

A few changes in you C can be useful
Code:
#include <stdio.h>
main()
{
printf ("var1=Power\n");
printf ("var2=No Power\n");
}

If this is not possible, the we should do like
Code:
#/bin/sh

test > variables.out 2> errorfile
var1=$(grep "The output is" variables.out | awk -F":" 'NR==1{print $NF}')
var2=$(grep "The output is" variables.out | awk -F":" 'NR==2{print $NF}')

exit

# 3  
Old 04-15-2013
Here is another approach without using intermediate files:
Code:
#!/bin/ksh

./test | while IFS=':' read s1 s2
do
        (( ++c ))
        [[ $c -eq 1 ]] && var1="$s2" || var2="$s2"
done
printf "var1: %s\nvar2: %s\n" "$var1" "$var2"

# 4  
Old 04-15-2013
@Yoda: Why can't we do this way? Smilie

Code:
#!/bin/ksh

./test | while IFS=':' read var1 var2
do
        (( ++c ))
done
printf "var1: %s\nvar2: %s\n" "$var1" "$var2"

# 5  
Old 04-15-2013
Quote:
Originally Posted by PikK45
@Yoda: Why can't we do this way? Smilie

Code:
#!/bin/ksh

./test | while IFS=':' read var1 var2
do
        (( ++c ))
done
printf "var1: %s\nvar2: %s\n" "$var1" "$var2"

I don't get it! How this code is gonna work? Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing sqlplus output to shell variable

Hi , I am using below code : for i in `ps -ef|grep pmon|awk {' print $8 '}|cut -f3 -d'_'|grep -v '^grep'` do ORACLE_SID=$i export ORACLE_SID; dest=`sqlplus "/ as sysdba" <<EOF set heading off feedback on verify off select DESTINATION from v\\$archive_dest where target in... (5 Replies)
Discussion started by: admin_db
5 Replies

2. Shell Programming and Scripting

Passing multiple arguments to a shell script

Hi Gurus, Need some help with the shell scripting here. #!/bin/ksh ps -ef | grep -i sample.ksh | grep -v grep > abc.txt if then echo "sample.ksh is executing" else echo "sample.ksh is not executing" fi (1 Reply)
Discussion started by: jayadanabalan
1 Replies

3. Shell Programming and Scripting

running C program to output in multiple locations

Hi Guys, What I am looking at doing is to run a C program in my home directory, but output files in multiple directories BUT not at the same instance. For e.g. 1st instance: Run program.c and output results in path /aaa/bbb/ccc/ 2nd instance: Run program.c again and output results... (9 Replies)
Discussion started by: Jatsui
9 Replies

4. Shell Programming and Scripting

Help required in passing multiple arguments from a shell script to a pl/sql block

Hi, hope everyone are fine. Please find my issue below, and I request your help in the same In a configuration file, i have a variable defined as below TEST = 'One','Two','Three' I am trying to pass this variable in to a sql script which is define in a pl/sql block as follows, In the... (1 Reply)
Discussion started by: ramakanth_burra
1 Replies

5. Shell Programming and Scripting

Help me! Format output file using shell program

Hi, I have following input file. I want to generate output file in specific format using shell program. The input file has atleast few thousands of lines, the below are some sample lines. Input file: "ORDER NO"|"ORDER AMT"|"LINE ITEM"|"LINE AMT"|"SALES COMMISION %" ORD3456|5000|LIN345|30|25%... (8 Replies)
Discussion started by: dsubha
8 Replies

6. UNIX for Dummies Questions & Answers

Passing command output as an argument to a shell script

Hi, I have a very small requirement where i need to pass command output as an argument while invoking the shell script.. I need to call like this sh testscript.sh ' ls -t Appl*and*abc* | head -n 1' This will list one file name as ana argument.. I will be using "$1" in the shell... (2 Replies)
Discussion started by: pssandeep
2 Replies

7. UNIX for Advanced & Expert Users

how do collect shell output in a C program

i use the system command to execute a shell command... ca i collect the out put in the form of a string or something using the same C program? (5 Replies)
Discussion started by: damn_bkb
5 Replies

8. Shell Programming and Scripting

Shell program to accept multiple request at the same time

Hi, I got a script which sends the Email to the user based on certain variables received from Tivoli Server Monitoring 6.1. Now to keep track of the mails I have wrote a stored procedure in DB2 as we use DB2 UDB as back end which take the variables that were used to send the mail and store it... (3 Replies)
Discussion started by: tcskurra
3 Replies

9. Programming

Passing Parameters and getting values back from a c program to Shell script

I am having a shell script which has to be called from a C program. I have to pass two parameters to this script. HOw can I do that? eg: int main() { char st1; char str2; // call a shell script call_sh(str1,str2) where call_sh is the name of the shell script. then i need to get the return... (5 Replies)
Discussion started by: Rajeshsu
5 Replies

10. Shell Programming and Scripting

Passing shell variables to awk program..

Hello, Can we pass shell variables like $PATH etc. to a awk program part for example, awk ' { fieldValue=$PATH .... }' file (1 Reply)
Discussion started by: Vishnu
1 Replies
Login or Register to Ask a Question