Shell Basics

script basics

  • — shabang (#!)
  • — every shell script normally starts with (#!/bin/bash)
  • — occasionally we can notice shell script starting with (#!/bin/sh) . but if u see /bin/sh will be linked to /bin/bash

root@server.com [~]#  ls -l /bin/sh
lrwxrwxrwx 1 root root 4 Jul 22 2011 /bin/sh -> bash*

  • — no Linux distribution are using SH shell anymore
  • — any other line having # is called comment and ignored by shell

2 ways to execute a shell script

  • — bash <file name>
  • — ./<file name>      #make sure you have executable permissions for file

Example :
image

Variables

  • — variables are things that hold data or information

1)  Global variables/Environment variables (variables are available in all shells. The env or printenv commands can be used to display environment variables)

2) local variable ( Local variables are only available in the current shell. Using the set built-in command without any options will display a list of all variables (including environment variables) and functions.)

  • —  special variables $#,$0,$!,$?,$@,$-,$$,$_,.
  • —  Positional variables $1,$2,….
  • — $# { represents the total number of arguments passed to shell script }
  • — $0 { represents it’s own shell script name }
  • — $1,$2,$3,….{ represents the first,second,third and soon…. argument passed to shell script}
  • — $<varname> {represents the value declared for variable locally in a script }

Example :
image

Quotes

  • — double quotes ( ” ) used in variable declaration in strings . it tells to get the value of a variable in echo statements
  • — single quotes ( ‘ ) used to allow special characters to be a part of string . it tells not to get the value of a variable in echo statements
  • — special characters ( #,$,&,*,?,[ ],’,< >,`, )
  • — ( ` ) back tick is used evaluate the command and put the value in variable
  • — ( ) back slash is also used to allow special characters as a part of string like ( ‘ ) .
  • note : do not confuse with back slash ()and back tick (`).

Example :
image

Comparison Operators

Four comparison operators for strings
— equal (=)
— not equal (!=)
— non zero length (-n)
— zero length (-z)

Six comparison operators for numbers

  • — equal (-eq)
  • — not equal (-ne)
  • — greater than (-gt)
  • — less than (-lt)
  • — less than or equal to (-le)
  • — greater than or equal to (-ge)

Six comparison operators for files

  • — Is the file a directory (-d)
  • — Is the file a regular file (-f)
  • — Is read permission is set (-r)
  • — Is write permission is set (-w)
  • — Is execute permission is set (x)
  • — Is that a non empty file (-s)

Redirection and Pipes 

  •  >
    • Example :stores a single line in the file /tmp/mymessage, creating the file if necessary.
      $ echo “This is a test message.” > /tmp/mymessage
  • >>
  • <
  • >&
  • 2>
  • |
  • &&
  • ||
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s