Nothing Special   »   [go: up one dir, main page]

tech@NISHANT

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 23

Practical : 1

Study of basic Commands of LINUX/UNIX.


1

1. Echo
tech@NISHANT:-$ echo 11
hello students............11
hello students ....

2. Cal
tech@NISHANT:-$ cal
May 202Q
Su Mo Tu We Th Fr Sa
1 2 ·3 LI
5 6 7 8 9 10 11
12 13 lQ 15 16 17 18
19 20 21 22 23 2Q 25
26 27 28 29 30 31

3. clear
tech@NISHANT:-$

4. Ls
tech@NISHANT: $ls
avgmark.sh NISHANT.sh dirl factorial.sh file.sh pelindrome.sh
table.sh
cal.sh NISHANTl.sh disorder.sh fibonnacci.sh menu.sh primenumber.sh
validdate.sh •

5. Cd
tech@NISHANT:N$ cd dirl
tech@NISHANT:N/dir $

6. Pwd
tech@NISHANT:N$ pwd
/home/tech

7. Mkdir

Page 1 of 8
tech@NISHANT:~$ mkdir dir2
tech@NISHANT:~$ ls
avgmark.sh N I S H A N T .sh dirl disorder.sh pr1• menum
fibonnacci.sh menu.sh ber.sh
validdate.sh
cal.sh NISHANTl.sh dir2 factorial.sh pelindrome.sh table.sh
file.sh •

8. Rmdir
tech@NISHANT:~$ rmdir
dir2 tech@NISHANT:~$ ls
avgmark.sh dirl file.sh table.sh
cal.sh disorder.sh menu.sh validdate.sh
NISHANT.sh factorial.sh pelindrome.sh
NISHANTl.sh primenumber.sh
fibonnacci.sh

9.Mv

tech@NISHANT:~/dir1$ ls
NISHANT.txt
tech@NISHANT:~/dir1$ cd ..
tech@NISHANT:~$ ls
avgmark.sh NISHANl.sh factorial.sh
cal.sh dirl fibonnacci.sh menu.sh table.sh
NISHANT.sh disorder.sh file.sh pelindrome.sh validdate.sh
tech@NISHANT:~$ vi text.txt primenumber.sh
tech@NISHANT:~$ ls
avgmark.sh NISHANTl.sh factorial.sh
cal.sh dirl fibonnacci.sh menu.sh table..sh
chetan.sh disorder.sh file.sh pelindrome.sh text.txt
tech@CHETAN:~$ mv text.txt dirl primenumber.sh validdate.sh
tech@CHETAN:-$ ls
avgmark.sh chetanl.sh factorial.sh menu.s.h table.sh
cal.sh dirl fibonnacci.sh pelindrome.sh validdate.sh
chetan.sh disorder.sh file.sh primenumber.sh
tech@CHETAN:-$ cd
dirl
tech@CHETAN:-/dir1$
ls chetan.txt
text.txt
tech@CHETAN:~/dir1$

10. Cp

tech@CHETAN:~$ vi textl.txt
tech@CHETAN:~$ ls
avgmark.sh chetanl.sh chetan.sh disorder.sh $ cd dirl
cal.sh dirl tech@CHETAN:~ tech@CHETAN:~
Page 2 of 8
/dir1$ ls chetan.txt factorial.sh menu.sh table.sh
text.txt fibonnacci.sh pelindrome.sh textl.txt
file.sh primenumber..sh validdate..sh

Page 3 of 8
tech@CHETAN:~/dir1$ cd ..
tech@CHETAN:~$ cp textl.txt dirl
tech@CHETAN:~$ cd dirl
tech@CHETAN:-/dir1$ la
chetan.txt text.txt textl.txt
tech@CHETAN:-/dir1$

Practical : 2

2 Study of Advance commands and filters of Linux/UNIX

1. Cat
tech@CHETAN:-$ cat >File.txt
hello students
tech@CHETAN:~$ ls
File.txt cal.sh chetanl.sh di.sorder.s fibonnacci.s
h menu.sh primenumber.sh h textl.txt
avgmark.sh chetan.sh dirl factorial.sh file.sh
pelindrome.sh table.sh validdate.sh

