Home  |   Guest Book |    Technical Page |    Personal Page  |    <--Back   

Welcome to Technical World
 
About Me
Who is a Good QA
Latest News
Testing Concepts
Automation Tools
Agile Development
UNIX Basics
Shell Scripting
Perl Scripting
Python Scripting
MySQL
Technology
Repository
Imp. Commands
My Resume

  

A special class of variables is environment variables

      – Have special meaning for the shell

      – Define the working environment

       – Can be modified at the command line level or through the startup files

        – Used by some programs to customize themselves to the user

 A number of utilities (such as mail and cron) may need to use the editor; the environment variable EDITOR

           allows you to customize the utilities so that you always work with your favorite editor

           · In the absence of environment variable, you may have to specify the command as

            mail -e vi sanjiv@aryabhat.umsl.edu

 An example is the environment variable DISPLAY which is used by most of the X programs

·         In the absence of environment variable, you may have to specify the command as

         sxclock -g 100x100 -display 192.0.0.10:0.0

 Other environment variables are shown in Table 2

 

Basic Coniditions are comparison in shell scripting .  

 

String Comparison

Description

Str1 = Str2

Returns true if the strings are equal

Str1 != Str2

Returns true if the strings are not equal

-n Str1

Returns true if the string is not null

-z Str1

Returns true if the string is null

Numeric Comparison

Description

expr1 -eq expr2

Returns true if the expressions are equal

expr1 -ne expr2

Returns true if the expressions are not equal

expr1 -gt expr2

Returns true if expr1 is greater than expr2

expr1 -ge expr2

Returns true if expr1 is greater than or equal to expr2

expr1 -lt expr2

Returns true if expr1 is less than expr2

expr1 -le expr2

Returns true if expr1 is less than or equal to expr2

! expr1

Negates the result of the expression

File Comditionals

Description

-d file

True if the file is a directory

-e file

True if the file exists (note that this is not particularly portable, thus -f is generally used)

-f file

True if the provided string is a file

-g file

True if the group id is set on a file

-r file

True if the file is readable

-s file

True if the file has a non-zero size

-u

True if the user id is set on a file

-w

True if the file is writable

-x

True if the file is an executable

 

   Table 1: Other metacharacters

> 

redirect stdout to a file

>> 

append stdout to a file

< 

take stdin from a file

| 

pipeline

<<

str stdin follows, upto next str on a line by itself

* 

match any string of zero or more characters

?

match any single character in the filenames

[ccc] 

match any single character from ccc in the filenames

 

ranges such as [0-9] and [a-z] are legal

; 

terminate command

& 

terminate command and run it in the background

‘...‘ 

run command(s) in ...

(...) 

run command(s) in ... as a subshell

{...} 

run command(s) in ... in current shell

$1, $2 

$0 ... $9 replaced by arguments to shell file

$var 

value of shell variable var

${var} 

value of shell variable to be concatenated with text

\c 

take character c literally (suppress newline in echo)

’...’ 

take ... literally

"..." 

take ... literally, but after interpreting $, ‘...‘, and \

# 

beginning of comment (ends with the end of line)

var=value 

assignment (no spaces around operator)

p1 && p2 

run p1; if successful, run p2

p1 || p2 

run p1; if unsuccessful, run p2

 

Table 2: Common environment variables
EDITOR  Editor to be used by various programs
DISPLAY  Display server identification
ENV  Name of initialization file (ksh only)
EXINIT  Set up options for vi
HOME  Absolute pathname of home/default directory
MAIL  system mail folder to keep incoming mail
PAGER  Application for per page display
PATH  list of directories to search for commands
PWD  Absolute pathname of current directory
SHELL  Name of shell environment
TERM  Terminal type
TZ  Time zone
USER  User name
VISUAL  Visual editor for various programs

 

Table 3: Shell built-in variables

$# 

number of arguments

$* 

all arguments to shellscript

$@ 

similar to $*

$0

Name of the script being executed

$- 

options supplied to the shell

$? 

return value of the last command executed

$$ 

pid of the shell

$! 

pid of the last command started with &

$IFS 

list of characters that separate words in arguments

$PS1 

prompt string, default value ’$ ’

$PS2 

prompt string for continued command line, default value ’> ’

EDITOR 

Name of your favorite editor

 

 

Table 4: Shell I/O redirection

> 

file direct stdout to file

>> 

file append stdout to file

< 

file take stdin from file

p1 | p2 

connect stdout of p1 to stdin of p2

n> file

direct output from file descriptor n to file

n>> file

append output from file descriptor n to file

n>&m 

merge output from file descriptor n with file descriptor m

n<&m 

merge input from file descriptor n with file descriptorm

<<s 

here document; take stdin until next s at beginning of a line;substitute for $, ‘...‘, and \

<<\s s

here document with no substitution

<<’ss’

here document with no substitution

 

 

cut  
reads std-in, extracts selected fields, writes std-out
cut -c1,4,7  characters 1, 4, and 7
cut -c1-3,8  characters 1 thru 3, and 8
cut -c-5,10  characters 1 thru 5, and 10
cut -c3-  characters 3 thru last
cut -f1,4,7  tab-separated fields 1, 4, and 7
cut -d" " -f1  blank-separated field 1
   
echo a.b.c | cut -d"." -f2 yields b

 

 

ss

sCopyright 2009 Kunal Saxena Inc. All rights reserved