Returning Strings from C program to Unix shell script


 
Thread Tools Search this Thread
Top Forums Programming Returning Strings from C program to Unix shell script
# 8  
Old 11-25-2007
Java Passing C variable to Unix in a C program

Hi All,

I'm executing unix commands from my C script.
Using system("unix command").
Please see the below code:
main(int argc, char *argv[])
{
system("hostname > hostname.txt");
}
For removing the hard coding in the above code. I tried this.
main(int argc, char *argv[])
{
system(" hostname > argv[1]");
}
But the output is getting stored in the file "argv[1]".
I tried using $argv[1], But it didn't work.
Please let me know how can I achieve this.

Thanks & Regards,
Venkatesh.
# 9  
Old 11-25-2007
Quote:
Originally Posted by venkatesh_sasi
set output ='/root/Desktop/get_password arg1 arg2'
Try

Code:
output=`/root/Desktop/get_password arg1 arg2`

Why do you need "set" ?

Why do you keep programs in a Desktop directory?
# 10  
Old 11-25-2007
Hi porter,

I have just posted that code as an example. I'm not storing the programs on Desktop.
I tried giving output= '/root/Desktop/get_password' and it's working fine.
I gave a space after '='.
When I tried giving output= '/root/Desktop/get_password arg1 arg2' , it's throwing an error.
Error: No such file or directory.

regards,
venkatesh.

Last edited by venkatesh_sasi; 11-25-2007 at 06:01 AM..
# 11  
Old 11-25-2007
Porter was telling you about a syntax error in your script:

set output=.....

is wrong, use

output=....

instead. And, btw., don't use backticks, as they are an (outdated) abomination in the eyes of The Lord. ;-) The correct syntax would be:

output="$(/path/to/program arg1 arg2 arg3)"

bakunin
# 12  
Old 11-25-2007
Hi,

Thanks a lot.
output="$(/path/to/program arg1 arg2 arg3)"
This worked.Smilie

regards,
venkatesh
# 13  
Old 11-25-2007
Hi All,

I'm executing unix commands from my C script.
Using system("unix command").
Please see the below code:
main(int argc, char *argv[])
{
system("hostname > hostname.txt");
}
For removing the hard coding in the above code. I tried this.
main(int argc, char *argv[])
{
system(" hostname > argv[1]");
}
But the output is getting stored in the file "argv[1]".
I tried using $argv[1], But it didn't work.
Please let me know how can I achieve this.

Thanks & Regards,
Venkatesh.
# 14  
Old 11-25-2007
Quote:
Originally Posted by venkatesh_sasi
But the output is getting stored in the file "argv[1]".
I tried using $argv[1], But it didn't work.
C doesn't work like a shell script, you can't expect variables to simply expand.

1. Prepare a string
2. use sprintf() or a similar mechanism to write the command along with the argument passed to the program into that string
3. execute system() with this string.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Control not returning from Sqlplus to calling UNIX shell script.

Hello All, I have exactly same issue @vikas_trl had in following link: https://www.unix.com/shell-programming-and-scripting/259854-control-not-returning-sqlplus-calling-unix-shell-script.html I wonder if he or somebody else could find the issue's cause or the solution. Any help would... (4 Replies)
Discussion started by: RicardoQ
4 Replies

2. Shell Programming and Scripting

Control not returning from Sqlplus to calling UNIX shell script.

Hello All, I have a UNIX script which will prepare anonymous oracle pl/sql block in a temporary file in run time and passes this file to sqlplus as given below. cat > $v_Input_File 2>>$v_Log << EOF BEGIN EXECUTE IMMEDIATE 'ALTER SESSION FORCE PARALLEL DML PARALLEL 16'; EXECUTE... (1 Reply)
Discussion started by: vikas_trl
1 Replies

3. Shell Programming and Scripting

Control from UNIX script is not returning to the Parent program

Hi All, My program flow is as below Windows batch -- > Cygwin batch --> zsh script There are multiple Cygwin batch scripts that are called from Windows batch file . But when i am executing the first cygwin batch script the control goes to the zsh file and executes and stoping from... (1 Reply)
Discussion started by: Hypesslearner
1 Replies

4. Shell Programming and Scripting

how to execute a unix shell script from a java program

Hi All, well , i am facing this problem.. i have tried a few sample codes but there isn't any solution . could anyone please give a sample code as of how to do this... Please see the below details...and read the details carefully. I have written some code, logic is 1)from... (4 Replies)
Discussion started by: aish11
4 Replies

5. Shell Programming and Scripting

Returning to shell from previous script

Found myself stuck on this seemingly trivial issue. I have this script which call other shell files to do their deeds. The shell files in turn call some other programs as well. My problem is that in two of these shell files, the control doesnt return to next command in script unless the Enter key... (2 Replies)
Discussion started by: DoesntMatter
2 Replies

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

7. AIX

The shell script is not returning proper result

Can anybody pls look into this script and tell me where I went wrong. After running this script, it is showing like "Trying to overlay current working directory ABORT!!!" :-( ARGCNT=$# if then echo "Two parameters are needed for this shell " echo "Please try again with... (1 Reply)
Discussion started by: clnsharma123
1 Replies

8. Shell Programming and Scripting

shell program for sorting strings in an alphabetical order

Hi, I trying to find the solution for writing the programming in unix by shell programming for sorting thr string in alphabetical order. I getting diffculty in that ,, so i want to find out the solution for that Please do needful Thanks Bhagyesh (1 Reply)
Discussion started by: bp_vanarse
1 Replies

9. Programming

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... (11 Replies)
Discussion started by: satguyz
11 Replies

10. Shell Programming and Scripting

Returning Values (shell Script)

I have an application on Informix 4GL, and I am invoking the shell script from the program, but I need to know if the shell script work fine or not, in order to continue the process. I know that we can use $? to get the final status but this is on unix command. How can I return a value from the... (3 Replies)
Discussion started by: jennifer01
3 Replies
Login or Register to Ask a Question