0% found this document useful (0 votes)
2 views

practical 1-10 shruti

The document provides an introduction to Linux, explaining its nature as a free and open-source operating system kernel, its various distributions, and its prevalence in server environments. It includes practical exercises on basic and advanced Linux commands, shell scripting, and operators, along with examples of how to create and execute shell scripts. The document serves as a tutorial for users to learn and practice essential Linux commands and scripting techniques.

Uploaded by

makwanashruti415
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

practical 1-10 shruti

The document provides an introduction to Linux, explaining its nature as a free and open-source operating system kernel, its various distributions, and its prevalence in server environments. It includes practical exercises on basic and advanced Linux commands, shell scripting, and operators, along with examples of how to create and execute shell scripts. The document serves as a tutorial for users to learn and practice essential Linux commands and scripting techniques.

Uploaded by

makwanashruti415
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Enrollment-no:226480316057 LOS(4331602)

INTRODUCTION TO LINUX

What Is Linux?

Linux is an operating system's kernel. You might have heard of UNIX. Well, Linux is a UNIX
clone. But it was actually created by Linus Torvalds from Scratch.

Linux is free and open-source, that means that you can simply change anything in Linux and
redistribute it in your own name! There are several Linux Distributions, commonly called
“distros”.

• Ubuntu Linux
• Red Hat Enterprise Linux
• Linux Mint
• Debian
• Fedora
Linux is mainly used in servers. About 90% of the internet is powered by Linux servers. This is
because Linux is fast, secure, and free! The main problem of using Windows servers are their
cost. This is solved by using Linux servers. The OS that runs in about 80% of the smartphones in
the world, Android, is also made from the Linux kernel. Most of the viruses in the world run on
Windows, but not on Linux!

1
Enrollment-no:226480316057 LOS(4331602)

Linux Shell or “Terminal”

So, basically, a shell is a program that receives commands from the user and gives it to the OS to
process, and it shows the output. Linux's shell is its main part. Its distros come in GUI (graphical
user interface), but basically, Linux has a CLI (command line interface). In this tutorial, we are
going to cover the basic commands that we use in the shell of Linux.

To open the terminal, press Ctrl+Alt+T in Ubuntu, or press Alt+F2, type in gnome-terminal, and
press enter. In Raspberry Pi, type in lxterminal. There is also a GUI way of taking it, but this is
better!

2
Enrollment-no:226480316057 LOS(4331602)

Practical – 1
Aim: Study of basic commands in Linux/Unix.
Solution:
1. pwd — When you first open the terminal, you are in the home directory of your user. To
know which directory you are in, you can use the “pwd” command. It gives us the
absolute path, which means the path that starts from the root. The root is the base of the
Linux file system. It is denoted by a forward slash( / ). The user directory is usually
something like "/home/username".
2. ls — Use the "ls" command to know what files are in the directory you are in. You can
see all the hidden files by using the command “ls -a”.
3. cd — Use the "cd" command to go to a directory. For example, if you are in the home
folder, and you want to go to the downloads folder, then you can type in “cd
Downloads”. To go back from a folder to the folder before that, you can type “cd ..” . The
two dots represent back.
4. mkdir & rmdir — Use the mkdir command when you need to create a folder or a
directory. For example, if you want to make a directory called “DIY”, then you can type
“mkdir DIY”. Remember, as told before, if you want to create a directory named “DIY
Hacking”, then you can type “mkdir DIY\ Hacking”. Use rmdir to delete a directory. But
rmdir can only be used to delete an empty directory. To delete a directory containing
files, use rm.
5. rm - Use the rm command to delete files and directories. Use "rm -r" to delete just the
directory. It deletes both the folder and the files it contains when using only the rm
command.
6. touch — The touch command is used to create a file. It can be anything, from an empty
txt file to an empty zip file. For example, “touch new.txt”.
7. cat Command: The cat command is a multi-purpose utility in the Linux system. It can be
used to create a file, display content of the file, copy the content of one file to another
file, and more. cat [OPTION]... [FILE]..
8. man & --help — To know more about a command and how to use it, use the man
command. It shows the manual pages of the command. For example, “man cd” shows the
manual pages of the cd command. Typing in the command name and the argument helps
it show which ways the command can be used (e.g., cd –help).
9. cp — Use the cp command to copy files through the command line. It takes two
arguments: The first is the location of the file to be copied, the second is where to copy.
10. mv — Use the mv command to move files through the command line. We can also use
the mv command to rename a file. For example, if we want to rename the file “text” to
“new”, we can use “mv text new”. It takes the two arguments, just like the cp command.
11. rename Command: The rename command is used to rename files. It is useful for
renaming a large group of files. rename 's/old-name/new-name/' files

