Basic Linux Shell Scripts

 

commands

 I. Basic Understanding about Linux Shell Scripts

1. Basic Understand about Linux Shell Scripts

  • On Linux, the best way to control Linux is from command interface, it can support you with more options than GUI interface.
  • So if you want to learn Linux, you should learn command-line.
  • Commands on Linux based on the BASH (Bourne Agian Shell).
  • By default, in each user’s home directory, we will have some files by default such as .bash_history, .bash_logout, .bash_profile, and .bashrc. To view these hidden files we have to use “ls -a” command.
    • .bash_history : all history for your bash commands.
[root@localhost ~]# cat .bash_history | more
wget http://www.rootkit.nl/files/lynis-1.3.0.tar.gz
ls
tar -xzvf lynis-1.3.0.tar.gz
cd lynis-1.3.0
ls
./lynis –check-update
ls
./lynis
./lynis –check-all
cd /
    • .bash_logout : sets command when we exit a shell. By default, we can set “clear” command to clear all previous command on terminal.
[root@localhost ~]# cat .bash_logout
# ~/.bash_logout
    • .bash_profile : Calls .bashrc file for more configuration data. Add ~/bin directory to your PATH.
[root@localhost ~]# cat .bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programsPATH=$PATH:$HOME/binexport PATH
[root@localhost ~]#
    • .bashrc : Calls the /etc/bashrc file for basic configuration data. For root user, this file adds aliases for rm, mv and cp commands to help prevent accident deletion of a file.
[root@localhost ~]# cat .bashrc
# .bashrc
# User specific aliases and functions
alias rm=’rm -i’
alias cp=’cp -i’
alias mv=’mv -i’# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
[root@localhost ~]#

2. Redirecting Input and Output

  •  Standard input comes from a keyboard normally, but when you have a full of data, you don’t need to type everything agian – you can simply redirect that file with <.
    • Example: # database_program < database_data
  • If you want to save startdard output to a file. You can using this >.
    • Example: ls -lah / > folderlist
  • If you want to append data to folderlist, you have to use >>.
    • Example: ls -lah /var >> folderlist
  • We also can combine that
    • Example: database_program < database_data > database_output
  • Standard error output can help us to troubleshooting the problem
    • Example: database_program < database_data 2> error_log

3. Input and Output Pipes

  • The pipe (|) can combine the commands.
    • Example: ls -lah | more
  • The pipe takes the stardard ouput from ls -l command and sends the result as stardard input to more command. So we don’t need to create and delete any temporary files.

4. Running Bash Shell in Background

  • Linux is a multitasking system, you can run multiple programs from a single command.
  • 2 ways we can make programs run in background command. Example you wrote one shell script backup.sh and you want to run that in background.
    • Example: ./backup.sh &
  • The ampersand (&) sends program execution to the “background”.
  • Ctrl+Z command suspends a running program.
  • bg command to send the command to background.
  • You can return the program the foreground by fg command.

5. Tildes and Home Directory

  • Tilde (~) presents the home directory of current logged-on user.
  • We can use the tilde with most of bash shell. Example, users can go to home directory by cd ~ command or list all files in home directory by ls ~ command.

6. Dots

  • A single dot (.) represents the current directory.
  • A double dot (..) can help us to navigate the parent directory.
  • We also can use dot to run a bash script in current directory. Example: ./iptables status

7. Wildcards

  • The characters are the asterisk (*) and the question mark (?). The asterisk (*) presents any numbers or any letters. Each question mark represents one alphanumberic characters.
    • Example: ls b* . List all files with first character is b.
    • Example: ls b? . List all file with 2 characters and the first character is b.
  • We can define special characters with more detail with brackets [].
  • Example: ls n[1-6].txt List all files with n is the first character and second is from 1 –> 6.
  • Example: ls n[123456].txt . List all files with n is the first character and second is from 1 –> 6.

8. Slashes in the Shell.

  • We have 2 types: forward slashes (/) and backslashes (\).
  • A single forward slash (/) represents the root directory on Linux. Forward slash also help you to navigate to the subdirectories /var/log.
  • Backslash help you to escapes the meaning of the character in the shell scripts.
  • Example: ls /mnt/windows/My Documents –> will not work in Linux. We have to use ls /mnt/windows/My\ Documents

8. Quotes

  •  Single quotes (‘) : The shell does not process any variables or commands.
  •  Double quotes (“): The shell processes variables, such as $NAME, but does not process any commands.
  •  Back quotes (‘) : The shell tries to process every word in quotes as a command.
[root@localhost ~]# export $Name=Kevin
[root@localhost ~]# echo Welcome $Name, the date is date
Welcome Kevin, the date is date
[root@localhost ~]#
  • Example with single quotes:
[root@localhost ~]# echo ‘Welcome $Name, the date is date’
Welcome $Name, the date is date
  • Example with Double quotes:
[root@localhost ~]# echo “Welcome $Name, the date is date”
Welcome Kevin, the date is date
[root@localhost ~]#
  • Example with Back quotes:
[root@localhost ~]# echo “Welcome $Name, the date is `date`”
Welcome Kevin, the date is Mon Oct 7 21:16:29 PDT 2013
[root@localhost ~]#

9. Aliases

  • Aliases is one of the most useful shell variables.
  • alias command will show you all list of commands that we can use.
[root@localhost ~]# alias
alias cp=’cp -i’
alias l.=’ls -d .* –color=auto’
alias ll=’ls -l –color=auto’
alias ls=’ls –color=auto’
alias mv=’mv -i’
alias rm=’rm -i’
alias which=’alias | /usr/bin/which –tty-only –read-alias –show-dot –show-tilde’
[root@localhost ~]#

Example to configure one alias on Linux System

[root@localhost ~]# alias listroot=’ls -lah /’
[root@localhost ~]# listroot
total 135K
dr-xr-xr-x. 24 root root 4.0K Oct 7 21:08 .
dr-xr-xr-x. 24 root root 4.0K Oct 7 21:08 ..
-rw——-. 1 root root 8.0K Oct 7 01:24 aquota.group
-rw——-. 1 root root 7.0K Oct 7 01:24 aquota.user
-rw-r–r–. 1 root root 0 Oct 7 01:24 .autofsck
drwxr-xr-x. 2 root root 4.0K Jan 6 2013 bashscripts
dr-xr-xr-x. 2 root root 4.0K Mar 25 2013 bin
dr-xr-xr-x. 5 root root 1.0K Mar 25 2013 boot
drwxr-xr-x. 19 root root 3.9K Oct 7 01:40 dev
drwxr-xr-x. 108 root root 12K Oct 7 21:10 etc
drwxr-xr-x. 5 root root 4.0K Sep 23 11:30 home
drwxr-xr-x. 23 root root 12K Jan 29 2013 kamailio
dr-xr-xr-x. 12 root root 4.0K Mar 25 2013 lib
dr-xr-xr-x. 9 root root 12K May 21 08:42 lib64
drwx——. 2 root root 16K Jan 4 2013 lost+found
drwxr-xr-x. 2 root root 4.0K Jan 5 2013 media
drwxr-xr-x. 4 root root 4.0K Jan 5 2013 mnt
drwxr-xr-x. 6 root root 4.0K Jan 15 2013 opt
dr-xr-xr-x. 172 root root 0 Oct 7 01:24 proc
dr-xr-x—. 31 root root 4.0K Oct 7 21:09 root
dr-xr-xr-x. 2 root root 12K May 21 08:42 sbin
drwxr-xr-x. 7 root root 0 Oct 7 01:24 selinux
drwxr-xr-x. 2 root root 4.0K Sep 23 2011 srv
drwxr-xr-x. 13 root root 0 Oct 7 01:24 sys
drwxr-xr-x. 11 root root 1.0K Oct 7 21:10 tmp
drwxr-xr-x. 13 root root 4.0K Jan 4 2013 usr
drwxr-xr-x. 22 root root 4.0K Jan 10 2013 var
[root@localhost ~]#

If you don’t want to use that alias

[root@localhost ~]# unalias listroot

10. Shell Conditionals

If we want to build a shell scripts, the most basic of these is the conditinal also known as an if conditional.

  • if [ x -eq y ] : If x=y?
  • if [ x -ne y ] : If x#y?
  • if [ x -gt y ] : If  x > y ?
  • if [ x -ge y ] : If x >= y ?
  • if [ x -lt y ] : If x < y ?
  • if [ x -le y ] : If x <= y ?
  • if [ -s /etc/file ] : if /etc/file have data ?
  • if [ -f /etc/file ] : if /etc/file is a regular file ?
  • if [ -d /etc/dir ] : if /etc/dir is directory ?
  • if [ x -ne y -a x -lt z] : if x# y and x < z ?

11. Write a sample shell script

  • Create shell script for calculator
[root@localhost ~]# vi sampleshell.sh
clear
sum=0
i=”y”echo ” Enter one no.”
read n1
echo “Enter second no.”
read n2
while [ $i = “y” ]
do
echo “1.Addition”
echo “2.Subtraction”
echo “3.Multiplication”
echo “4.Division”
echo “Enter your choice”
read ch
case $ch in
1)sum=`expr $n1 + $n2`
echo “Sum =”$sum;;
2)sum=`expr $n1 – $n2`
echo “Sub = “$sum;;
3)sum=`expr $n1 \* $n2`
echo “Mul = “$sum;;
4)sum=`expr $n1 / $n2`
echo “Div = “$sum;;
*)echo “Invalid choice”;;
esac
echo “Do u want to continue ?”
read i
if [ $i != “y” ]
then
exit
fi
done
  • Change permission to activate execute permission
[root@localhost ~]# chmod 766 sampleshell.sh
  • Run shell script
[root@localhost ~]# ./sampleshell.sh
Enter one no.
1
Enter second no.
1
1.Addition
2.Subtraction
3.Multiplication
4.Division
Enter your choice
1
Sum =2
Do u want to continue ?
n
[root@localhost ~]#

 

II. Some Linux / Unix Command Examples in Practice

1. Top 10 Basic Linux / Unix Commands

 

2. 30 best commands used by Linux / Unix Administrator

 

Thanks for using ITHelpblog.com.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Go to top