Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 25

1.Write a shell script to display current date, time, username and directory.

!/bin/bash
echo "Current Date:"
date
echo "Username:"
whoami
echo "Current direcotry:"
pwd

Output:

Current Date:
Wed Dec 27 17:53:27 UTC 2017
Username:
root
Current direcotry:
/home

2. Write script to determine whether given file exist or not, file name is supplied as
Command line argument, also check for sufficient number of command line argument

#!/bin/bash

if [ -d /usr/games ];

then

echo "The Directory Exists"

else

echo "The Directory is not present"

fi
echo

c=$( wc -c < test.txt)

echo "Number of characters in test.txt is $c"

echo

w=$( wc -w < test.txt)

echo "Number of words in test.txt is $w"

echo

l=$( ec -l < test.txt)

echo "Number of lines in test.txt is $l"

3. Write a shell script that uses special variables related to a command line

$0

The filename of the current script.

$n
These variables correspond to the arguments with which a script was invoked. Here n is
a positive decimal

number corresponding to the position of an argument (the first argument is $1, the
second argument is $2,

and so on).

$#

The number of arguments supplied to a script.

$*

All the arguments are double quoted. If a script receives two arguments, $* is equivalent
to $1 $2.

$@

All the arguments are individually double quoted. If a script receives two arguments, $@
is equivalent to

$1 $2.

$?

The exit status of the last command executed.

$$

The process number of the current shell. For shell scripts, this is the process ID under
which they are

executing.

$!
The process number of the last background command

Command-Line Arguments

#!/bin/sh

echo "File Name: $0"

echo "First Parameter : $1"

echo "Second Parameter : $2"

echo "Quoted Values: $@"

echo "Quoted Values: $*"

echo "Total Number of Parameters : $#"

Here is a sample run for the above script −

$./test.sh Zara Ali

File Name : ./test.sh

First Parameter : Zara

Second Parameter : Ali

Quoted Values: Zara Ali

Quoted Values: Zara Ali

Total Number of Parameters : 2

4. Write a shell script to access the array values

#! /bin/bash

# To declare static Array

arr=(prakhar ankit 1 rishabh manish abhinav)


# To print all elements of array

echo ${arr[@]}

echo ${arr[*]}

echo ${arr[@]:0}
echo ${arr[*]:0}

output :
prakhar ankit 1 rishabh manish abhinav
prakhar ankit 1 rishabh manish abhinav
prakhar ankit 1 rishabh manish abhinav
prakhar ankit 1 rishabh manish abhinav

5. Write shell script to show various system configuration like :

a) Currently logged user name and his long name


b) Current shell
c) Your home directory
# function to display a line of asterices

function line(){

echo -e "User name: $USER (Login name: $LOGNAME)" >> /tmp/info.tmp.01.$$


echo -e "Current Shell: $SHELL" >> /tmp/info.tmp.01.$$$

echo -e "Home Directory: $HOME" >> /tmp/info.tmp.01.$$$

echo -e "Your O/s Type: $OSTYPE" >> /tmp/info.tmp.01.$$$

echo -e "PATH: $PATH" >> /tmp/info.tmp.01.$$$

echo -e "Current directory: `pwd`" >> /tmp/info.tmp.01.$$$


echo -e "Currently Logged: $nouser user(s)" >> /tmp/info.tmp.01.$$$

6. Write shell script to show various system configuration like:


a) Your operating system type
b) Your current path setting
c) Your current working directory
d) Show all available shells
nouser=`who | wc -l`

echo -e "User name: $USER (Login name: $LOGNAME)" >> /tmp/info.tmp.01.$$$

echo -e "Current Shell: $SHELL" >> /tmp/info.tmp.01.$$$

echo -e "Home Directory: $HOME" >> /tmp/info.tmp.01.$$$

echo -e "Your O/s Type: $OSTYPE" >> /tmp/info.tmp.01.$$$

echo -e "PATH: $PATH" >> /tmp/info.tmp.01.$$$

