Somethings about Shell Scripts

its a set of commands written under one name

steps to write a shell script

open vi editor

write the code

save the code with name run the file

Sample code for shell scripts

cat > myshell

ls -l

who

cal

ctrl +d

write the above  lines and save it and run as

sh myshell

Steps to create a executeable file in solaris

1.             given full permission(rwx) to a given file using chmod command

eg chmod 700 filename

2.             then run it by using the following command ./filename

declareation of variables

name=jatinder                                          Correct

name=”jatinder”                                      Correct

name=jatinder singh                                Wrong

name=”jatinder singh”                             Correct

Code 1

name=”Jatinder”

echo “Name is : ”

echo $name

age=24

echo “Age is : ”

echo $age

Code 2

cat > code2

echo “Enter Your Details”

echo “Enter Your Name: ”

read name

echo “Enter Your Age: ”

read age

echo ”    ”

echo “Your Details are : ”

echo “Name is : ${name}”

echo “Age is : ${age}”

echo ”   ”

echo “Thanks for detail”

if you want to export data to a child shell command is

export variablename

Environmental Variables

echo $HOME

echo $LOGNAME

echo $SHELL

echo $PATH

echo $PS1

Things we can use in if condition

->Options used to text file status

test           -f             filename   True if file exists and is an ordinary file

test           -d             filename   True if file exists and is a directory

test           -r             filename   True if file exists and is readable

test           -w            filename   True if file exists and is writeable

test           -x             filename   True if file exists and is executable

test           -s             filename   True if file exists and is not empty

-> Arithmetic test options

-eq           Equal to

-ne           Not Equal to

-gt            Greater than

-ge           Greater than or equal to

-lt             Less than

-le            Less than or equal to

-> String test Operators/Options

here S1 and S2 are some string values

S1 = S2                   True if strings S1 and S2 are identical

S1 != S2  True if strings are not identical

-z S1                        True if S1 contains no characters

e.g: test ${name} = “jatinder”

 

Codes

logincheck

echo “Enter Login Details”

echo “Login ID: ”

read userid

echo “Password: ”

read password

if test ${userid} = “admin” -a ${password} = “password”

then

echo “Welcome to our Site”

else

echo “ERROR incorrect USER ID and PASSWORD”

fi

CASE STATEMENT

Case code 1

echo “Enter dozen or score…. ”

echo “Enter your choice: ”

read val

case ${val} in

dozen) echo “12” ;;

score) echo “20” ;;

esac

Case Code 2

echo “Enter the day number of a weak:”

read weakday

case ${weakday} in

1) echo “MONDAY”;;

2) echo “TUESDAY”;;

3) echo “WEDNESDAY”;;

4) echo “THURSDAY”;;

5) echo “FRIDAY”;;

6) echo “SATURDAY”;;

7) echo “SUNDAY”;;

8) echo “ERROR”;;

esac

FOR Statement

CODE 1

for name in jatinder satwinder kamal

do

echo “${name}”

done

CODE 2

for filename in code1 forcode logincheck

do

echo “DISPLAYING DATA OF THE FILE \n”

cat ${filename}

done

CODE 3

(printing data of files passed as a parameter from command line)

for filename in $1 $2 $3

do

echo “DISPLAYING DATA OF THE FILE \n”

cat ${filename}

done

expr Command

a=10

b=5

echo “Sum of 2 numbers is : ”

expr $a + $b

echo “First Number Minus Second Number is : ”

expr $a – $b

echo “Multiply result is : ”

expr $a \* $b

echo “Division Result is : `expr $a / $b`”