Unix Programming - call external program from script

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > September 2004 > call external program from script





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author call external program from script
dafella24

2004-09-22, 9:22 pm

I want to assign the return value from an external c program to a
varible in my bash script. (Ex. I have a program call foo (executable)
returns an int. Normall you just do ./foo arg to invoke it in a shell,
but now I want to do something like $ret=foo arg in a script.
Helpppppppp!

thanks
Måns Rullgård

2004-09-22, 9:22 pm

bi90261@yahoo.com (dafella24) writes:

> I want to assign the return value from an external c program to a
> varible in my bash script. (Ex. I have a program call foo (executable)
> returns an int. Normall you just do ./foo arg to invoke it in a shell,
> but now I want to do something like $ret=foo arg in a script.


../foo
ret=$?

--
Måns Rullgård
mru@mru.ath.cx
SM Ryan

2004-09-23, 9:13 am

bi90261@yahoo.com (dafella24) wrote:
# I want to assign the return value from an external c program to a
# varible in my bash script. (Ex. I have a program call foo (executable)
# returns an int. Normall you just do ./foo arg to invoke it in a shell,
# but now I want to do something like $ret=foo arg in a script.
# Helpppppppp!

#!/bin/sh
root=`ls -ld /`
echo ---------------------------
st=$?
echo 'root ls: ' $root
echo 'ls status: ' $st
echo ---------------------------
stdout=`mkdir /error`
st=$?
echo 'stdout: ' $stdout
echo 'status: ' $st
echo ---------------------------
stdout=`mkdir /error 2>&1`
st=$?
echo 'stdout+stderr: ' $stdout
echo 'status: ' $st
echo ---------------------------


---------------------------
root ls: drwxrwxr-t 36 root admin 1224 22 Sep 06:47 /
ls status: 0
---------------------------
mkdir: /error: Permission denied
stdout:
status: 1
---------------------------
stdout+stderr: mkdir: /error: Permission denied
status: 1
---------------------------


For ksh and derivatives, you can use nestable $(...) as well as `...`.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
One of the drawbacks of being a martyr is that you have to die.
Stephane CHAZELAS

2004-09-23, 9:13 am

2004-09-23, 11:33(-00), SM Ryan:
[...]
> #!/bin/sh
> root=`ls -ld /`
> echo ---------------------------
> st=$?


You get echo status there.

[...]
> For ksh and derivatives, you can use nestable $(...) as well as `...`.

[...]

`...` are nestable too, but in a less convenient way:

echo `echo \`echo \\\`echo foo\\\`\``

$(...) is better that `...` not that much because they are
nestable but because they don't interfer with backslash
processing.

ls '\'

for instance, lists the '\' file when outside backticks, but
lists the '' file when inside:

$ ls '\'
\\: No such file or directory
$ : `ls '\'`
\: No such file or directory
$ : $(ls '\')
\\: No such file or directory

--
Stephane
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com