echo -e "Current directory: `pwd`" >> /tmp/info.tmp.01.$$$

echo -e "Currently Logged: $nouser user(s)" >> /tmp/info.tmp.01.$$$

if [ -f /etc/redhat-release ]

then

echo -e "OS: `cat /etc/redhat-release`" >> /tmp/info.tmp.01.$$$

fi

if [ -f /etc/shells ]
then

echo -e "Available Shells: " >> /tmp/info.tmp.01.$$$

echo -e "`cat /etc/shells`" >> /tmp/info.tmp.01.$$$

fi

if [ -f /etc/sysconfig/mouse ]

then

echo -e "--------------------------------------------------------------------" >> /tmp/info.tmp.01.$$$

echo -e "Computer Mouse Information: " >> /tmp/info.tmp.01.$$$

echo -e "--------------------------------------------------------------------" >> /tmp/info.tmp.01.$$$

echo -e "`cat /etc/sysconfig/mouse`" >> /tmp/info.tmp.01.$$$

fi

echo -e "--------------------------------------------------------------------" >> /tmp/info.tmp.01.$$$

echo -e "Computer CPU Information:" >> /tmp/info.tmp.01.$$$

echo -e "--------------------------------------------------------------------" >> /tmp/info.tmp.01.$$$

cat /proc/cpuinfo >> /tmp/info.tmp.01.$$$

echo -e "--------------------------------------------------------------------" >> /tmp/info.tmp.01.$$$

echo -e "Computer Memory Information:" >> /tmp/info.tmp.01.$$$

echo -e "--------------------------------------------------------------------" >> /tmp/info.tmp.01.$$$

cat /proc/meminfo >> /tmp/info.tmp.01.$$$

if [ -d /proc/ide/hda ]

then

echo -e "--------------------------------------------------------------------" >> /tmp/info.tmp.01.$$$

echo -e "Hard disk information:" >> /tmp/info.tmp.01.$$$


echo -e "--------------------------------------------------------------------" >> /tmp/info.tmp.01.$$$

echo -e "Model: `cat /proc/ide/hda/model` " >> /tmp/info.tmp.01.$$$

echo -e "Driver: `cat /proc/ide/hda/driver` " >> /tmp/info.tmp.01.$$$

echo -e "Cache size: `cat /proc/ide/hda/cache` " >> /tmp/info.tmp.01.$$$

fi

echo -e "--------------------------------------------------------------------" >> /tmp/info.tmp.01.$$$

echo -e "File System (Mount):" >> /tmp/info.tmp.01.$$$

echo -e "--------------------------------------------------------------------" >> /tmp/info.tmp.01.$$$

cat /proc/mounts >> /tmp/info.tmp.01.$$$

7. Write a Shell script to accept any two file names and check their file pessiormins

