How to create my own command in linux - linux

I wrote a simple cpp code by using vi. I want to use cat command inside this program (by using execl and it will take my file as argument). I also have to write my own command. For example it will work like that: $mycommand filename. And cat command which is inside my cpp code will show my cpp file's content. I'm new so I don't know how to do it. Do you have any suggestions?

1) Redirecting output
The output of the cat command should go to the standard output of your program.
For this your program should pass its standard output FD to the exec command. This will be the default case. So you don't have to do anything extra.
2) cat on the source file
Now, the cat thing : "And cat command which is inside my cpp code will show my cpp file's content. " This statement does not explain your need. If you want the cat of the executable you are running, you can do an objdump on your executable. The executable path can be found out from the proc file system with your pid. For a generic case, it does not matter whether the file is a cpp file or not.
3) making command
To make your executable run as a standard command on a terminal, you must add the directory containing the executable to the PATH directories list. You can do so by adding your executable to /usr/local/bin or /usr/bin. (Convention is that it should go in /usr/local/bin since it has been compiled locally). If you don't have administrator privileges on the machine, you must append the source directory to the PATH directory list.

Related

run c++ program in linux terminal with command

I have a cpp file called FileSystem.cpp, while I want to use the linux terminal and call the FileSystem executable file with command
FileSystem -i
" no matter where it located and call it without extension or './' at the front. I tried call it directly from terminal but it said:
FileSystem: command not found
When you type a command into the command line like FileSystem -i, without an explicit path on the command (no / characters in the first word), it looks for the exectuable in your $PATH. You can use the command echo $PATH to see what your current path is.
Normally, on linux, your path will include the directory $HOME/bin if it exists. That's the bin directory in your home directory, so you can put an executable you create (such as FileSystem) in that directory and then run it as FileSystem -i

When should I type ./ to run program and when I should not?

I am very beginner in Linux. I am confused, can someone tell me when we should start the command line with ./ to run a program and when we don't?
I see they do not use it in tutorials, but bash would not recognize the program w/o it.
Thanks a lot,
Sadegh
'.' refers to the current directory. Similarly '..' refers to the parent director.
Consider the following examples:
./foo
Will attempt to execute a program called foo in the current directory.
../foo
Will attempt to execute a program in the parent directory. This and ./foo are called "relative paths" as they are relative to your current position.
foo
Will search for the program in your current PATH, which is a sequence of directories the shell searches to find executables. You can see your value of PATH by enter ing 'echo $PATH'.
Finally, you can give an 'absolute path', such as:
/home/bar/foo
Which will use the whole path starting at root ('/').
when you type a command, linux will finds executables to execute it.
the question is where should it search for?
there is a variable in bash which is called $PATH. lets echo it to see it's content:
$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/game/usr/local/games
so the os looks at these paths
So, what happens if your executable file is not in the $PATH variable????? (for example your scripts)
in this situation you should tell it where your executable file is!
if it is in current directory run this:
./program_name
./app is specifying the path to app. Same as /usr/bin/app is specifying a path.
Unless the directory containing the app is on your PATH then you need to specify a path.

Execute commands in a file

I am using the 2003 textbook - http://www.amazon.com/Unix-Shell-Programming-3rd-Edition/dp/0672324903
My OS is linux L-ubuntu 13 which is not based on POSIX (I think)
It says that I can store who | wc -l in a file called nu and then execute nu. But, before that I need to make this file executable by using chmod +x file(s). This does not work. How do I make the nu "command" work ? I know I can do it by naming nu as nu.sh and then doing bash nu.sh, but I want to try this way also.
To execute a file that is not in the PATH, you must give a properly qualified directory name. While giving the name of the file in the current directory is sufficient as an argument to a program, in order to execute a shell script or other executable file, you must give at least a relative path. For example, if the file is in your home directory, which is also the working directory, any of the following are acceptable:
./nu
~/nu
/home/username/nu
However, simply nu will only attempt to search the PATH, which probably includes places such as /bin, /usr/bin, and so on.

autoconf substitute path in script

I have a project where there are several helper scripts that call the main executable with different command-line options. Right now, the scripts assume the executable is in the same directory, so the calls to the executable in the script look like ./my_program. This, however, is not very flexible. What if the program is installed in the /usr/bin directory, and is not in the current directory?
Is there a way, using automake or autoconf, to generate these scripts, and substitute the calls to the executable with either ./my_program or just my_program, depending on whether or not the executable is already installed?
Sure. IMO the simplest solution with autotools would be:
create new m4 macro under m4/ folder that finds a path of your program, and sets it to a variable.
For example, you created a macro:
MY_PROGRAM_PATH_CHECK([action-if-found], [action-if-not-found])
This macro creates MY_PROGRAM_PATH variable if path is found.
configure.ac
MY_PROGRAM_PATH_CHECK(,[AC_MSG_ERROR([my_program path not found, woot?])
AC_SUBST(MY_PROGRAM_PATH)
AC_CONFIG_FILES([src/script1.sh], [chmod +x src/script1.sh])
AC_CONFIG_FILES([src/script2.sh], [chmod +x src/script2.sh])
convert your scripts to .in files, so the substitution would happen:
src/Makefile.am
bin_SCRIPTS = script1.sh script2.sh
src/script1.sh
#MY_PROGRAM_PATH#/my_program --option1
src/script2.sh
#MY_PROGRAM_PATH#/my_program --option2

Doubt regarding executable files in linux

I have a program written in C, which is named computeWeight.c and to compile it i use the following code
chaitu#ubuntu:~$ gcc -Wall -o computeWeight computeWeight.c
//to execute it:
chaitu#ubuntu:~$ ./computeWeight
Do i have any mechansim where i can directly use as mentioned below,
chaitu#ubuntu:~$ computeWeight
Should i be changing any permissions on the executable to get this?
You need to add "." to your path. Some people regard this as dangerous, though. See for instance http://www.arsc.edu/support/policy/dotinpath.html .
The $PATH variable define the places where linux would look for executables (try typing echo $PATH in a terminal). You need to put that file in one of those places. One way is to add a bin folder in your home directory, put the executable file there, and add this line (which adds the bin directory in your home folder to the search path) to your .cshrc file so that it'd be executed for every shell:
set PATH = ($PATH $HOME/bin)
With that said I don't think typing ./ is that bad.
export PATH=$PATH:.

Resources