for study VI editer
http://cc.iiti.ac.in/vicom.pdf
http://www.tutorialspoint.com/unix/pdf/unix-vi-editor.pdf
Practical
List:
1. Study of Basic commands of Linux/UNIX.
2. Study of Advance commands and filters of Linux/UNIX.
3. Write a shell script to generate marksheet of a
student. Take 3 subjects, calculate and display total marks, percentage and
Class obtained by the student.
http://www.dailyfreecode.com/code/shell-script-generating-mark-sheet-1705.aspx
echo
"Enter the five subject marks for the student"
read m1 m2
m3 m4 m5
sum1=`expr
$m1 + $m2 + $m3 + $m4 + $m5`
echo
"Sum of 5 subjects are: " $sum1
per=`expr
$sum1 / 5`
echo
" Percentage: " $per
if [ $per
-ge 60 ]
then
echo
"You get Distinction”
elif [
$per -ge 50 ]
then
echo “You
get First class”
elif [
$per -ge 40 ]
then
echo
"You get Second class"
else
echo "You get Fail"
fi
OUTPUT
**********
[04mca58@LINTEL 04mca58]$ sh filemenu.sh
Enter the five subject marks for the
student
45
35
30
40
42
Sum of 5 subjects are: 192
Percentage : 76.8
You get
Distinction
4. Write a
shell script to find factorial of given number n.
http://www.dailyfreecode.com/code/calculate-factorial-number-1533.aspx
echo "Total no of factorial wants"
read fact
ans=1
counter=0
while [ $fact -ne $counter ]
do
counter=`expr $counter + 1`
ans=`expr $ans \* $counter`
done
echo "Total of factorial is $ans"
-------------------------------------------------------------------
output
-------------------------------------------------------------------
Total no of factorial wants
5
Total of factorial is 120
5. Write a shell script which will accept a number b and
display first n prime numbers as output.
http://www.bashguru.com/2009/11/shell-script-to-find-prime-number.html
#!/bin/bash
# SCRIPT: prime1.sh
# USAGE : ./prime1.sh
# PURPOSE: Finds whether given number is prime or not
#####################################################################
echo -n "Enter a number: "
read num
i=2
while [ $i -lt $num ]
do
if [ `expr $num % $i` -eq 0 ]
then
echo "$num is not a prime number"
echo "Since it is divisible by $i"
exit
fi
i=`expr $i + 1`
done
echo "$num is a prime number "
Output: [root@venu ]# ./prime1.sh Enter a number: 1879 1879 is a prime number [root@venu ]# ./prime1.sh Enter a number: 119 119 is not a prime number Since it is divisible by 7
6. Write a shell script which will generate first n
fibonnacci numbers like: 1, 1, 2, 3, 5, 13,…
http://www.bashguru.com/2010/12/shell-script-to-generate-fibonacci.html
if [ $# -eq 1 ]
then
Num=$1
else
echo -n "Enter a Number :"
read Num
fi
f1=0
f2=1
echo "The Fibonacci sequence for the number $Num is : "
for (( i=0;i<=Num;i++ ))
do
echo -n "$f1 "
fn=$((f1+f2))
f1=$f2
f2=$fn
done
echo
OUTPUT: # sh fibo_iterative.sh 18 The Fibonacci sequence for the number 18 is : 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 # sh fibo_iterative.sh Enter a Number :20 The Fibonacci sequence for the number 20 is : 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765
7. Write a menu driven shell script which will print the
following menu and execute the given task.
MENU
a.
.
Display calendar of current month
b.
.
Display today’s date and time
c.
.
Display usernames those are currently logged in the system
d.
.
Display your name at given x, y position
e.
.
Display your terminal number
f.
.
Exit
===================================================================
Echo “ MENU
a.
.
Display calendar of current month
b.
.
Display today’s date and time
c.
.
Display usernames those are currently logged in the system
d.
.
Display your name at given x, y position
e.
.
Display your terminal number
f.
.
Exit
“
Read
i
Case
“$i” in
1)
Cal
;;
2)
;;
3)
;;
4)
Tput
cup 10 10
Echo pradish ;;
5)
Pwd
;;
6)
Exit
;;
*) echo “enter valid in put” ;;
esac
8. Write a shell script to read n numbers as command
arguments and sort them in descending order.
echo $0 # script
file name
echo $1 # first
argument
echo $2 # second
argument
echo $3 # third
argument
9. Write a shell script to display all executable files,
directories and zero sized files from current directory.
find
$dir -size 0
DU ---- for dir
10. Write a shell script to check entered string is
palindrome or not.
clear
echo
"Ente
r a string
to be entered:"
read str
echo
len=`echo
$str | wc -c`
len=`expr
$len - 1`
i=1
j=`expr
$len / 2`
while test
$i -le $j
do
k=`echo
$str | cut -c $i`
l=`echo
$str | cut -c $len`
if test $k
!= $l
then
echo
"String is not palindrome"
exit
fi
i=`expr $i
+ 1`
len=`expr
$len - 1`
done
echo
"String is palindrome"
11. Shell programming using filters (including grep,
egrep, fgrep)
Grep is a very
useful tool in the Unix world. If you don't know it already, it is very much
like a search tool. It can search for a text or pattern in one or multiple
input files or data coming from (unix) pipes.
Most of the Linux
distributions out there offer three ways to use Grep from the command-line (aka terminal): grep, fgrep,
and egrep.
grep
The grep command
is the most common one but also the oldest. It offersBasic Regular Expression matching.
The particularity of BRE is that some metacharacters ({, }, and parenthesis) need to be escaped (using a backslash \) to be used properly. (see also egrepbelow)
The particularity of BRE is that some metacharacters ({, }, and parenthesis) need to be escaped (using a backslash \) to be used properly. (see also egrepbelow)
grep 'testi\{1,2\}ng'
file.txt
|
will match any lines
containing either testing or testiing in
file.txt.
fgrep
fgrep, short for fixed(-size
strings) grep, is used to do a search for a string with a fixed size (can
be seen as a constant), contrarely to patterns.
fgrep '{test}match*'
file.txt
|
will match any text
containing exactly {test}match*. No parsing or interpreting of
regular expression or whatever, is done by grep.
egrep
egrep, short for extended
grep, (contrarely to the simple grep) search for matches using a pattern
written using Extended Regular Expressions. The changes from BRE is
that you do not need to escape metacharacters, and also ERE provides three more
metacharacters: |, +, and ? (note that they can still be used in the basic grep
by escaping them).
egrep 'testi\{1,2\}ngs?'
file.txt
|
will match any text
containing any of: testing, testings, testiing, testiings.
10. Write a shell
script to check entered string is palindrome or not.
clear
echo "Enter
a string to be entered:"
read
str
echo
len=`echo
$str | wc -c`
len=`expr
$len - 1`
i=1
j=`expr
$len / 2`
while test
$i -le $j
do
k=`echo
$str | cut -c $i`
l=`echo
$str | cut -c $len`
if test
$k != $l
then
echo "String
is not palindrome"
exit
fi
i=`expr
$i + 1`
len=`expr
$len - 1`
done
echo "String
is palindrome"
12. Study of Unix Shell and Environment Variables.
Environment
Variables:
Following
is the partial list of important environment variables. These variables would
be set and accessed as mentioned above:
Variable
|
Description
|
DISPLAY
|
Contains
the identifier for the display that X11 programs should use by default.
|
HOME
|
Indicates
the home directory of the current user: the default argument for the cd
built-in command.
|
IFS
|
Indicates
the Internal Field Separator that is used by the parser for word splitting
after expansion.
|
LANG
|
LANG
expands to the default system locale; LC_ALL can be used to override this.
For example, if its value is pt_BR, then the language is set to (Brazilian)
Portuguese and the locale to Brazil.
|
LD_LIBRARY_PATH
|
On
many Unix systems with a dynamic linker, contains a colon-separated list of
directories that the dynamic linker should search for shared objects when
building a process image after exec, before searching in any other
directories.
|
PATH
|
Indicates
search path for commands. It is a colon-separated list of directories in
which the shell looks for commands.
|
PWD
|
Indicates
the current working directory as set by the cd command.
|
RANDOM
|
Generates
a random integer between 0 and 32,767 each time it is referenced.
|
SHLVL
|
Increments
by one each time an instance of bash is started. This variable is useful for
determining whether the built-in exit command ends the current session.
|
TERM
|
Refers
to the display type
|
TZ
|
Refers
to Time zone. It can take values like GMT, AST, etc.
|
UID
|
Expands
to the numeric user ID of the current user, initialized at shell startup.
|
13. Write a shell script to validate the entered date. (eg. Date format
is : dd-mm-yyyy)
#!/bin/bash
# Shell program to find the validity of a given date
dd=0
mm=0
yy=0
# store number of days in a month
days=0
# get day, month and year
echo -n "Enter day (dd) : "
read dd
echo -n "Enter month (mm) : "
read mm
echo -n "Enter year (yyyy) : "
read yy
# if month is negative (<0) or greater than 12
# then it is invalid month
if [ $mm -le 0 -o $mm -gt 12 ];
then
echo "$mm is invalid month."
exit 1
fi
# Find out number of days in given month
case $mm in
1) days=31;;
2) days=28 ;;
3) days=31 ;;
4) days=30 ;;
5) days=31 ;;
6) days=30 ;;
7) days=31 ;;
8) days=31 ;;
9) days=30 ;;
10) days=31 ;;
11) days=30 ;;
12) days=31 ;;
*) days=-1;;
esac
# find out if it is a leap year or not
if [ $mm -eq 2 ]; # if it is feb month then only check of leap year
then
if [ $((yy % 4)) -ne 0 ] ; then
: # not a leap year : means do nothing and use old value of days
elif [ $((yy % 400)) -eq 0 ] ; then
# yes, it's a leap year
days=29
elif [ $((yy % 100)) -eq 0 ] ; then
: # not a leap year do nothing and use old value of days
else
# it is a leap year
days=29
fi
fi
# if day is negative (<0) and if day is more than
# that months days then day is invaild
if [ $dd -le 0 -o $dd -gt $days ];
then
echo "$dd day is invalid"
exit 3
fi
# if no error that means date dd/mm/yyyy is valid one
echo "$dd/$mm/$yy is a vaild date"
# Shell program to find the validity of a given date
dd=0
mm=0
yy=0
# store number of days in a month
days=0
# get day, month and year
echo -n "Enter day (dd) : "
read dd
echo -n "Enter month (mm) : "
read mm
echo -n "Enter year (yyyy) : "
read yy
# if month is negative (<0) or greater than 12
# then it is invalid month
if [ $mm -le 0 -o $mm -gt 12 ];
then
echo "$mm is invalid month."
exit 1
fi
# Find out number of days in given month
case $mm in
1) days=31;;
2) days=28 ;;
3) days=31 ;;
4) days=30 ;;
5) days=31 ;;
6) days=30 ;;
7) days=31 ;;
8) days=31 ;;
9) days=30 ;;
10) days=31 ;;
11) days=30 ;;
12) days=31 ;;
*) days=-1;;
esac
# find out if it is a leap year or not
if [ $mm -eq 2 ]; # if it is feb month then only check of leap year
then
if [ $((yy % 4)) -ne 0 ] ; then
: # not a leap year : means do nothing and use old value of days
elif [ $((yy % 400)) -eq 0 ] ; then
# yes, it's a leap year
days=29
elif [ $((yy % 100)) -eq 0 ] ; then
: # not a leap year do nothing and use old value of days
else
# it is a leap year
days=29
fi
fi
# if day is negative (<0) and if day is more than
# that months days then day is invaild
if [ $dd -le 0 -o $dd -gt $days ];
then
echo "$dd day is invalid"
exit 3
fi
# if no error that means date dd/mm/yyyy is valid one
echo "$dd/$mm/$yy is a vaild date"
14. Write an awk program using function, which convert each word
in a given text into capital.
$ cat > test_lower_case
hi you are using awk script
$ cat < test_lower_case
hi you are using awk script
$ awk '{print toupper ($0) }' test_lower_case > test_uper_case
$ cat < test_uper_case
HI YOU ARE USING AWK SCRIPT
$
hi you are using awk script
$ cat < test_lower_case
hi you are using awk script
$ awk '{print toupper ($0) }' test_lower_case > test_uper_case
$ cat < test_uper_case
HI YOU ARE USING AWK SCRIPT
$
15. Write a program for process creation using C. (Use of gcc
compiler).
/* ----------------------------------------------------------------- */
/* PROGRAM fork-01.c */
/* This program illustrates the use of fork() and getpid() system */
/* calls. Note that write() is used instead of printf() since the */
/* latter is buffered while the former is not. */
/* ----------------------------------------------------------------- */
#include <stdio.h>
#include <string.h>
#include <process.h>
#include <sys/types.h>
#define MAX_COUNT 200
#define BUF_SIZE 100
void main(void)
{
pid_t pid;
int i;
char buf[BUF_SIZE];
fork();
pid = getpid();
for (i = 1; i <= MAX_COUNT; i++) {
sprintf(buf, "This line is from pid %d, value = %d\n", pid, i);
write(1, buf, strlen(buf));
}
}
/* PROGRAM fork-01.c */
/* This program illustrates the use of fork() and getpid() system */
/* calls. Note that write() is used instead of printf() since the */
/* latter is buffered while the former is not. */
/* ----------------------------------------------------------------- */
#include <stdio.h>
#include <string.h>
#include <process.h>
#include <sys/types.h>
#define MAX_COUNT 200
#define BUF_SIZE 100
void main(void)
{
pid_t pid;
int i;
char buf[BUF_SIZE];
fork();
pid = getpid();
for (i = 1; i <= MAX_COUNT; i++) {
sprintf(buf, "This line is from pid %d, value = %d\n", pid, i);
write(1, buf, strlen(buf));
}
}
nice one
ReplyDeleteThis comment has been removed by the author.
ReplyDelete