Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Anyway to get return value of c program from command line?

Tags:

c

linux

I understand if I write a bash script I can get the return value, but is there anyway to get the return value without scripting, and just command line?

like image 926
Brandon Yates Avatar asked Nov 22 '11 16:11

Brandon Yates


People also ask

How do you find the return value of a command?

The return value of a command is stored in the $? variable. The return value is called exit status. This value can be used to determine whether a command completed successfully or unsuccessfully.

How will you check the return value of the executable?

$? will give you the last exit code of an executed command/app. You can run your app as normal and then immediately after check what $? has returned and based on that tweak your script logic.

How do I check the return value of a program in Linux?

In Linux, run the program at the command prompt and then use the echo command to confirm the return value: $ echo $? At the command prompt in Windows, you must code a batch file to fish out the return value.

What is return in command?

return command is used to exit from a shell function. It takes a parameter [N], if N is mentioned then it returns [N] and if N is not mentioned then it returns the status of the last command executed within the function or script. N can only be a numeric value. Syntax: return [N]


2 Answers

Yes, the same way you'd do in a Bash script. Run your program like this:

./your_program; echo $? 
like image 177
jweyrich Avatar answered Oct 14 '22 13:10

jweyrich


In light of the invalidation of the previous answer (good point, Carl Norum), let me re-phrase my comment as an answer:

BASH stores the return value of the previously run command in the variable $?. This is independent of the programming langauge used to write said command (the command can also be a shell internal).

like image 38
gspr Avatar answered Oct 14 '22 13:10

gspr