2. Rm
tech@CHETAN:~$ vi cal.sh
tech@CHETAN:~$ ls
File.txt cal.sh chetanl.sh disorder.sh fibonnacci.
sh menu.sh primenumber.sh textl.txt
avgmark.sh chetan.sh dirl factorial.sh file.sh
pelindrome.sh table.sh validdate.sh
tech@CHETAN:~$ rm cal.sh
tech@CHETAN:~$ ls

File.txt chetan.sh dirl factorial.sh file.sh


pelindrome.sh table.sh validdate.sh
avgmark.sh chetanl.sh disorder.sh menu.sh
fibonnacci.sh
primenumber.sh textl.txt

3. . We
tech@CHETAN:~$ wc textl.txt
2 Q 17 textl.txt

4. Chmod
tech@CHETAN:~$ chmod 755 textl.txt

Page 4 of 8
5. Exit

Page 5 of 8
6. Uname
tech@CHETAN:-$ uname
Li·nux •

7. Who

8. Whoami
tech@CHETAN:-$ whoami
tech

9. Date
tech@CHETAN:-$ date
Sun May 5 19:09:09 IST 202Q

10. Time
-
tech@CHETAN:-$ time
real 01n0.000s
u.ser 0m0.000s
sys 01n0.000s

Page 6 of 8
Practical : 3

3 Write a shell script to generate marksheet of a student. Take 3 subjects, calculate and display total marks, percen

Program:-

echo "*****************"
echo "Student
Marksheet" echo
"*****************" echo
"Enter maths Marks:" read
maths
echo "Enter physics Marks:"
read physics
echo "Enter chemistry Marks:"
read chemistry
echo "*****************"
total=' expr $maths + $physics + $chemistry'
echo "Total Marks:"$total
percentage=' expr $total / 3'
echo "Percentage:" $percentage 0/o
if [ $percentage -ge 60 ]
then
echo "Class: First Class Distinction"
elif [ $percentage -ge 50 ]
then
echo "Class: First class"
elif [ $percentage -ge 40 ]
then
echo "Class: Second class"
else
echo "Class: Fail"
fi
echo "*****************"

Page 7 of 8
output : -
tech@CHETAN:-$ vi avgmark.sh
tech@CHETAN:-$ sh avgmark.sh
*****************

Student Marksheet
*****************

Enter maths Marks:


95
Enter physics Marks:
86
Enter chemistry Marks:
91
*****************

Total Marks:272
Percentage: 90 %
Class: First Class Distinction
*****************

Practical : 4

4 Write a shell script to display multiplication table of given number

Program:-

echo "Enter Number to Generate Multiplication Table"


read -p "Enter the number : " number
echo "***********************"
i=1
while [ $i -le 1O ]
do
echo" $number* $i ='expr $number\* $i' "
i=' expr $i + 1'
done
echo ''***********************''

output : -
tech@CHETAN:-$ vi table.sh
Page 8 of 8
tech@CHETAN:-$ sh table.sh
Enter Number to Generate Multiplication Table
Enter the number : 5
***********************
5*1=5
5*2=10
5*3=15
5 * 4=20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
''***********************''

Practical : 5

5 Write a shell script to find factorial of given number n.

Program:-

read -p "Enter the number : " number


fact=1

while [ $number -gt 1 ]


do
fact=$((fact * number))
number=$((number - 1))
done

echo "The factorial is :" $fact

Output: -

tech@CHETAN:-$ vi factorial.sh
tech@CHETAN:-$ sh factorial.sh
Enter the number: 4
The factorial is : 24

Page 9 of 8
Practical : 6

6 Write a shell script which will accept a number band display first n prime numbers as output.

Program:-

read -p "Enter the NUmber: " n


11
read -p "Enter the Number: n
echo "The prime numbers up to $n
11
are:
m=2
while [ $m -le $n ]
do
i=2
flag=O
while [ $i -le 'expr $m / 2' ]
do
if ['expr $m % $i' -eq O ] ;
then
flag=1
break
fi
i=' expr $i + 1'
done
if [ $flag -eq O ] ; then
echo $m
fi
m='expr $m + 1'
done

