LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-05-2007, 02:30 AM   #1
araomai
LQ Newbie
 
Registered: Apr 2007
Location: Spain / China
Distribution: Ubuntu, Debian
Posts: 8

Rep: Reputation: 0
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?
 
Old 05-05-2007, 02:38 AM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
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.
 
Old 05-05-2007, 06:55 AM   #3
elsheikhmh
Member
 
Registered: Aug 2004
Location: Cairo, Egypt
Distribution: Slackware
Posts: 101

Rep: Reputation: 15
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
 
Old 05-06-2007, 06:44 AM   #4
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Any way to get "Alice"; "Call of Duty" series and "Descent 3" to work? JBailey742 Linux - Games 13 06-23-2006 01:34 PM
execute a script every 5 minutes "cron" ALInux Linux - General 1 02-10-2006 02:36 AM
running a script: ". scriptname" vs "./scriptname" to execute beeblequix Linux - Enterprise 3 11-07-2005 04:40 PM
remove the "Run or Display?" when I execute a script llee Programming 5 05-05-2003 11:21 AM
remove the "Run or Display?" when I execute a script llee Linux - Software 0 05-02-2003 02:52 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration