LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 08-19-2008, 07:07 AM   #1
Ashkan_s
Member
 
Registered: Jul 2008
Distribution: Fedora
Posts: 77

Rep: Reputation: 22
Smile create a command in linux


Hi all
I want to write a c program that can be run like a command in command line
I know that I should use argv and argc. But what I don't know is that how to make it a command. I wrote a simple program:
Code:
#include <stdio.h>

int main(int argc, char **argv)
{
	printf(argv[1]);
	printf("\n");
	
	return(0);
}
Now when I want to use it in console I have to write :
./a arg1
in the directory in which is my program.

(my program name is a.out)

but I want to write without ./ :
a arg1

anywhere like other commands (in any directory)

what should I do?
 
Old 08-19-2008, 07:12 AM   #2
nigelc
Member
 
Registered: Oct 2004
Location: Sydney, Australia
Distribution: Mageia 7
Posts: 406
Blog Entries: 4

Rep: Reputation: 80
mkdir ~/bin
put in bin
& it should work
 
Old 08-19-2008, 07:13 AM   #3
bhaslinux
Member
 
Registered: Oct 2003
Location: UnitedKingdom
Distribution: Debian Bullseye
Posts: 357

Rep: Reputation: 49
$ export PATH=$PATH:.
and then execute your command
 
Old 08-19-2008, 07:17 AM   #4
Ashkan_s
Member
 
Registered: Jul 2008
Distribution: Fedora
Posts: 77

Original Poster
Rep: Reputation: 22
Thanks nigelc

It works successfully
 
Old 08-19-2008, 07:22 AM   #5
Ashkan_s
Member
 
Registered: Jul 2008
Distribution: Fedora
Posts: 77

Original Poster
Rep: Reputation: 22
Thank you too bhaslinux

It works successfully too
 
Old 08-19-2008, 09:02 AM   #6
b0uncer
LQ Guru
 
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Rep: Reputation: Disabled
I'll write a little longer answer, read if you like..

To be able to run the program ("command") without mentioning the directory it is in (./ stands for "inside this current working directory" -- the dot means current directory and the slash has the same meaning as in any other path) you need to put the program into a directory that is listed in your $PATH environment variable, which contains directories that are searched for executables automatically when you type in something that could be a "command". For example /bin, /usr/bin, /sbin and /usr/sbin are usually such directories. Try to run
code]echo $PATH[/code]
and you should see which directories are listed. There are several directories because some programs are different from others in some way -- for example administrative executables are usually placed into an sbin directory rather than "plain" bin directory, and so on. If you don't want to move the program into a directory that is listed in $PATH, you can do it the other way around (as suggested above) and alter the environment variable to contain the directory where the executable is -- effectively forcing the directory to be looked in for executables. Note that if there are two executables that have the same name but are in different directories, not both are executed -- one of the directories is "scanned" first and when the first match is found, that program is executed.

Altering your environment variable the way bhaslinux mentioned does no permanent changes - next time you log in (or start another shell) the environment variable is back to "normal". To make permanent changes you must edit your profile file (for example) so that each time you run a shell (or log in), the environment variable is altered. There are several files that can do this, and the one you need to use depends on your needs (do you need system-wide change for all users, only for yourself, for login shells or not, ...)

And do understand that it might be dangerous to alter your $PATH contain any directories, like your home directory, especially if you put them before the system-wide executable directories, because in that case if somebody placed a harmful program into such directory (usually regular users can't write to system-wide executable directories, but you can write to your own home directory -- and anybody that gets into your user account!) and you happened to type in it's name, it was executed and possibly caused harm to you. It's difficult to accidentally type in "send_my_files_all_over_the_net" if there was such an evil program placed into a directory that was in $PATH, but a clever bad guy might name it so that a small slip caused you to run it -- like "/." or "'" or " " or something else people usually don't think can be executable names
 
Old 08-19-2008, 10:29 AM   #7
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
I think it is also worth mentioning that your program can be easily built with a 'sesnsible' name. For a simple case like your example, run:
Code:
make theBaseNameOfYourSourceFile

eg
make helloWorld
This will create the executable of the same name as your C source file, in the current working directory. When you start creating more complex programs, you can use a Makefile to accommodate the additional complexity of the build process.
--- rod.
 
Old 08-19-2008, 11:34 PM   #8
Ashkan_s
Member
 
Registered: Jul 2008
Distribution: Fedora
Posts: 77

Original Poster
Rep: Reputation: 22
Thank you b0uncer and theNbomr
I understood it well with your answers and now I Know why I should do that things.
 
  


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
[SOLVED] command to create encrypted password student04 Linux - Security 8 09-15-2013 08:44 PM
command to create shortcut for an application in the start menu for linux neerajchaudhari Linux - Software 1 08-29-2005 03:11 AM
Linux shell command for makefile.in to create a text file and write to it alix123 Programming 8 01-07-2005 08:18 AM
How can I create a new command? soner Linux - General 2 10-26-2004 02:13 PM

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

All times are GMT -5. The time now is 07:54 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