Output: -

tech@CHETAN:-$ vi
primenumber.sh
tech@CHETAN:-$ sh
primenumber.sh
Page 10 of
8
Enter the Number: 11
The prime numbers up to 11 are:
2
3
5
7
11

Practical : 7

6 Write a shell script which will accept a number band display first n prime numbersas output.

Program:-

read -p "Enter the Number: " number


X=O
y=1
i=2

echo "Fibonacci Series Upto $number Number: "


echo "$x"
echo "$y"

while [ $i -It $number ]


do
i=$((i + 1))
Z=$((X + y))
echo "$z"
X=$y
y=$Z
done

Output: -

tech@CHETAN:-$ vi fibonnacci.sh
tech@CHETAN:-$ sh
fibonnacci.sh Enter the Number: 9
Fibonacci Series Upto 9 Number:
Page 11 of
8
0
1
1
2
3
5
8
13
21

Practical : 8

1
7

Write a shell script which will generate first n fibonnacci numbers like: 1, 1, 2, 3, 5,13, ...

Program:-

#! /bin/bash
lecho ''Select Anyone Option 11

echo ,1**************************************************,,
II 1)
echo Display Calendar of the currentmonth''
.echo 112) Display Today's Date and Time11
echo II 3)
Display Username 1r1ho are currently logged in"
echo '' Q) Display your name on the given x, y position''
II 5) your terminal Number''
echo Display
echo '' 6) Exit''
echo ''Enter your choice:''
read ch

case $ch in
1) cal · ·
2) date ' ;' ;
3) who;;
q)

Page 12 of
row=$(tput lines)
col=$(tput cols)
echo ''Terminal Window has Rows=$row Cols=$col ''
echo 11Enter desired X, Y position''
echo ''X
position='' read x
echo 11V position=''
ready
echo "Enter the name''
read name
tput cup $x $y
echo 11 $name 1,1 • '•
5) tty ; ;
6) echo 11 Exit 11 •,•,
*) echo "Enter a valid choice" ·f ·I
esac

Output: -

tech@CHETAN:-$ vi menu.sh
tech@CHETAN:-$ sh menu.sh
Select Anyone Option
1) Display Calendar of the current month
2) Display Today's Date and Time
3) Display Username who are currently logged in
4) Display your name on the given x,y position
5) Display your terminal Number
6) Exit
Enter your choice:
2
Sun May 5 10:24:11 1ST 2024

Practical : 9

8 Write a menu driven shell script which will print the following menu and execute
he given task.

Program:-

read -p "Enter The Number: "n

for ((i=O; i<$n; i++))

do
Page 13 of
read -p "Enter value of arr[$i]: "
arr[$i]

done

# Sorting code

for ((i=O; i<$n; i++))

do

for ((j=O; j<$(($n - i - 1)); j++))

do

if [ ${arr[j]} -It ${arr[$((j+1))]} ]

then

# Swapping temp=$

{arr[j]} arr[$j]=${arr[$

((j+1))]} arr[$

((j+1))]=$temp

fi

done

done

echo "Numbers in Descending order: "


${arr[*]}

Output: -

Enter The Number: 5

Enter value of arr[O]: 67

Enter value of arr[1]: 2

Enter value of arr[2]: 34

Page 14 of
Enter value of arr[3]: 108

Enter value of arr[4]: 23

Numbers in Descending order: 108


67 34 23 2

Practical : 1O

9 Write a shell script to read n numbers as command arguments and sort them in
descending order.

Program:-

echo Executable files


files=$(find lab_solutions -executable -type f)
echo $files
echo
echo List of Directories
dir=$(1s -d )
echo $dir
echo
echo List of zero sized files zero=$
(find -size 0)
echo $zero

Output: -

tech@CHETAN:-$ vi practical_9.sh
tech@CHETAN;-$ sh practical_9.sh

List of Directories dir=.


echo
List of zero sized files zero=./.sudo_as_admin_successful
./.motd_shown ./manu.sh ./chetanl.sh

Practical : 11

10 Write a shell script to display all executable files, directories and zero sized files
fromcurrent directory.

Page 15 of
Program:-