3
Enrollment-no:226480316057 LOS(4331602)

Practical – 2
Aim: Study of advanced commands in Linux/Unix.
Solution:
1. echo — The "echo" command helps us move some data, usually text into a file. For
example, if you want to create a new text file or add to an already made text file, you just
need to type in, “echo hello, my name is alok >> new.txt”. You do not need to separate
the spaces by using the backward slash here, because we put in two triangular brackets
when we finish what we need to write.
2. cat — Use the cat command to display the contents of a file. It is usually used to easily
view programs.
3. Command Aliases: alias ALIASNAME="ACTUAL_COMMAND_NAME"
4. su Command: The su command provides administrative access to another user. In other
words, it allows access of the Linux shell to another user. Syntax: su <user name>
5. id Command: The id command is used to display the user ID (UID) and group ID (GID).
Syntax: id
6. useradd Command: The useradd command is used to add or remove a user on a Linux
server. Syntax: useradd username
7. passwd Command: The passwd command is used to create and change the password for a
user. Syntax: passwd <username>
8. grep Command: The grep is the most powerful and used filter in a Linux system. The
'grep' stands for "global regular expression print." It is useful for searching the content
from a file. Generally, it is used with the pipe. Syntax:command | grep <searchWord>
9. sort Command: The sort command is used to sort files in alphabetical order. Syntax: sort
<file name>
10. gzip Command: The gzip command is used to truncate the file size. It is a compressing
tool. It replaces the original file by the compressed file having '.gz' extension. Syntax:
gzip <file1> <file2> <file3>...
11. date Command
12. The date command is used to display date, time, time zone, and more. Syntax: date
13. cal Command: The cal command is used to display the current month's calendar with the
current date highlighted. Syntax: cal
14. time Command: The time command is used to display the time to execute a command.
Syntax: time

4
Enrollment-no:226480316057 LOS(4331602)

Practical – 3
Aim: Introduction to shell scripting:
a)shebang line
b)creation of shell file
c)changing of file mode
d)hello world in shell scripting
Solution:
What is Shell? A shell is special user program which provide an interface to user to use
operating system services. Shell accept human readable commands from user and convert them
into something which kernel can understand. It is a command language interpreter that execute
commands read from input devices such as keyboards or from files. The shell gets started when
the user logs in or start the terminal.
There are several shells are available for Linux systems like –
1. BASH (Bourne Again SHell) – It is most widely used shell in Linux systems. It is used as
default login shell in Linux systems and in macOS. It can also be installed on Windows
OS.
2. CSH (C SHell) – The C shell’s syntax and usage are very similar to the C programming
language.
3. KSH (Korn SHell) – The Korn Shell also was the base for the POSIX Shell standard
specifications etc.
Each shell does the same job but understand different commands and provide different built in
functions.
Shell Scripting: Usually shells are interactive that mean, they accept command as input from
users and execute them. However some time we want to execute a bunch of commands routinely,
so we have type in all commands each time in terminal.
As shell can also take commands as input from file we can write these commands in a file and
can execute them in shell to avoid this repetitive work. These files are called Shell Scripts or
Shell Programs. Shell scripts are similar to the batch file in MS-DOS. Each shell script is saved
with .sh file extension eg. myscript.sh
A) Shebang line: It is called a shebang or a "bang" line. It is nothing but the absolute path to the
Bash interpreter. It consists of a number sign and an exclamation point character (#!), followed
by the full path to the interpreter such as /bin/bash. All scripts under Linux execute using the
interpreter specified on a first line. # !/bin/bash
B) Creation of shell file:
Open terminal

