The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
returning values from shell script to calling C program nehamore Shell Programming and Scripting 2 02-04-2008 11:09 AM
Returning Strings from C program to Unix shell script venkatesh_sasi High Level Programming 24 11-28-2007 04:48 AM
shell program for sorting strings in an alphabetical order bp_vanarse Shell Programming and Scripting 1 10-25-2006 11:41 AM
returning to the parent shell after invoking a script within a script gurukottur Shell Programming and Scripting 5 09-26-2006 08:05 AM
Returning Values (shell Script) jennifer01 Shell Programming and Scripting 3 11-29-2001 06:31 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-12-2005
satguyz satguyz is offline
Registered User
  
 

Join Date: Mar 2005
Posts: 13
Thumbs up Returning Strings from C program to Unix shell script

Hi,
Iam calling a C program from a Unix shell script. The (C) program reads encrypted username/password from a text file , decrypts and returns the decrypted string.
Is there any way i can return the decrypted string to Unix shell program.
My shell script uses the output of the program to login to oracle DB.

C programs main() function returns int. How is it possible to return a String from a C program?

Please help me.

Thanks in advance.

Rao.
  #2 (permalink)  
Old 03-12-2005
reborg's Avatar
reborg reborg is offline Forum Staff  
Administrator
  
 

Join Date: Mar 2005
Location: Ireland
Posts: 4,240
The return value of the main() function is the exit status for the program, it should normally be 0 for correct completion or something differnt for error or special conditions.

Your shell script should not use this value as the output of the program, but rather to be sure that the program has completed successfully. Instead you should write the decrypted password to stdout and read that value from your script.


Code:
somewhere in your C code you would do:

printf("%s", decrypted_password);

make sure that this is the only output from the program.

in your script you would then have something like :
The script snippet here assumes you are using ksh/bash

Code:

...
clear_text_password=$(my_C_program)
if [[ $? -eq 0 ]] ; then
     #The password was returned correctly
     #Do whatever you like here
     echo "The decrypted value is: ${clear_text_password}
else
     echo "Password decryption failed"
     exit 1
fi

  #3 (permalink)  
Old 03-13-2005
satguyz satguyz is offline
Registered User
  
 

Join Date: Mar 2005
Posts: 13
Thank you very much reborg.
I am going to follow whatever approach you suggested. I have a small concern here. In future, if some one comes and adds one more printf to the program (for debugging purpose), the script won't function properly right??

Is it possible to write the output values to a common memory which can be used by Unix shell as well as C program. Once the C program writes the output to that common memory , can Unix pick the values?
Since the program deals with passwords I can't write the output to a temp text file so that shell script reads the file and delete it.

How about writing the values to environment variables from C and accessing from Unix?

Iam new to Unix and C, please correct me if my questions are dumb.

Thanks
Rao.
  #4 (permalink)  
Old 03-13-2005
reborg's Avatar
reborg reborg is offline Forum Staff  
Administrator
  
 

Join Date: Mar 2005
Location: Ireland
Posts: 4,240
That's not really the way things are done in Unix.

There is an old tenet in Unix programming which says, if you have nothing to say don't say anything, in other words if there is no reason for output from your program don't have any. So taking that into account you should not have any debugging output.

That said you could make sure you have the correct information by making sure the outut of the "correct" printf has a well defined format, by prefixing it with something, like this.


Code:

printf("DECRYPTED_DATA %s", decrypted_password);

he the script would for example become

Code:
clear_text_password=$(my_C_program | awk '/DECRYPTED_DATA/{print $2}' )
if [[ $? -eq 0 ]] ; then
     #The password was returned correctly
     #Do whatever you like here
     echo "The decrypted value is: ${clear_text_password}
else
     echo "Password decryption failed"
     exit 1
fi

  #5 (permalink)  
Old 03-13-2005
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,126
Debug output should go to stderr, not stdout.
  #6 (permalink)  
Old 03-13-2005
reborg's Avatar
reborg reborg is offline Forum Staff  
Administrator
  
 

Join Date: Mar 2005
Location: Ireland
Posts: 4,240
Quote:
Originally Posted by Perderabo
Debug output should go to stderr, not stdout.
Very true, I did provide the alternative because I think the concern was that additional output might be added to the program by someone lazy or not sufficiently informed as to know that.

satguyz, you should read up on the use of stdout and stderr for both your application and your shell script.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:03 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0