Professional Documents
Culture Documents
Linux Lab Final Manual
Linux Lab Final Manual
!/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
else
fi
echo
echo
echo
3. Write a shell script that uses special variables related to a command line
$0
$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).
$#
$*
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 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
#! /bin/bash
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
function line(){
if [ -f /etc/redhat-release ]
then
fi
if [ -f /etc/shells ]
then
fi
if [ -f /etc/sysconfig/mouse ]
then
fi
if [ -d /proc/ide/hda ]
then
fi
7. Write a Shell script to accept any two file names and check their file pessiormins
if [ $# -ne 2 ]
then
elif [ ! -e $1 -o ! -e $2 ]
then
else
then
else
fi
fi
8. Write a Shell script to read a file name and change the existing file permissions.
read file
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
read file_name
if [ -c $file_name ]
then
else
Input:
test.txt
Output:
Character special file test.txt not found
Second-
#! /bin/bash
read file_name
if [ -b $file_name ]
then
else
Input:
img.jpg
Output:
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;
#!/bin/bash
var=1234
Output:
k@laptop:~/SHELL$ ./s.sh
The value of var is 1234
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`
users=`who | wc -l`
up=`date ; uptime`
Users :
12. Write a shell script to print all Arguments with script name and total number of
arguments passed
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
#!/bin/bash
shift 2
shift
Addition
In the following example we are taking two integer value from the user and showing the
result after addition.
#!/bin/sh
read a b
# perform addition
result=`expr $a + $b`
# show result
Output:
$ sh add.sh
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
read a b
# perform addition
# show result
Output:
$ sh add2.sh
Enter two numbers:
1.2 3.4
Result: 4.6
Subtraction
In the following example we will take two numbers from the user and print the
subtraction result.
#!/bin/sh
read a b
# print output
Output:
$ sh subtract.sh
10 9
Result: 1
$ sh subtract.sh
9 10
Result: -1
$ sh subtract.sh
10.5 9.1
Result: 1.4
Multiplication
#!/bin/sh
read a b
# print output
Output:
$ sh multiplication.sh
23
Result: 6
$ sh multiplication.sh
-2 3
Result: -6
$ sh multiplication.sh
1.5 4
Result: 6.0
Division
#!/bin/sh
read a b
# compute division result
# print output
Output:
$ sh division.sh
42
Result: 2
$ sh division.sh
52
Result: 2.50000000000000000000
The -l option loads the standard math library with default scale set to 20.
Modulus
In the following example we will get the remainder by dividing two numbers.
#!/bin/sh
read a b
# print output
Output:
$ sh modulus.sh
52
Result: 1
$ sh modulus.sh
5.1 2
Result: 1.1
#!/bin/sh
read a b
# check
if [ $a -eq $b ]
then
else
Fi
15.Write a shell script to read two numbers and check their relation using relational
operators
#!/bin/sh
read a b
# perform addition
result=`expr $a + $b`
# show 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
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
else
fi
then
else
fi
then
fi
then
else
fi
10 != 20 : a is not equal to b
-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.