Shell command programmimg

Bash is a Unix/Linux shell and command language written as a free software replacement for the Bourne shell. Bash is a command processor that typically runs in a text window, where the user types commands that cause actions. The name itself is an acronym for Bourne-again shell, referring to its objective as a free replacement for the Bourne shell.


Shell types

Most LINUX system will usually offer a variety of shell types:

  • sh or Bourne Shell: the original shell still used on Linux systems. This is the basic shell, a small program with few features. While this is not the standard shell, it is still available on every Linux system for compatibility with LINUX programs.
  • bash or Bourne Again shell: the standard GNU shell, intuitive and flexible. Probably most recommended for beginning users while being at the same time a powerful tool for the advanced and professional user. On Linux, bash is the standard shell for common users. This shell is a superset of Bourne shell so commands that work in sh, also work in bash. However, the reverse is not always the case.
  • csh or C shell: the syntax of this shell resembles that of the C programming language. Sometimes preferred by C/Java programmers.
  • tcsh or TENEX C shell: a superset of the common C shell, enhancing user-friendliness and speed. That is why some also call it the Turbo C shell.
  • ksh or the Korn shell: sometimes appreciated by people with a LINUX background. A superset of the Bourne shell; with standard configuration a nightmare for beginning users.

The file /etc/shells gives an overview of known shells on a Linux system:

> cat /etc/shells
/bin/sh
/bin/bash
/bin/tcsh
/bin/csh

Your default shell is set in the /etc/passwd file

> cat /etc/passwd | grep user1
user1:x:501:501::/home/user1:/bin/bash

To switch from one shell to another, just enter the name of the new shell in the active terminal. The system finds the directory where the name occurs using the PATH settings, and since a shell is an executable file (program), the current shell activates it and it gets executed. A new prompt is usually shown, because each shell has its typical appearance:



A bash shell program to read input file, sort, add number lines(nl), remove duplicates and write to new file

uniq

uniq is a Unix utility which, when fed a text file, outputs the file with adjacent identical lines collapsed to one. It is a kind of filter program. Typically it is used after sort. It can also output only the duplicate lines (with the -d option), or add the number of occurrences of each line (with the -c option).

sort

program that prints the lines of its input or concatenation of all files listed in its argument list in sorted order. Sorting is done based on one or more sort keys extracted from each line of input. By default, the entire input is taken as sort key. Blank space is the default field separator.

nl

nl is a utility for numbering lines, either from a file or from standard input, reproducing output on standard output.

#!/bin/bash

# save this program as uniquefruits.sh
# chmod 500 uniquefruits.sh
# to execute type 'sh uniquefruits.sh'

# sort input file 'fruits.txt', remove duplicate entries and save as 'uniquefruits.txt'
sort fruits.txt | uniq | nl > uniquefruits.txt
input:
> cat fruits.txt
apple
mango
orange
pineapple
strawberry
strawberry
apple
grapes
output:
> cat uniquefruits.txt
     1  apple
     2  grapes
     3  mango
     4  orange
     5  pineapple
     6  strawberry


Set Graphics Mode

These statements call the graphics functions. These specified functions remain active until the next occurrence of this escape sequence. Graphics mode changes the colors and attributes of text (such as bold and underline) displayed on the screen.

echo -e. The e switch enable echo's interpretation of special characters.


#!/bin/bash
echo -e "\x1B[30m This is black text \x[0m"
echo -e "\x1B[31m This is red text \x[0m"
echo -e "\x1B[32m This is green text \x[0m"
echo -e "\x1B[33m This is yellow text \x[0m"
echo -e "\x1B[34m This is blue text \x[0m"
echo -e "\x1B[35m This is magento text \x[0m"
echo -e "\x1B[36m This is cyan text \x[0m"
echo -e "\x1B[37m This is white text \x[0m"

echo -e "\x1B[40m This is black background \x[0m"
echo -e "\x1B[41m This is red background \x[0m"
echo -e "\x1B[42m This is green background \x[0m"
echo -e "\x1B[43m This is yellow background \x[0m"
echo -e "\x1B[44m This is blue background \x[0m"
echo -e "\x1B[45m This is magento background \x[0m"
echo -e "\x1B[46m This is cyan background \x[0m"
echo -e "\x1B[47m This is white background \x[0m"
echo -e "\x1B[40m This is black background \x[0m"

echo -e "\x1B[0m all attributes off \x[0m"
echo -e "\x1B[1m Bold On "
echo -e "\x1B[2m Underscore "
echo -e "\x1B[5m Blink on "
echo -e "\x1B[7m Reverse video on "
echo -e "\x1B[8m Conceal on "
echo -e "\x1B[0m all attributes off \x[0m"

output:



Working with environmental variables in Linux

An environment variable is a named object that contains data used by one or more application.

to show environmental variables
  • printenv – Print all or part of environment.
  • env – Print all exported environment or run a program in a modified environment.
# printenv
HOSTNAME=myhostname
SELINUX_ROLE_REQUESTED=
TERM=xterm
SHELL=/bin/bash
HISTSIZE=1000
SSH_CLIENT=172.16.51.15 61242 22
SELINUX_USE_CURRENT_RANGE=
QTDIR=/usr/lib64/qt-3.3
QTINC=/usr/lib64/qt-3.3/include
SSH_TTY=/dev/pts/0
USER=root
...