5
Enrollment-no:226480316057 LOS(4331602)

Create folder and give it a name as shell scripting


Go to that folder using cd command
Create .sh file: touch (filename).sh
Open file from folder
Write code and save file
C) Changing of file mode:
Go to terminal
Use chmod command to change the mode of file: Chmod +x filename.sh
Use ./filename.sh for executing file
D) Hello world in shell script:
#!/bin/sh
echo "Hello World" x=30
y=20
z=$(( $x + $y |bc)) echo "$z"

Output:-

spce@spce-H81MHV3: $ cd darshan
spce@spce-H81MHV3: /darshan$ touch he11owor1d.sh spce@spce-H81MHV3: /darshan$
touch Arithmetic operation.sh spce@spce-H81MHV3: /darshan$ chmod +x helloworld.sh
spce@spce-H81MHV3: /darshan$ ./he1lowor1d.sh
Hello World 50

Sum:30
Method 2
Sum:30.50
method 3

6
Enrollment-no:226480316057 LOS(4331602)

Sum:30

Practical-5
Aim: Operators in shell scripting:
a)Arithmetic
b)Equality
c)Relational
d)Logical
Solution:

7
Enrollment-no:226480316057 LOS(4331602)

Practical-6

Aim: Mark sheet of student:


a)take marks of 3 subjects from user
b)calculate summation and average
c)use relational operator for various classes
Solution:
#!/bin/sh
echo "enter three subject marks"
read a b c
sum=`expr $a + $b + $c`
echo "sum=$sum"
avg=$(echo "$sum / 3" | bc)
echo "avg=$avg"
if [ $avg -ge 70 ]
then
echo "first class"
elif [ $avg -ge 50 ]
then
echo "second class"
else
echo "fail"
fi
Output:
spce@spce-H81MHV3:~/Hem$ chmod +x marksheet
spce@spce-H81MHV3:~/Hem$ ./marksheet

8
Enrollment-no:226480316057 LOS(4331602)

enter three subject marks


45 85 63
sum=193
avg=64
second class

9
Enrollment-no:226480316057 LOS(4331602)

Partical-7

Practical – 4
Aim: Methods for Arithmetic Operations:
a)EXPR method
b)Bench calculator method
c)calculator using bench calculator method
Solution:
There are three different methods for arithmetic operations
1. Z=` expr $X + $Y`
2. Z=$(echo "$X + $Y" | bc ) (bench calculator)
3. Z=$(($X + $Y))
Bench calculator is the best method among all three because it work for floating point value as
well.
calculator using bench calculator method:
#!/bin/sh
echo "Enter Two Value" read a b
echo "Method 1" z=”expr $a + $b” echo "Sum:$z" echo "Method 2"
z=$(echo "$a + $b" |bc)
echo "Sum:$z" echo "method 3" z=$(($a + $b)) echo "Sum:$z"
spce@spce-H81MHV3: /darshan$ ./Arithmetic operation.sh
Enter Two Value

10
Enrollment-no:226480316057 LOS(4331602)

10 20.50
Method 1
Aim: Introduction to conditional statements:
a)if
b)if … else
c)elif
d)find given number is positive or negative
Solution:
a) if
Syntax:
If [ conditional expression ]
then
Statement 1
Statement 2
Fi
b)if … else
Syntax:
if [ condition ]
then
Statement 1
Statement 2
else
Statement 3
statement 4
fi
c) elif
elif ladder (if elif else)
Syntax:

11
Enrollment-no:226480316057 LOS(4331602)

if [ condition ]
then
Statement 1
Statement 2
elif [ condition ]
then
Statement 3
statement 4
else
Statement 4
Statement 5
Fi

#!/bin/sh
echo "enter one number"
read a
if [ $a -gt 0 ]
then
echo "postive"
elif [ $a -lt 0 ]
then
echo " negative"
else
echo "zero"
fi

Output:

12
Enrollment-no:226480316057 LOS(4331602)

spce@spce-H81MHV3:~/Hem$ chmod +x marksheet


