0

Im not sure if it is possible, but would like to know if i can use the result of a main() function as the command line argument for another program. im currently using gcc compiler on linux so i figure it would look something like this:

./listcommandlinearugments ./generaterandomstring

but this does not work... in the above example the first program (listcommandlinearguments) simply lists ./generaterandomstring as a command line arugment instead of using the result of the main funtion (0) because it was int main() and returned 0.

dbush
  • 205,898
  • 23
  • 218
  • 273
  • Are you using Linux? – user253751 May 23 '22 at 14:38
  • yes im using linux – Kevin K May 23 '22 at 14:39
  • Does this answer your question? https://unix.stackexchange.com/questions/27428/what-does-backquote-backtick-mean-in-commands – user253751 May 23 '22 at 14:41
  • CAn you calrify the question? Are you looking for a shell solution, using two programs as-is? Or can you modify the `listcommandlinearugments` source code? The tag suggests that it's C source code. – MSalters May 23 '22 at 14:41
  • i tried ./listcommandlinearugments | ./generaterandomstring and got no result. the program to list command line arugements should have showed the result of generaterandomstring.c as a command line arugment – Kevin K May 23 '22 at 14:59
  • to try and clarify my question, i am trying to use the return 0; from a main function in one program as the command line argument for another program. So one program returns 0 and the other program returns that 0 was a command line argument – Kevin K May 23 '22 at 15:03
  • sorry if im not good at explaining this stuff, im new to programming – Kevin K May 23 '22 at 15:04
  • The duplicate notation is for bash. This is a c question requesting usage of exec. – netskink May 23 '22 at 15:42
  • @KevinK are you trying to write a bash script to call a c program, or a c program to call another c program? Your question mentions C, but your tags show bash and not C. – netskink May 23 '22 at 15:43
  • @netskink i think its a bash script because its a text file that i wrote the code into. the only thing is i ran it through a gcc compiler so its what i think is an executable file now – Kevin K May 23 '22 at 16:45

1 Answers1

1

You can use backticks to insert the output of a program into another command:

./generaterandomstring `./listcommandlinearugments`

EDIT:

Since you want the return value of the listcommandlinearugments, run that first, then use $? to get the return value:

./generaterandomstring
./listcommandlinearugments $?
dbush
  • 205,898
  • 23
  • 218
  • 273
  • 2
    Aren't we supposed to prefer `$()` these days? – 2e0byo May 23 '22 at 14:42
  • i tried this and there was no sign of the program in backticks being run at all. the program to list command line arguments said that there were no additional command line arguments when i executed " ./commandlinearguments (backticks)`./generatearandomstring`(backticks) " – Kevin K May 23 '22 at 14:57
  • This actually worked.. somewhat... it passes the string inside of the printf in one program as the command line argument to the other program!!! however i was originally trying to pass the returned value from the first program as the command line argument of another program.. but ill take it! Thx a ton! – Kevin K May 23 '22 at 15:27
  • im having trouble getting it to return the return value of the second program. The complexity is that my second program takes one command line argument itself. Example: i tried this: ./comandlinearguments ./stringtolowercase HELLO $? and its simply returning ./stringtolowercase, HELLO and 0 as the command line arguments – Kevin K May 23 '22 at 16:35
  • i got some functionality with ./commandlinearguments (backtick)./returnzero(backtick)$? However i cant seem to get it to work with a command line argument for the second function example ./commanlinearguments (backtick)./stringtolowercase HELLO(backtick)$? maybe something is wrong with my code, is that how you would add a command line argument to the second function? – Kevin K May 23 '22 at 16:51