A question about running functions straight off a terminal
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
A question about running functions straight off a terminal
Can you run functions in C straight off a terminal?
For example, I typed in
getuid()
and then I got a ">" prompt
what would I type in after that to return the user's id?
Also a question about this code
// Writing data
if(write(fd, &userid, 4) == -1) // Write user ID before note data
fatal("in main() while writing userid to file");
write(fd, "\n", 1); // Terminate line
I just need to understand the if(write(fd, &userid, 4) == -1) line.
is the 'if' statement checking to see that the fd, hex address of userid or a dereference?? and the int 4 are all not equal to -1? Is this understanding correct? and the last line is just to add a "\n" to the end of the file right?
Also, where does the write function actually take place if there's an 'if' in front of it?
Also, can anyone make a good irc chat room suggestion that I could go to for questions such as these? I'm gonna have a bunch of questions on C.
the 'if' keyword tests the value of an expression. In this case, the expression is the value of a test for equality, which evaluates to either 0 or non-zero. The values involved in the equality expression are the '-1', and the return value from the write() function. When write() encounters an eror condition, it returns '-1', and sets the value of the global variable 'errno'.
The expression is evaluated by first evaluating the parameters passed to write(), then calling write(), then comparing the return value against '-1'. if(....), simply branches based on the value of the expression, and does not affect the evaluation of the expression.
What you are wanting to do is impossible. C code must be compiled into a binary executable. There is however, something that may get you close. There is a tiny compiler called tcc which will let you run such code as shown withotu compiling first. It does a just-in-time compile of the code. Most simply-written C code can then be run as a script by the compiler.
I found something interesting the other day which does much the same ecept it is using gcc. You could do it from the terminal but it would be tedious typing anything useful in that way. But a small bash script can also be used to produce a JIT program:
For example, I typed in
getuid()
and then I got a ">" prompt
what would I type in after that to return the user's id?
The shell expected the definition of the function. It is a valid syntax in bash and it's similar to C. The shell has simply interpreted your statement as a new function definition and offered the secondary prompt waiting for completion.
So then if I wanted I could just search for the getuid() function in the C /usr/includes and just type it in when the ">" prompt appears, hit enter and it should return the userID, gonna try it out.
Shell functions only superficially resemble C, they are not C. You won't be able to find the definition of getuid and type it in as a shell function.
You seem confused about a number of elementary programming concepts. I don't want to be discouraging, as C is still valuable to understand, but perhaps you should start off with another language and come back to C with a better foundation? There are some excellent free textbooks out there for other languages. I would recommend either the Structure and Interpretation of Computer Programs (if you are seriously interested in understanding software), or one of the excellent introductions to programming in Python (Dive into Python, for example).
So then if I wanted I could just search for the getuid() function in the C /usr/includes and just type it in when the ">" prompt appears, hit enter and it should return the userID, gonna try it out.
Nope. I assumed you've understood what gnashley replied. You cannot run C functions in the shell!
The shell is an interpreted language and it has its own syntax, built-in functions and rules. C is a compiled programming language: you write the code in a text file, compile it and then run the resulting executable. That's it.
In the shell you can define your own functions: they resemble the C syntax just for the opening declaration, for example
Code:
my_fun(){
echo This is my custom shell function.
return
}
This is the reason why the shell expected further input when you typed getuid(). But it is far from running a C function in the shell environment.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.