Making own command on linux

A shell is an interface to the operating system's services. All process are created by fork() and given a new direction by the exec() process. The trace of forks and calls can be seen from the strace command.

1. Using shell script

This is more like using a shell to create more shell commands. A shell script is an executable which is executed by the terminal or the shell interpreter. Linux default current generation is the bourne again shell or bash. The default commands available for the shell can be written as a script to perform more complex functions and can be run as an executable. Aliases can also be used.

2. Using programming languages like c and c++

The compiled code produces and executable which can be treated as a command. Refer to c++ code snippet for exmple.

#include<iostream> using namespace std; int main() { cout<<"Enter name - "; string s; cin>>s; cout<<endl<<"Welcome "<<s<<". System ready to rumble!!"<<endl; return 0; }

Common ground for points 1 and 2

In both cases, we need to run the commands using ./command in the specific directory. This is not like a true command. All standard commands are stored as executables or as links to the executables in specific directories like /bin/ or /usr/bin/ etc. To actually call the command we do not specify the directory, we just specify the name without ./. There is a PATH variable which stores the locations of all the mentioned folders, which are searched when a command is called. To emulate the working of standard commands, we need to add the directory to the PATH using a command as follows -

export PATH = "$PATH:<directory to be added>"

This will be valid for the particular session on the bash terminal. Therefore, to make the command always available, we need to add this command to the .bashrc or .profile file in the home directory.

3. Complexity increased - Use python

Many packages are available for python which can be used to create commands for us which might actually help us in day to day work instead of using the commands as simple hello world programs. There are two types to classify this use as ->

  1. Standard for different packages.
  2. Using optparse Refer to python code snippet for example on nmap package for python.
#! /usr/bin/env python3 import nmap import sys nm=nmap.PortScanner() i = 0 for ip in sys.argv: i = i+1 if i==1: continue else: nm.scan(ip, '21-443') for host in nm.all_hosts(): print("{} ({})".format(host, nm[host].hostname())) for proto in nm[host].all_protocols(): for kk in nm[host][proto].keys(): if (kk==80) or (kk==443): print("{} : {}".format(proto,kk))

#! is the shebang directive. Using optparse, the commands we make will be very similar to the existing command structure. We can even have the help section and error messages inbuilt which make the commands more interactive.

4. Argparse

The package of optparse is now deprecated and replaced by argparse based on optparse. An example for a simple command to compute squares using argparse -

import argparse parser = argparse.ArgumentParser() parser.add_argument("square", type=int, help="display a square of a given number") parser.add_argument("-v", "--verbosity", type=int, choices=[0, 1, 2], help="increase output verbosity") args = parser.parse_args() answer = args.square**2 if args.verbosity == 2: print("the square of {} equals {}".format(args.square, answer)) elif args.verbosity == 1: print("{}^2 == {}".format(args.square, answer)) else: print(answer)

The output is as follows -

$ python x.py usage: x.py [-h] [-v {0,1,2}] square x.py: error: too few arguments
$ python x.py 4 16
$ python x.py -v 0 4 16
$ python x.py -v 1 4 4^2 == 16
$ python -v 2 4 the square of 4 equals 16

More Info at - Argparse