spce@spce-H81MHV3:~/Hem$ ./marksheet
enter one number
1
postive
spce@spce-H81MHV3:~/Hem$ ./marksheet
enter one number
-8
negative
spce@spce-H81MHV3:~/Hem$ ./marksheet
enter one number
0
Zero

13
Enrollment-no:226480316057 LOS(4331602)

Practical-8
Aim: Write a shell script to find given number is odd or even using equality operator.
Solution:
#!/bin/sh
read n
temp=`expr $n % 2`
if [ $temp -eq 0 ]
then
echo "even"
else
echo "odd"
fi

Output:

spce@spce-H81MHV3:~/Hem$ chmod +x marksheet


spce@spce-H81MHV3:~/Hem$ ./marksheet
3
odd
spce@spce-H81MHV3:~/Hem$ ./marksheet
6
Even

14
Enrollment-no:226480316057 LOS(4331602)

Practical-9
Aim: Introduction to looping statements:
a)while
b)until
c)for
d)find factorial using while loop
Solution:
There are 3 types of loops in shell script
1) While
2) Until
3) For
Syntax:
a)While loop:
While [ condition ]
Do
Statement 1
Statement 2
Done
b) until loop
until [ expression ]
do
statement 1
statement 2
done
c) for loop:
for variable in word1 word2 word3
do

15
Enrollment-no:226480316057 LOS(4331602)

statement 1
statement 2
done
d)factorial using while loop
#!/bin/sh
echo ”Enter Any Number” read num
fact=1
while [ $num -gt 1 ]
do fact=$((fact * num)) num=$((num - 1)) done
echo ”Factoria1=$fact”

Output:-

spce@spce-H81MHV3: $ cd darshan
spce@spce-H81MHV3: /darshan$ touch factoria1.sh spce@spce-H81MHV3: /darshan$ chmod
+x factorial.sh spce@spce-H81MHV3: /darshan$ ./factorial.sh
Enter Any Number
s Factoria1=120

16
Enrollment-no:226480316057 LOS(4331602)

Practical-10
Aim: Introduction to switch case:
a)syntax
b)menu driven calculator using switch case
Solution:
a)syntax:
case var_name in
pattern 1)
statement 1 ;;
pattern 2)
statement 2 ;;
pattern 3)
statement 3 ;;
*)
#this is default case
esac
b)calculator shell script
#!/bin/sh
echo ”Menu driven calculator” echo ”Enter Two Number" read a b
echo ”1.add 2.sub 3.mu1 4.div 5.exit" read op
case $op in
1) c =”expr $a + $b”;;
2) c =”expr $a - $b”;;
3) c =”expr $a \* $b”;;
4) c =”expr $a / $b”;;
5) exit esac
echo ”result=$c”

17
Enrollment-no:226480316057 LOS(4331602)

Output:-

pce@spce-H81MHV3: $ cd darshan
spce@spce-H81MHV3: /darshan$ touch switchcase.sh spce@spce-H81MHV3: /darshan$ chmod
+x switchcase.sh spce@spce-H81MHV3: /darshan$ ./switchcase.sh
Menu driven calculator Enter Two Number
12
1.add 2.sub 3.mul 4.div 5.exit
i
./switchcase.sh: 8: c: not found result=
spce@spce-H81MHV3: /darshan$ ./switchcase.sh Menu driven calculator
Enter Two Number 1 2
1.add 2.sub 3.mul 4.div 5.exit
i
result=3
spce@spce-H81MHV3: /darshan$ ./switchcase.sh Menu driven calculator
Enter Two Number 2 3
1.add 2.sub 3.mul 4.div 5.exit 2
result=-1
spce@spce-H81MHV3: /darshan$ ./switchcase.sh Menu driven calculator
Enter Two Number 7 8
1.add 2.sub 3.mul 4.div 5.exit 3
result=56

18
Enrollment-no:226480316057 LOS(4331602)

Practical-11
Aim: Write a menu driven shell script which will print the following:
a)display calendar of current month
b)display today’s date and time
c)display user names those are currently logged in the system
d)display your terminal number
Solution:

19

You might also like