echo "Input your string without space"


read vstr
for (( i=O; i<${#vstr}; i++ ))
do
rvstr=${vstr:$i:1}${rvstr}
done
echo "Input string was: $vstr"
echo "After Reversing String Is: $rvstr"
if [ "$vstr" = "$rvstr" ]
then
echo "String Is Palindrome."
else
echo "String Is Not Palindrome."
fi

Output: -

Input your string without space


hello
Input string was: naman
After Reversing String Is: naman
String Is Palindrome.

Practical : 12

12. shell programming using filters(including grep , egrep ,fgrep)

Program:-

# Search for lines containing "error" in a log file


grep "error" logfile.txt

# Search for lines containing "error" or "warning" using egrep


egrep "errorlwarning" logfile.txt

# Search for lines containing the exact string "error" using fgrep
fgrep "error" logfile.txt
Page 16 of
Practical : 13

13. Study of Unix Shell and Environment Variables.

A shell provides us with an interface to the unix system. It takes input from us and executes
programs based on that input.

Shell is an environment where we can run our commands, programs and shell scripts. There
are different types of Shell as there are different types of operating systems.

Shell Prompt

The prompt$ which is called the command prompt, is issued by the shell. While the prompt
is displayed, you can type a command.

Types of shells

Bourne shell

In this type of shell the $ character is the default prompt.

Subcategories of Bourne shell

Bourne shell (sh)

Korn shell (ksh)

Bourne Again shell (bash)

POSIX shell (sh)

C shell
In this type of shell the 0/o character is the default

prompt. Sub categories of C-type shell

• C Shell (csh)

• TENEX/TOPS C shell

(tcsh) Envi ranment Variables

Environment Variables are variables that are set up in your shell when you login. They are
called "environment Variables" because most of them affect the way your UNIX shell works
for you.

Environment Variables allows us to customize how the system works and the behavior of the
applications of the system.

How to see Environment Variables in ubuntu?


Page 17 of
printenv is used to display environment variables in ubuntu.

Output: -
- --
CH :~tech@
ATE
N printenv
SHELL=/bin/bash
WSL_DISTRO_NAME=Ubuntu
NAME=CHETAN
PWD=/home/tech
LOGNAME=tech
HOME=/home/tech
LANG=C.UTF-8
WSL_INTEROP=/run/WSL/Q9_interop
LS_COLORS=rs=0:di=01;3Q:ln=01;36:mh=00:pi=Q0;33:so=01;35:do=01;35:bd=Q0;33;01:cd
=Q0;33;01:or=Q0;31;01:mi=00:su=37;Ql:sg=30;Q3:ca=30;Ql:tw=30;Q2:ow=3Q;Q2:st=37;Q
Q:ex=01;32:•.tar=01;31:*.tgz=01;31:•.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;
31:*.lzQ=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7
z=01;31:*.zip=01;31:*.z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo
=01;31:*.xz=01;31:*.zst=01;31:*.tzst=01;31:•.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.
tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;3
l:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=
01;31:*.rz=01;31:*.cab=01;31:*.wim=01;31:*.swm=01;31:*.dwm=01;31:*.esd=01;31:*.j
pg=01;35:*.jpeg=01;35:*.mjpg=01;35:*.mjpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=0
1;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.t
iff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;
35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.webp=01;35:*.
ogm=01;35:*.mpQ=01;35:*.mQv=01;35:*.mpQv=01;35:*.vob=01;35:*.qt=01;35:•.nuv=01;3
5:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:•.avi=01;35:*.fli=
01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:•.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cg
m=01;35:*.emf=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:
•.mQa=00;36:*.mid=00;36:*.midi=00;36:•.mka=00;36:•.mp3=00;36:•.mpc=00;36:•.ogg=0
0;36:•.ra=00;36:•.wav=00;36:*.oga=00;36:•.opus=00;36:•.spx=00;36:•.xspf=00;36:
LESSCLOSE=/usr/bin/lesspipe %s %s
TERM=xterm-256color

LESSOPEN=I /usr/bin/lesspipe %s
USER=tech
SHLVL=l
WSLENV=
XDG_DATA_DIRS=/usr/local/share:/usr/share:/var/lib/snapd/desktop
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/us
r/local/games:/usr/lib/wsl/lib:/mnt/c/Program Files/Common Files/Oracle/Java/jav
apath:/mnt/c/Program Files/Python311/Scripts/:/mnt/c/Program Files/Python311/:/m
nt/c/Windows/system32:/mnt/c/Windows:/mnt/c/Windows/System32/Wbem:/mnt/c/Windows
/System32/WindowsPowerShell/vl.0/:/mnt/c/Windows/System32/0penSSH/:/mnt/c/Progra
m Files/NVIDIA Corporation/NVIDIA NvDLISR:/mnt/c/WINDOWS/system32:/mnt/c/WINDOWS
:/mnt/c/WINDOWS/System32/Wbem:/mnt/c/WINDOWS/System32/WindowsPowerShell/vl.0/:/m
nt/c/WINDOWS/System32/0penSSH/:/mnt/c/Program Files/dotnet/:/mnt/c/MinGW/bin:/mn
tic/Program Files/nodejs/:/mnt/c/Program Files/Git/cmd:/mnt/c/Program Files/Mong
oDB/Server/7.0/bin:/mnt/c/Users/cheta/AppData/Local/Microsoft/WindowsApps:/mnt/c
/Users/cheta/AppData/Local/Programs/Microsoft VS Code/bin:/mnt/c/Program Files/J
etBrains/IntelliJ IDEA Community Edition 2023.3.3/bin:/mnt/c/Users/cheta/AppData
/Roaming/npm:/mnt/c/Program Files/mongosh/:/snap/bin
HOSTTYPE=x86_6Q
_=/usr/bin/printenv

Page 18 of
Practical : 14

11 Write a shell script to check entered string is palindrome or not.

Program:-

dd=0
mm=0
yy=0
days=0
read -p ''enter day ( dd) : dd 11

read -p ''enter month (mm) : 11

mm read -p
''enter year (yy) : ''
yy if [$mm-le 0 -o $mm -gt
12] then
echo "$mm is invalid month: "
exit 1
fi
case $mm in
1 I 3 I s I 7 I a I 10 I 12)
days=31
• •

2) ''
days=28
• •
f ,

LI I 6 I 9days=30
I 11 )