if [ $# -ne 2 ]

then

echo "No arguments"

elif [ ! -e $1 -o ! -e $2 ]

then

echo "FIle does not exist"

else

p1=`ls -l $1|cut -c2-10`

p2=`ls -l $2|cut -c2-10`


if [ $p1 == $p2 ]

then

echo "File permissions are equal & is $p1"

else

echo "File permissions are not equal"

echo "1st file $p1"

echo "2nd file $p2"

fi

fi

8. Write a Shell script to read a file name and change the existing file permissions.

echo -n "Enter file name : "

read file

# find out if file has write permission or not

[ -w $file ] && W="Write = yes" || W="Write = No"

# find out if file has excute permission or not

[ -x $file ] && X="Execute = yes" || X="Execute = No"

# find out if file has read permission or not

[ -r $file ] && R="Read = yes" || R="Read = No"

echo "$file permissions"

echo "$W"
echo "$R"

echo "$X"

9. Write a shell script to read a file name and check if it is a directory or block special file
or
character special file

#! /bin/bash

echo -e "Enter file name: \c"

read file_name

if [ -c $file_name ]

then

echo Character special file $file_name found

else

echo Character special file $file_name not found

Input:

test.txt

Output:
Character special file test.txt not found

Second-

#! /bin/bash

echo -e "Enter file name: \c"

read file_name

if [ -b $file_name ]

then

echo Block special file $file_name found

else

echo Block special file $file_name not found

Input:

img.jpg

Output:

Block special file img.jpg not found

10. Write a shell script to print current month calendar and to replace the current day
number
by ‘*’or ‘**’ respectively.
#!/bin/bash
echo "hello" `whoami`
echo `hostname`
echo `pwd`
echo "Here is the calender for this month"
cal

syntax;

cal [ [ month ] year

11. Write a shell program to illustrate command substitution.

#!/bin/bash

var=1234

echo -e "The value of var is $var \n"

echo -E "The value of var is $var \n"

echo "The value of var is $var \n"

echo -n "The value of var is $var \n"

Output:

k@laptop:~/SHELL$ ./s.sh
The value of var is 1234

The value of var is 1234 \n

The value of var is 1234 \n

The value of var is 1234 \nk@laptop:~/SHELL$

-e option enables interpretation of backslash escapes.

-E option to disable interpretation of backslash escapes (default).

-n option to disable insertion of new line.

Command substitution

When shell encounters a command, it substitutes the command with output from the
command given as:

`command`

Sample code:

#!/bin/sh

date=`date`

echo "date : $date"

users=`who | wc -l`

echo "Users : $user"

up=`date ; uptime`

echo "Uptime is $up"


Output:

date : Sun Nov 23 14:05:32 PST 2014

Users :

Uptime is Sun Nov 23 14:05:32 PST 2014

14:05:32 up 1 day, 17:02, 4 users, load average: 0.52, 0.44, 0.30

12. Write a shell script to print all Arguments with script name and total number of
arguments passed

Create a shell script as follows:


#!/bin/bash
# Purpose: Demo bash function
# -----------------------------
 ## Define a function called test()
test(){
  echo "Function name:  ${FUNCNAME}"
  echo "The number of positional parameter : $#"
  echo "All parameters or arguments passed to the function: '$@'"
  echo
}

## Call or invoke the function ##


## Pass the parameters or arguments  ##

test linuxtechtips
test 1 2 3 4 5
test "this" "is" "a" "test"

Run it as follows:
$ chmod +x script.name.here
$ ./script.name.here

13 .Write a shell script to access command line arguments by shifting position

#!/bin/bash

# total number of command-line arguments

echo "Total arguments passed are: $#"

# $* is used to show the command line arguments

echo "The arguments are: $*"

echo "The First Argument is: $1"

shift 2

echo "The First Argument After Shift 2 is: $1"

shift

echo "The First Argument After Shift is: $1"


14. Write a shell script to read two numbers and perform arithmetic operations

Addition

We use the + symbol to perform addition.

In the following example we are taking two integer value from the user and showing the
result after addition.

#!/bin/sh

# take two integers from the user

echo "Enter two integers: "

read a b

# perform addition

result=`expr $a + $b`

# show result

echo "Result: $result"


In the above script `expr $a + $b` means we are adding values stored in variable a and
b and evaluating the expression using the expr command. We are then saving the result
of the addition operation in the variable result.

Output:

$ sh add.sh

Enter two integers:

10 20

Result: 30

In the following example we are enclosing the variables in double quotes and using bc
to handle floating point numbers.

#!/bin/sh

# take two numbers from the user

echo "Enter two numbers: "

read a b

# perform addition

result=`expr "$a + $b" | bc`

# show result

echo "Result: $result"

Output:

$ sh add2.sh
Enter two numbers:

1.2 3.4

Result: 4.6

Subtraction

To perform subtraction we use the - symbol.

In the following example we will take two numbers from the user and print the
subtraction result.

#!/bin/sh

# take two numbers from user

echo "Enter two numbers: "

read a b

# compute subtraction result

result=`expr "$a - $b" | bc`

# print output

echo "Result: $result"

Output:

$ sh subtract.sh

Enter two numbers:

10 9

Result: 1
$ sh subtract.sh

Enter two numbers:

9 10

Result: -1

$ sh subtract.sh

Enter two numbers:

10.5 9.1

Result: 1.4

Multiplication

To perform multiplication we use the * symbol.

In the following example we will multiply two numbers.

#!/bin/sh

# take two numbers from user

echo "Enter two numbers: "

read a b

# compute multiplication result

result=`expr "$a * $b" | bc`

# print output

echo "Result: $result"

Output:
$ sh multiplication.sh

Enter two numbers:

23

Result: 6

$ sh multiplication.sh

Enter two numbers:

-2 3

Result: -6

$ sh multiplication.sh

Enter two numbers:

1.5 4

Result: 6.0

Division

To perform division we use the / symbol.

In the following example we will divide two numbers.

#!/bin/sh

# take two numbers from user

echo "Enter two numbers: "

read a b
# compute division result

result=`expr "$a / $b" | bc -l`

# print output

echo "Result: $result"

Output:

$ sh division.sh

Enter two numbers:

42

Result: 2

$ sh division.sh

Enter two numbers:

52

Result: 2.50000000000000000000

The -l option loads the standard math library with default scale set to 20.

Modulus

To perform modulus operations we use the % symbol.

In the following example we will get the remainder by dividing two numbers.

#!/bin/sh

# take two numbers from user


echo "Enter two numbers: "

read a b

# compute modulus result

result=`expr "$a % $b" | bc`

# print output

echo "Result: $result"

Output:

$ sh modulus.sh

Enter two numbers:

52

Result: 1

$ sh modulus.sh

Enter two numbers:

5.1 2

Result: 1.1

#!/bin/sh

# take two numbers from the user

echo "Enter two numbers: "

read a b

# check
if [ $a -eq $b ]

then

echo "Numbers are equal."

else

echo "Not equals."

Fi

15.Write a shell script to read two numbers and check their relation using relational
operators

#!/bin/sh

# take two integers from the user

echo "Enter two integers: "

read a b

# perform addition

result=`expr $a + $b`

# show result

echo "Result: $result"

In the above script `expr $a + $b` means we are adding values stored in variable a and
b and evaluating the expression using the expr command. We are then saving the result
of the addition operation in the variable result.

Output:

$ sh add.sh

Enter two integers:


10 20

Result: 30

16. Write a shell script to read two numbers and apply Boolean operators( logical
AND,OR and

#!/bin/sh

a=10

b=20

if [ $a != $b ]

then

echo "$a != $b : a is not equal to b"

else

echo "$a != $b: a is equal to b"

fi

if [ $a -lt 100 -a $b -gt 15 ]

then

echo "$a -lt 100 -a $b -gt 15 : returns true"

else

echo "$a -lt 100 -a $b -gt 15 : returns false"

fi

if [ $a -lt 100 -o $b -gt 100 ]

then

echo "$a -lt 100 -o $b -gt 100 : returns true"


else

echo "$a -lt 100 -o $b -gt 100 : returns false"

fi

if [ $a -lt 5 -o $b -gt 100 ]

then

echo "$a -lt 100 -o $b -gt 100 : returns true"

else

echo "$a -lt 100 -o $b -gt 100 : returns false"

fi

The above script will generate the following result −

10 != 20 : a is not equal to b

10 -lt 100 -a 20 -gt 15 : returns true

10 -lt 100 -o 20 -gt 100 : returns true

10 -lt 5 -o 20 -gt 100 : returns false

! This is logical negation. This inverts a true condition into


false and vice versa. [ ! false ] is true.

-o This is logical OR. If one of the operands is true, then the


condition becomes true. [ $a -lt 20 -o $b -gt 100 ] is true.

-a This is logical AND. If both the operands are true, then the
condition becomes true otherwise false. [ $a -lt 20 -a $b -gt 100 ] is false.

You might also like