LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-02-2007, 10:43 AM   #1
sleepyjake
LQ Newbie
 
Registered: Sep 2007
Posts: 4

Rep: Reputation: 0
ubuntu newbie helloworld will not run


Hello,
I have the latest version of Ubuntu installed and now I am trying to learn how it works.

Problem: I cant get a program that I wrote to run.

I wrote the classic "Hello world" and compiled it in the terminal with the following statement: gcc -o hello hello.c (there were no complaints from the system)

I checked via ls -l for a proper output file and found hello and it has the proper permissions for execution. when I try to run it from the command prompt $ hello it doesnt run and I get the following

The program 'hello' can be found in the following packages:
* hello-dbs
* hello
* hello-debhelper
Try: sudo apt-get install <selected package>
bash: hello: command not found


In suggestions?

Thanks in advance, rlh

Last edited by sleepyjake; 09-02-2007 at 10:44 AM.
 
Old 09-02-2007, 10:48 AM   #2
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
You need to use "./hello" to tell the shell to execute the file in the current directory. Otherwise, the shell will search for directories listed in $PATH for "hello" and since it's not in any of those directories, you'll get the "command not found" message.
 
Old 09-02-2007, 11:21 AM   #3
sleepyjake
LQ Newbie
 
Registered: Sep 2007
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks. It works. I did a search of shell commands but did not see ./

Any suggestions were to look for this command and possibly other things I need to know.

Thanks again
 
Old 09-02-2007, 11:22 AM   #4
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
"./" isn't a command. "." is shorthand for the current directory, so by using "./hello" you're really giving the shell the absolute path to "hello".
 
Old 09-02-2007, 11:32 AM   #5
sleepyjake
LQ Newbie
 
Registered: Sep 2007
Posts: 4

Original Poster
Rep: Reputation: 0
Ok.
So even though I have navigated via the terminal to the directory where I have stored the source and compiled and linked the file. I need to tell the shell where I am via ./ ?
 
Old 09-02-2007, 11:36 AM   #6
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Yes, because the shell looks for executables in the directories listed in your $PATH environment variable (you can see which directories are in there with "echo $PATH").
 
Old 09-02-2007, 12:33 PM   #7
b0uncer
LQ Guru
 
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Rep: Reputation: Disabled
You should read the pages in linuxcommand.org - they're really helpful and surely clear some things (like this, for example). There are some "shortcuts" in Linux shells, or ways to express something shorter than it actually is, and the dot is one of them (it's a file that always refers to the directory where it is in) - two dots (..) is another file found in every other directory than root directory, and it refers to the upper/parent directory (the dir where the directory is in, that has the two dots file in..got it?)

Like said above, when you issue a command, your shell tries to find the executable for you. There are several ways to execute an executable, for example:
- by giving a full path to it, and the executable name, like /bin/bash
- by giving just the executable name, for example just bash
In the latter case the shell doesn't automagically know where 'bash' (or anything) is, but it searches certain places where it can be. Usually (but can be configured otherwise if I'm right) it first checks all the directories mentioned in $PATH environmental variable (order matters - the executable is executed from where it's first found). If it's found somewhere in there, it's executed there; if not, then the shell usually checks to see if the file is in the current working directory. So if you have executables that have exactly the same name (but in different places), your shell runs the one that is found first. To make sure you run the right one, you can provide the full path to the executable, telling your shell to launch just that executable. And because you don't want to type in your full current-working-directory-here name, you use a special file (the dot, '.') as a "shortcut" to the directory: there ./ means just the same as if you had typed the full current working directory path.

Ubuntu has this nice feature not in every distribution that if you type in a command name that is not found (from $PATH; I'm not sure if it's even checked against the parent working directory, as your example seems to show..), Ubuntu checks to see if there's a program of that name in some package that's available in it's reposity. If yes, it kindly tells you the package name you need to install to obtain the executable you tried to run..
 
Old 09-02-2007, 04:11 PM   #8
sleepyjake
LQ Newbie
 
Registered: Sep 2007
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks for all the help.

linuxcommand.org is certainly a place to spend some time!
 
  


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
compiling HelloWorld with QT fakie_flip Programming 1 01-18-2006 01:12 PM
Kdevelop can't run the Helloworld(in C++,kde) made by itself tygk Mandriva 14 09-13-2005 10:16 AM
Anyone have a HelloWorld Python Qt script? SheldonPlankton Programming 1 07-22-2004 02:52 PM
how can i run my helloworld servlet in apache-tomcat-mod_jk? doublefailure Linux - Software 1 04-28-2003 08:33 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:25 PM.

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