*)
., • •

days=-1
, • ,•
esac
if [ $mm -eq 2 ]
then
if [ $((yy % LI)) -eq
0] then
days=29
fi
fi
if [ $dd -le 0 -o $dd -gt
$days] then
echo $dd is invalid''
11

exit 3
fi
Page 19 of
Output: -

tech@CHETAN: $ sh validdate.sh
valid date
enter day (dd) : 23
enter month (mm) : 85
enter year (yy) : 2015
85 is invalid month:

Practical : 15

15. Write an awk program using function, which convert

each word in a given text into capital.

Program:-

echo Enter the String


11 11

a=$(awk 'BEGIN{
getline str;
print toupper(str);
}')
echo $a

Output: -

tech@CHETAN: $ vi upercase.sh
tech@CHETAN: $ sh upercase.sh
Enter the String
hello students....
HELLO STUDENTS ....

Practical : 16

16. Write a program for process creation using C. (Use of gee


Page 20 of
compiler).

Program:-

#include &lt;stddef.h&gt;

#include &lt;stdlib.h&gt;

#include &lt;unistd.h&gt;

#include &lt;sys/types.h&gt;

#include &lt;sys/wait.h&gt;

/* Execute the command using this shell program. */

#define SHELL &quot;/bin/sh&quot;

int

my_system (canst char *command)

int status;

pid_t pid;

pid = fork

();

if (pid == 0)

/* This is the child process. Execute the shell command. */

exec! (SHELL, SHELL, &quot;-c&quot;, command, NULL);

_exit (EXIT_FAILURE);

else if (pid &It; 0)

/* The fork failed. Report failure. */

Page 21 of
status = -1·
'

Page 22 of
else

/* This is the parent process. Wait for the child to complete. */

if (waitpid (pid, &amp;status, 0) != pid)

status = -1·
'
return status;

Page 23 of

You might also like