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