Five Little Tricks To Achieve The Best Results In UNIX script

The following 5 tips really helpful in writing awesome scripts in UNIX. For serious learners of UNIX scripts, below are the right points to begin with.

Tip#1- Comments in Script: Lines not preceded by a hash ‘#’ character are taken to be Unix commands and are executed. Any Unix command can be used. For example, the script below displays the current directory name using the pwd command, and then lists the directory contents using the ls command.

#!/bin/sh 
# purpose: print out current directory name and contents 
pwd
ls

Tip#2- Writing variables: The script below shows how to assign and use a variable. In general, shell variables are all treated as strings (i.e. bits of text). Shells are extremely fussy about getting the syntax exactly right; in the assignment there must be no space between the variable name and the equals sign, or the equals sign and the value. To use the variable, it is prefixed by a dollar ‘$’ character.

#!/bin/sh # name is a variable 
name="fred"
echo "The name is $name"

> How to write script in UNIX effectively: A most popular course learn at your most free time.

Tip#3- Writing conditions: Conditionals are used where an action is appropriate only under certain circumstances. The most frequently used conditional operator is the if-statement. For example, the shell below displays the contents of a file on the screen using cat, but lists the contents of a directory using ls.

#!/bin/sh
# show script
if [ -d $1 ]
then
  ls $1
else
  cat $1
fi

Tip#4- Looping commands: The simplest looping command is the while command. An example is given below:

#!/bin/sh
# Start at month 1
month=1
while [ $month -le 12 ]
do
# Print out the month number
  echo "Month no. $month"
# Add one to the month number
  month=`expr $month + 1`
done

Tip#5- Sample Script: One of the best example of sample script is as follows.

#!/bin/sh
# DOS interpreter. Impersonates DOS as follows:
# DOS command   UNIX equivalent   Action
# cd            cd                Change directory
# dir           ls                List directory contents
# type          cat               List file contents
# del           rm                Delete a file
# ren           mv                Rename a file
# copy          cp                Copy a file

echo "Welcome to the DOS interpreter"
echo "Type Ctrl-C to exit"

# Infinite loop
while [ "forever" ]
do
# Show DOS prompt; c stops a new line from being issued
  echo "DOS> c"  
# Read in user's command
  read command arg1 arg2
# Do a UNIX command corresponding to the DOS command
  case $command in
    cd)
      cd $arg1
      ;;
    dir)
      ls
      ;;
    type)
      cat $arg1
      ;;
    del)
      rm $arg1
      ;;
    ren)
      mv $arg1 $arg2
      ;;
    copy)
      cp $arg1 $arg2
      ;;
    *)
      echo "DOS does not recognise the command $command"
      ;;
  esac   
done

You can read online some additional examples here.


Also Read

  1. The popular Five Big data types learn today video
  2. You need story Blue Prism Tutorial RPA for beginners
  3. Best Quote from Tony Robins
Advertisements

Author: Srini

Experienced software developer. Skills in Development, Coding, Testing and Debugging. Good Data analytic skills (Data Warehousing and BI). Also skills in Mainframe.