Returning Strings from C program to Unix shell script

Hi,

I'm having a requirement where I need to call a C program from a shell script and return the value from the C program to shell script.
I refered a thread in this forum. But using that command in the code, it is throwing an error

clear_text_password=$(get_password)

Error: bash: get_password : command not found

I have compiled the code get_password.c , the executable is generated and there's only one display statement in the code.

And also please let me know whether I can pass any arguments to the C program or not. If so, what's the way to do that.

Thanks for the help in advance.

regards,
venkatesh.

is the binary get_passwd available in the expected directory / the directory from which the shell script runs ?

if not, make sure to have the absolute path of the binary in the shell script

Yes, you can pass arguments to binary ( C program )

./run arg1 arg2 arg3

catch in the program with argv

BTW shouldn't this be a new thread ?

Right you are... split and moved to a new thread.

The shell doesn't know where to find your command "get_password".
You could put it into a well known location (eg. "/usr/local/bin", but beware that that directory is often insecure so someone might be able to substitute a trojan in there), or add the current directory (".") to the PATH (which is a very bad thing to do!).
My preference is to create a new specific "bin" directory for bespoke tools (we use "/opt/sup/bin" in my current place of work), and add it to the PATH.

I would expect...

clear_text_password=`/path/to/app/get_password`

Hi All,

   Thanks for the replies.
    Please see the below shell script.
    \# ! /bin/bash
    echo " In the program"
    set output = '/root/Desktop/get_password'
    if [ $? -eq 0 ] 
    then
     echo $output
    fi
    output: In the program

   After displaying the statment "In the program", 
   it's just displaying a blank line.
   I tried putting the statment echo $\{output\} also, But it's of no use.
   Can I give
   set output ='/root/Desktop/get_password arg1 arg2'
   where arg1 and arg2 are the mandatory arguments that need to be passed to the get_password.

Note : The executable file get_password is there at the above mentioned location and also I'm printing only the password at the console.

Thanks & regards,
Venkatesh.

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.

Try

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

Why do you need "set" ?

Why do you keep programs in a Desktop directory?

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.

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. :wink: The correct syntax would be:

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

bakunin

Hi,

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

regards,
venkatesh

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.

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

When was that edict decreed?

I would strongly recommend reading Kernighan & Ritchie (K&R).
Basically C doesn't work like shell, and that book will give you an excellent grounding.

I too was supprised by the "don't use backtick" comment but did some research

From Dave Korn's ksh93 manpage:

The standard output from a command enclosed in parentheses preceded by a dollar sign ( $( ) ) or a pair of grave accents ( ` ` ) may be used as part or all of a word; trailing new-lines are removed. In the second (obsolete) form, the string between the quotes is processed for special quoting characters before the command is executed

Aagh, the is always something new to learn!

Hm, so that may be true for "ksh" post 93, but what about "sh"?

I personally don't use [, or [[ as my goal is portability. :slight_smile:

guys,
i have the same problem,
i have a C program, i want to call this program from a Unix Shell Script ??

Help please ..

i tryed what you just said earlier but didnt Work... :frowning:

Post *exactly* what you tried between CODE markers.

Shell is rather strict about punctuation.