LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   can not execute a C script call "if" (https://www.linuxquestions.org/questions/programming-9/can-not-execute-a-c-script-call-if-551437/)

araomai 05-05-2007 02:30 AM

can not execute a C script call "if"
 
Hi,

I am using Ubuntu 7.04.

I am starting to test C on Linux and something I do not really understand has happened. I created this simple script called "if.c":

#include <stdio.h>
int main(void) {
int test = 1;
if (test) {
printf ("*** Value of the test is %d.\n", test);
}
return 0;
}


So, the thing is...

1) If I compile it and name it "if":

$ gcc -g -o if if.c

If compiles OK, but when I execute it:

$ if
>


It doesn't run. I need to stop it with ctrl+c.


2) However, if I compile it with another name:

$ gcc -g -o maybe if.c

It does run!!!

$ maybe
*** Value of the test is 1.


Somebody knows what's going on? Is there any conflict with using "if" as the name?

Dark_Helmet 05-05-2007 02:38 AM

Quote:

Is there any conflict with using "if" as the name?
Yes. Specifically the bash builtin command "if" that you see used in scripts. More specifically, you will have a conflict with any other executable found in your PATH. That's why you should always preface your newly compiled executable with "./" as in:
Code:

./if

or

./maybe

Also, a C program is not commonly referred to as a "script"; it's more commonly referred to as a program. Calling it a "script" will likely lead to confusion.

elsheikhmh 05-05-2007 06:55 AM

Quote:

$ if
>

It doesn't run. I need to stop it with ctrl+c.
No, bash builtin's "if" runs and enters a kind of multiline mode after you hit enter, showing the prompt ">" waiting for you to enter the rest of the IF condition. It will terminate normally (not gracefully) by end-if keyword: "fi". try it:
Code:

$ if
> fi
sh: syntax error near unexpected token `fi'

-Mustafa

jim mcnamara 05-06-2007 06:44 AM

What everyone is telling you:
1. You created a compiled program that is named the same thing as a word in bash. Bash is your shell language.

2.
Code:

which if
will tell you the type of command or file you get by typing the word "if".

3. putting ./ in front of a filename means "use the current working directory" to find the file.


All times are GMT -5. The time now is 07:41 PM.