I am having a situation for which I am looking for some suggestion.
Suppose I write a program which prints the directory names of the directories.
Is it possible to convert this program into a command (just on my system).
Not be alias but via C only.
As long as the file is executable (has the exec x access for the user starting it) and can be seen from the command interpreter (usually bash or sh), you can consider it to be a command.
There will be no difference in running your own file from your path than the ls command for instance.
Also, the C (or C++ ...) language is not a requirement. There are plenty of commands in, for instance, /usr/bin that are a script, meaning they're sh or bash (or even perl)...
access Ensure the file has the x access right (e.g. chmod u+x file)
path Ensure the file is in your PATH, or add an entry in your path (for instance) with PATH=$PATH:mypath
test Test it well before to put it in a path from which other users may have access
Put it in the path. On Linux, for example, you should put it in /usr/local/bin.
First, compile the program and create an executable using gcc program.c -o myexecfile. Then, an executable file named myexecfile is created in the same directory. You can run it by using ./myexecfile.
If you are on Unix(Linux etc.) and want to use it like ls or any other standard command, you need to place it in a directory that is specified in $PATH variable. For example, /usr/local/bin.
Related
My c program file is Numbers.c
cc- Numbers.c -o output.txt
can give me results in another file. But how can I modify that command line, therefore, I can add the results of Numbers.c to $PATH?
I tried:
cc Numbers.c >>PATH
But there is no change in $PATH when I check it;
echo $PATH
How to add an executable program to PATH environment variable?
Make these steps to add a C program to PATH variable.
First of all make a back up from important PATH Variable with echo $PATH > ~/path.txt
Compile the C source code with gcc Numbers.c -o Numbers
Suppose the executable output file Numbers is place under ~/barname.
Run export PATH="$PATH:~/barname" to append ~/barname folder to PATH variable.
Now you can run the executable file i.e: Numbers from any location on Terminal.
I answer to similar question at Iranian Ubuntu community.
and now translate it to English.
Edit
By performing jonathan-leffler notes in comment section, i can change above answer to:
Having many different directories inside PATH environment variable not recommend and have performance issue.
If i want to use specific programs only for myself, create a new directory in current user (me) home folder. mkdir $HOME/bin Then copy Numbers program and all of other programs in to it. for example cp ~/barname/Numbers ~/bin. And finally add only one directory i.e: $HOME/bin to PATH variable by export PATH="$PATH:$HOME/bin".
In a situation when working on a real multi user system, like when work as Sysadmin its better to copy programs to /usr/local/bin and then adding this to PATH by export PATH="$PATH:/usr/local/bin. Now every users can access and run programs.
Say I made and compiled a small program in C to count the bytes of a file, called filebyte. To run it I would use ./filebyte
Now I want to make it universal on bash, like for example to run a php file, I would use bash command php file.php, same way I would like to run my program, filebyte filename.
How do I do this?
Thanks!
I often create a bin/ directory in my home directory, for small custom applications.
You then need to add that directory to your PATH, which is a list of colon-separated paths that your shell searches for executables when you type a name on thr command line.
This is usually accomplished by putting this in your ~/.bashrc file:
PATH="$PATH:~/bin"
Check the environment variable PATH and put the executable in one of the directories listed. You can also put it in a custom directory and then append it to PATH. You can check it by executing printenv PATH
If you want it for your current active shell alone, do
export PATH=$PATH:</path/to/file>
For permanently making the file available add the above line to ~/.bashrc
Why add it in PATH variable, man bash says why,
PATH The search path for commands. It is a colon-separated list of
directories in which the shell looks for commands (see COMMAND
EXECUTION below). A zero-length (null) directory name in the
value of PATH indicates the current directory. A null directory
name may appear as two adjacent colons, or as an initial or
trailing colon. The default path is system-dependent, and is set
by the administrator who installs bash. A common value is
''/usr/gnu/bin:/usr/local/bin:/usr/ucb:/bin:/usr/bin''.
I compiled a silly little "hello world" C program called main.c:
gcc main.c
As expected, a file called a.out appeared, which they say is an executable. From that same directory, if I type
a.out
and hit enter, it says "command not found". But if I type
./a.out
It says "hello world", as desired. I've never seen an executable that requires a './' in front of it to run. Why now?
All executables that aren't in your PATH require an explicit path from root / or the local directory ./ to run. A quick search turns up other threads with essentially the same question:
Why do you need ./ (dot-slash) before script name to run it in bash?
This also has the added benefit of helping with your auto completion in your shell (assuming it supports it). If you type just aTabTab then it will list every executable in your path that starts with "a". However, if you type ./aTab it will probably just auto-complete as a.out since it will only look at executable files in the current directory starting with "a". So, looking at it that way, the "./" actually saves you typing a few keys!
It is standard practice in Unix and Linux not to have the current working directory in the path. If you want to have MSDOS/Windows behavior, alter your PATH variable to include . as the first directory.
It's because the system is looking for a.out or any other exec. file in some special paths. And the current dir in not in that list by default (usually).
look at the list of such paths:
$ env|grep PATH
you can add such current dir to PATH env. variable:
$ export PATH=$PATH:.
But you better avoid doing that and run ./a.out.
Such tech. provides us understanding that we are running specified file from current dir,
not the other file with the same name from another (potentially) dir. So, we know what we run exactly.
When you type something like a.out into a Linux terminal, you're implying that you want to run a command called a.out. By default, the terminal does not look in the current directory for these commands, it looks in PATH - a set of directories for executable programs. It is usually these directories:
/bin
/usr/bin
/usr/local/bin
among others (you can check them all by running echo $PATH)
You have to specifiy the directory directory of your program for it to run, if it is not in one of the directories of PATH. For example:
./a.out works because . refers to the directory you're in
../a.out could work if a.out is in a parent directory (.. refers to the parent)
directory
projectdir/a.out also works, if your program is in the sub-directory, projectdir
That's because a.out is not in your $PATH.
The command you provide is searched in the $PATH (environment variable in linux) by the shell.
$PATH basically is the list of directories. When you provide the executable name, shell searches it in the directories provides by $PATH.
Since a.out is not in your $PATH, you've to explicitly provide the path to a.out.
I've a executable named say "sortx". Now I want to write a C program which transforms this executable into a shell command.
ex:
./sortx numbers.txt
After running the C program on "sortx" what I want is :
sortx numbers.txt
Add the directory in which sortx is present to $PATH. This way you could execute your program locally, like,
sortx numbers.txt
To add directory ~/my_bin to the beginning of the $PATH environment variable, add or update this in your .bash_profile:
PATH=~/my_bin:$PATH
On Linux to make any script or program globally executable (e.g "sortx" rather than "./sortx") you can put the script in wither /usr/bin or /bin -- I prefer /usr/bin :)
I/O Redirection
Advanced Shell Topics: stdin, stdout and redirection
I am programming a small unix shell written in c. I want it to do only some basic commands for now. e.g. ls, pwd, ch
My problem is how do I set the Home directory and Path directory? I want to read the configuration from a text file so it can be easily changed whenever.
I am going to be using execv() to call unix functions such as ls. For example PATH
should determine the directories my shell should use to search for executable programs
when the user types a command
Thanks
They are all simply environment variables that you manipulate e. g. through setenv(3) (run man 3 setenv for details). The variables are HOME and PATH. See also man 7 environ.
Note that setting/changing an environment variable only influences the current process and all processes forked from it after the setting/changing (unlike in Windows, AFAIK).
Check out the function setenv. See man 3 setenv for information about it.
Unix already offers you an environmental variable that contains all the paths where system executables are stored. Retrieve the variable in your code with getenv("PATH"); Each path is separated with a ':' so all you need to do is tokenize and begin searching those paths for the executable your command wants to run. In this function you should also be able to search any path of your choosing for an executable.
You can decide what directory you want to start in ("home directory" as you say) by manipulating the current working directory before the shell prompts with chdir(). You can also use that unix function for implementing a cd command that can be used throughout the run time of the shell.