to set environmental viariables

To set variable only for current shell:

VARNAME="my value"

To set it for current shell and all processes started from current shell

export VARNAME="my value"   

To set it permanently for all future bash sessions

  • add such line to your .bashrc file in your $HOME directory.

To set it permanently, and system wide (all users, all processes)

  • add set variable in /etc/environment
System Variable Meaning To View Variable Value Type
BASH_VERSION Holds the version of this instance of bash. echo $BASH_VERSION
HOSTNAME The name of the your computer. echo $HOSTNAME
CDPATH The search path for the cd command. echo $CDPATH
HISTFILE The name of the file in which command history is saved. echo $HISTFILE
HISTFILESIZE The maximum number of lines contained in the history file. echo $HISTFILESIZE
HISTSIZE The number of commands to remember in the command history. The default value is 500. echo $HISTSIZE
HOME The home directory of the current user. echo $HOME
IFS The Internal Field Separator that is used for word splitting after expansion and to split lines into words with
the read builtin command. The default value is <space><tab><newline>.
echo $IFS
LANG Used to determine the locale category for any category not specifically selected with a variable starting with LC_. echo $LANG
PATH The search path for commands. It is a colon-separated list of directories in which the shell looks for commands. echo $PATH
PS1 Your prompt settings. echo $PS1
TMOUT The default timeout for the read builtin command. Also in an interactive shell, the value is interpreted as
the number of seconds to wait for input after issuing the command. If not input provided it will logout user.
echo $TMOUT
TERM Your login terminal type. echo $TERM
export TERM=vt100
SHELL Set path to login shell. echo $SHELL
DISPLAY Set X display name echo $DISPLAY
export DISPLAY=:0.1
EDITOR Set name of default text editor. export EDITOR=/usr/bin/vim



the alias command

The alias command makes it possible to launch any command or group of commands (inclusive of any options, arguments and redirection) by entering a pre-set string. It allows a user to create simple names or abbreviations (even consisting of just a single character) for commands regardless of how complex the original commands are and then use them in the same way that ordinary commands are used.

syntax:

alias newcommand='oldcommand'

for example the following alias called changes will show all new files created or changed in last 24 hours

alias changes ='find / -mtime -1 -ls | more'


the time command

The linux time utility takes a program name as an input and displays information about the resources used by the program. Also, if the command exists with non-zero status, this utility displays a warning message and exit status.

Example:

> time find / -name *a*
...
real    0m0.819s
user    0m0.145s
sys     0m0.195s


Standard and error redirection in linux shell scripting

The shell and many Linux commands take their input from standard input (stdin), write output to standard output (stdout), and write error output to standard error (stderr). By default, standard input is connected to the terminal keyboard and standard output and error to the terminal screen. Redirection of I/O, for example to a file, is accomplished by specifying the destination on the command line using a redirection metacharacter followed by the desired destination.

Input and output in the Linux environment is distributed across three streams. These streams are:

  • standard input (stdin)
  • standard output (stdout)
  • standard error (stderr)

Stream Redirection:

Linux includes redirection commands for each stream. These commands write standard output to a file. If a non-existent file is targetted (either by a single-bracket or double-bracket command), a new file with that name will be created prior to writing.

Commands with a single bracket overwrite the destination's existing contents.

Overwrite

  • > - standard output

  • < - standard input

  • 2> - standard error

Commands with a double bracket do not overwrite the destination's existing contents.

Append

  • >> - standard output

  • << - standard input

  • 2>> - standard error

Examples:


Redirect output (stdout) to file1. Current contents of file are overwritten.

ls  > file1

Redirect output (stdout) to file1. Append to existing contents

ls  >> file1

Redirect error (stderr) to error.txt

Here I misspell cat. The resulting error gets saved to error.txt

kat file1  2> error.txt

In this example, we redirect standard output to output.txt and errors to error.txt

cat file1 > output.txt 2> error.txt

Here if file1 exists, it will be re-directed to output.txt. If it does not exist, then error will be directed to output.txt This may appear to be strange syntax, but remember that 2>&1 only tells the shell to redirect file descriptor 2 (standard error) to file descriptor 1 (standard output).

cat file1 > output.txt 2>&1	



show history command

The history command should produce a list of up to the last 500 commands used with line numbers.

> history 
...
496  service status network
497  systemctl status network
498  systemctl status httpd
499  systemctl status mysqld
500  systemctl start  mysqld

If you type ! plus a number, it will repeat the respective command

!499
systemctl status mysqld

Search History

Ctrl + R. This will output a search feature. Just begin typing a command and it will complete the command with the most recent match. If it is not the one you need, simply type a few more letters until you find the command you wanted. Once you find it, simply press the return key to run or press the right arrow key to edit it.


Add date and time to history command
> export HISTTIMEFORMAT='%F %T'

You can add this to the .bash_profile file to make it permanent: in /home/username/.bash_profile or /root/.bash_profile


4 different ways to repeat the last executed command.
  • Use the up arrow to view the previous command and press enter to execute it.
  • Type !! and press enter from the command line.
  • Type !-1 and press enter from the command line.
  • Press Control+P will display the previous command, press enter to execute it

Delete or Clear History of Commands
> history -c