Sa Jochu
Sa Jochu
WOMEN’S
POLYTECHNIC COLLEGE
KALAMASSERY
LABORATORY RECORD
YEAR: 2024
1|Page
GOVT. WOMEN’S
POLYTECHNIC COLLEGE
KALAMASSERY
LABORATORY RECORD
YEAR: 2024
CERTIFICATE
In System Administration Laboratory by Sri Jyolsna E.B during the year 2024
2|Page
INDEX
Sl DATE PAGE INITIAL
No: No : OF
NAME OF EXPERIMENT TEACHE
R
01 FAMILIARIZATION OF LINUX OS, FILES AND 22-08-2024 04
DIRECTORY ORGANIZATION
02. SYSTEM MANAGEMENT COMMANDS 22-08-2024 07
3|Page
Experiment No : 01 Date : 22-08-2024
4|Page
3. Hardware layer
Linux operating system contains a hardware layer that consists of several peripheral
devices like CPU, HDD, and RAM.
4. System Utility Programs
It is responsible for doing specialized level and individual activities.
5. Shell
It is an interface among the kernel and user. It can take commands through the user
and runs the functions of the kernel. The shell is available in distinct types of OSes.
These operating systems are categorized into two different types, which are the
graphical shells and command-line shells. The graphical line shells facilitate the
graphical user interface, while the command line shells facilitate the command line
interface.
Types of Shells
o Korn shell
o Bourne shell
o C shell
o POSIX shell
FEATURES
o Portable
o Open source
o Multiprogramming
o Multi-user
o Hierarchical file system
o Security
o Shell
APPLICATIONS
Thousands of governments and companies are using Linux operating system across
the world because of lower money, time, licensing fee, and affordability. Linux can be
used within several types of electronic devices. These electronic devices are easily
available for users worldwide.
5|Page
A few of the famous Linux-based electronic devices are :Yamaha Motive Keyboard,
Volvo In-Car Navigation System, TiVo Digital Video Recorder, Sony Reader, Sony
Bravia Television, One Laptop Per child XO2,Motorola MotoRokr EM35 phone,
Lenovo IdeaPad S9,HP Mini 1000,Google Android Dev Phone 1,Garmin Nuvi 860,
880, and 5000,Dell Inspiron Mini 9 and 12.
LINUX FILE SYSTEM AND DIRECTORY
A file system is a method for storing and organizing data on a computer. It is
responsible for managing data stored on disk and other storage devices. The Linux file
system is designed to provide a hierarchical and organized structure for files and
directories, making it easy to navigate and manage data on the system.
One of the primary functions of directories in Linux is to provide a hierarchical
structure for organizing files The Linux file system structure is a complex hierarchy
that includes multiple layers, each with its own role in managing and organizing data
on storage devices. At the top of the hierarchy is the root directory (/), which contains
all other directories and files. Below the root directory are several subdirectories, each
with its own purpose and set of files. With the help of this hierarchical structure,
complicated systems can be structured logically and organized. This makes it simpler
to locate and handle files on the system.
RESULT
Familiarized with Linux Operating System, Files and Directories.
6|Page
Experiment No : 02 Date : 22-08-2024
AIM :
To familiarize with the management of system using Linux commands - date, time,
pwd, ps, who, whoami, uptime, users, groups.
PROCEDURE:
1. Write command to display Current Date and Time in Linux
Command: $date
2. Write command to display Time in GMT
Command: $date -u
3. Write command to display the Given Date String in the Format of Date
Command: $date -d "2023-12-31"
4. Write command to display Past Dates
Command:$date --date="5 days ago"
5. Write command to display Future Dates
Command: $date --date="10 days"
6. Write command to set the System Date and Time
Command: $sudo date -s "2024-10-05 12:00:00"
7. Write command to use of Format specifiers with date command
Command: $date +"%Y-%m-%d %H:%M:%S"
8. Write command to print the current working directory in Linux
Command: $pwd
9. Write command to redirect the output of ‘pwd’ to a file in Linux?
Command: $pwd > filename.txt
10. Write command to shows the processes for the current shell.
Command: $ps
7|Page
11. Write command to view all running processes.
Command: $ps -A
12. Write command to show list of users logged in to system
Command: $who -u
13. Write command to show time of the system when it booted last time
Command: $who -b -H
8|Page
OUTPUT
7. 2024-10-05 12:34:45
8. /home/user
RESULT
The above commands are executed successfully and obtained desired output.
9|Page
Experiment No : 03 Date : 9-09-2024
USER AND GROUP MANAGEMENT COMMANDS
AIM:
To familiarize with the management of user and groups using Linux commands like
useradd, passwd, usermod, userdel, groupadd, groupmod, groupdel, and gpasswd.
PROCEDURE:
10 | P a g e
OUTPUT:
5. root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
...
username:x:1001:1001:,,,:/home/username:/bin/bash
Output for compgen -u:
root
daemon
bin
sys
...
username
11 | P a g e
9. Changing the password for group newgroup
New Password:
Re-enter new password:
12. Removing user username' ... Warning: group username' has no more members.
Done.
RESULT
The above commands are executed successfully and obtained desired output.
12 | P a g e
Experiment No : 04 Date : 4-09-2024
AIM:
To familiarize with management of files and directories and its permissions using
Linux commands-ls, mkdir, cd, rmdir, touch, cp, mv, rm, chmod, chage, sudo, chown.
PROCEDURE:
1. Write command to list all files of the current directory
Command: $ls
2. Write command to display one file per line
Command: $ls -1
3. Write command to display all information about files/directories
Command: $ls -l
4. Write command to display directory information
Command: $ls –ld
5. Write command to display hidden files
Command: $ls -a
6. Write command to create a folder/directory named WPTC
Command: $mkdir GPCK
7. Write command to create a subfolder/subdirectory named CT in WPTC and S5 in
CT
Command: $mkdir -p WPTC/CT/S5
8. Write command to create the directory at desired location
Command: $mkdir /desired/path/directory_name
9. Write command to quickly go back
Command: $cd --
10. Write command to move up to one folder
Command: $cd ..
11. Write command to go back to the original location
Command: $cd –
12. Write command to create a new file named biodata.txt in directory S5
13 | P a g e
Command: $touch WPTC/CT/S5/biodata.txt
13. Write command to rename the file biodata.txt to biodata1.txt
Command: $mv WPTC CT/S5/biodata.txt WPTC/CT/S5/biodata1.txt
14. Write command to copy file in the current directory
Command: $cp file1.txt file1_copy.txt
15. Write command to copy file to another directory
Command: $cp file1.txt /path/to/destination/
16. Write command to remove a file
Command: $rm file1.txt
17. Write command to remove a directory
Command: $rmdir directory1
18. Write command to give read, write and execute permissions to the file owner
using symbolic mode
Command: $chmod o+rwx file1.txt
19. Write command to remove write permission for the group and others using
symbolic mode
Command: $chmod go-w file1.txt
20 .Write command to give read and write permission to the file Owner, read, write,
and executable permission to the Group, and read-only permission to Others using
octal mode
Command: $chmod 674 file1.txt
21. Write command to give read and write permission to the file Owner, read, write,
and executable permission to the Group, and read-only permission to Others using
octal mode
Command: $chmod 674 file1.txt
22. Write command to display the version number
Command: $uname -r
23. Write command to change the owner of a file
Command: $sudo chown newuser file1.txt
24. Write command to view and change the user password expiry information
Command: $chage -l username
14 | P a g e
OUTPUT:
1. file1.txt file2.txt directory1 directory2
2. file1.txt
file2.txt
directory1
directory2
11. /previous/working/directory
15 | P a g e
19. no output, permissions updated
22. 5.11.0-37
RESULT
The above commands are executed successfully and obtained desired output.
16 | P a g e
Experiment No : 05 Date : 4-09-2024
AIM:
THEORY
Input Output Redirection in Linux
Input/output (I/O) redirection in Linux refers to the ability of the Linux operating
system that allows us to change the standard input ( stdin ) and standard output (
stdout ) when executing a command on the terminal. By default, the standard input
device is keyboard and the standard output device is screen.
For redirection, meta characters are used. Redirection can be into a file (shell meta
characters are angle brackets '<', '>') or a program ( shell meta characters are pipe
symbol '|').
Standard Streams In I/O Redirection
The bash shell has three standard streams in I/O redirection:
standard input (stdin) : The stdin stream is numbered as stdin (0). The bash shell
takes input from stdin. By default, keyboard is used as input.
standard output (stdout) : The stdout stream is numbered as stdout (1). The bash
shell sends output to stdout. Output goes to display.
standard error (stderr) : The stderr stream is numbered as stderr (2). The bash shell
sends error message to stderr. Error message goes to display.
Types of Redirection
1. Overwrite Redirection
Overwrite redirection is useful when you want to store/save the output of a
command to a file and replace all the existing content of that file. Commands
with a single bracket '>' overwrite existing file content.
o > : standard output
o < : standard input
o 2> : standard error
Note: Writing '1>' or '>' and '0<' or '<' is same thing. But for stderr you have to
write '2>'.
STANDARD OUTPUT REDIRECTION
Syntax
(cat-is used to copy the input and give the output to the screen)
17 | P a g e
1. cat >file.txt //standard output redirection- whatever you will write after
running this command, will be redirected and copied to the “file.txt”.
2. cat <file.txt // standard input redirection-Here cat command will take the input
from “file.txt” and print it to the terminal screen
3. cat 2>file.txt //standard error
Example
$cat >sample.txt
a
b
crl-z(save and exit)
$cat sample.txt
Output
a
b
$cat >sample.txt
c
d
$cat sample.txt
c
d
Egs
$ls -al > lists
$cat lists
$who > users
$cat users
2. Append Redirection
Append redirection is used to append the output to the file without
compromising the existing data of the file. Commands with a double bracket
'>>' do not overwrite the existing file content.
• >> - standard output
• << - standard input
• 2>> - standard error
Syntax
cat >>filename
Example
cat >>sample.txt
a
b
ctrl-z
cat sample.txt
a
b
18 | P a g e
cat >>sample.txt
c
d
cat sample.txt
a
b
c
d
Example
$ls *.txt
$ls *.txt | cat >TextFiles
$cat TextFiles
RESULT
Familiarize the concept of input and output redirections on Linux.
19 | P a g e
Experiment No : 06 Date : 8-10-2024
PIPES AND FILTERS
AIM:
To familiarize with the usage of pipes and filters using Linux commands - wc, cut,
paste, comm, uniq, more, less, cmp, diff, sort, file sleep, grep.
PROCEDURE:
1. Write Linux command to create 3 files and display the contents of a file without
having to open it.
command: touch file1.txt file2.txt file3.txt
command: cat file1.txt
command: cat file2.txt
command: cat file3.txt
2. Write Linux command to count the no. of lines, words, and characters in file1.txt.
command: wc -lwm file1.txt
3. Write Linux command to count the no. of words in all files you created.
command: wc -w file1.txt file2.txt file3.txt
4. Write Linux command to list all files in a directory and count the number of lines
(files) listed.
command: ls | wc -l
5. Write Linux command to count the no. of characters in all .txt files.
command: cat *.txt | wc -c
6. Write Linux command to create a text file named state.txt with the following data
and display the first 3 characters (bytes) of each state.
Data:
Kerala
20 | P a g e
Tamilnadu
Andhra Pradesh
Arunachal Pradesh
Assam
Bihar
command: cut -b 1,2,3 state.txt
8. Write Linux command to create a .csv file named data.csv with the following data
and extract columns.
Data:
Name, Age, Place
Anil, 16, Ernakulam
Vikram, 26, Kottayam
Seetha, 30, Idukki
Veena, 25, Kollam
a) Extract the second column (age).
command: cut -d',' -f2 data.csv
b) Print name and place fields.
command: cut -d',' -f1,3 data.csv
c) Print the first two columns.
command: cut -d',' -f1-2 data.csv
21 | P a g e
9. Write Linux command to list all logged-in users and extract their usernames.
command: who | cut -d' ' -f1
10. Write Linux command to print the first column of state.txt and store it in list.txt.
command: cat state.txt | head -n 3 | cut -d ' ' -f 1 > list.txt
11. Write Linux command to print the second word of the given message.
command: echo "hello world" | cut -d' ' -f2
22 | P a g e
d) Find common lines between two files, sort them, remove duplicates, and count the
unique common lines.
command: comm -12 <(sort file1.txt) <(sort file2.txt) | sort | uniq | wc -l
16. Write Linux command to list all files in a directory and view them one screen at a
time.
command: ls -l | more
17. Write Linux command to view a large file with better navigation features.
command: cat largefile.txt | less
23 | P a g e
18. Write Linux command to sort the contents of a file and view the sorted output with
less.
command: sort unsortedfile.txt | less
19. Write Linux command to compare two files file1 and file2.
command: diff file1.txt file2.txt
21. Write Linux command to suppress output and check differences in multiple files.
command: cmp -s file1.txt file2.txt && echo "Files are identical" || echo "Files are
different"
22. Write Linux command to count the differences between files using cmp.
command: cmp -l file1.txt file2.txt | wc -l
23. Write Linux command to compare files and filter out only the lines that differ.
command: diff --side-by-side file1.txt file2.txt | grep -E "(<|>)"
24. Write Linux command to sort the file marklist in alphabetical order.
command: sort marklist.txt
25. Write Linux command to sort the file marklist in numerical and reverse order.
command: sort -n marklist.txt
command: sort -nr marklist.txt
24 | P a g e
26. Write Linux command to find the type of a file and directory.
command: file marklist.txt
command: file /home
28. Write Linux command to list files containing the pattern “scanf” in file.c.
command: grep -l "scanf" file.c
30. Write Linux command to sort unique lines from a file and search for a pattern.
command: sort -u fruit.txt | grep "a"
31. Write Linux command to count the number of lines, words, and characters in files
that contain the word "n" and then sort them.
command: grep -i "n" fruit.txt | wc -l | sort -n
32. Extract the second column from a CSV file, sort it, remove duplicates, and count
the unique entries.
command: cut -d, -f2 file.csv | sort | uniq | wc -l
33. Compare two sorted files line by line and display the lines common to both.
command: comm -12 <(sort file1.txt) <(sort file2.txt)
34. Compare two files and show the differences or confirm they are identical.
command: diff file1.txt file2.txt
25 | P a g e
command: cmp file1.txt file2.txt
36. Wait for a few seconds, then list all files in a directory that match a pattern.
command: sleep 5 && ls | grep ".sh"
37. Sort a list of words, remove duplicates, and count how many unique words exist.
command: sort fruits.txt | uniq | wc -l
38. Extract and combine specific fields from lines in a text file that match a pattern.
command: grep "a" fruit.txt | cut -d' ' -f1,3 | paste -sd " "
40. Search for a pattern in a file and view the results with pagination.
command: grep "search_term" file.txt | less
26 | P a g e
OUTPUT:
1. (no output generated as files are empty)
2. 0 0 0 file1.txt
3. 0 file1.txt
0 file2.txt
0 file3.txt
0 total
4. 3
5. 0
6. Ker
Tam
And
Aru
Ass
Bih
7a. e l h
7b. Kerala
Tamilnadu
Andhra Pradesh
Arunachal Pradesh
Assam
Bihar
7c. Keral
Tamil
Andhr
27 | P a g e
Arunac
Assam
Bihar
8a. 16
26
30
25
8b. Anil, Ernakulam
Vikram, Kottayam
Seetha, Idukki
Veena, Kollam
8c. Anil, 16
Vikram, 26
Seetha, 30
Veena, 25
10. Kerala
Tamilnadu
Andhra
11. world
28 | P a g e
1356, Ammu
12c. 1250 1251 1356
Binu Done Ammu
12d. 1250 Binu 60
1251 Done 90
1356 Ammu 80
12e. Binu,60
Done,90
Ammu,80
13a. 2
3
4
5
6
13b. 7
13c.6
13d.4
14a. 1
2
3
4
5
14b. 1
2
3
4
5
14c. 1
2
29 | P a g e
3
4
14d. 4
5
15a. 1 1
2 2
3 3
4 4
5 5
6
15b. opens at specified line
15c. 1 2 5
2 3 6
3 4 7
4 5 8
5 6 9
16. -rw-rw-r-- 1 user user 57 Sep 28 14:25 a.c
-rw-rw-r-- 1 user user 0 Aug 31 22:39 aleena.txt
-rw-rw-r-- 1 user user 68 Sep 28 13:31 data.csv
-rwxrwxr-x 1 user user 309 Oct 8 14:26 day.sh
……….
17. view large file with navigation
18. aleena
anagha
nisa
nitty
sahva
sara
shivakami
30 | P a g e
19. 1d0
<1
6c5
<
---
>6
20. *** file1.txt 2024-10-10 23:49:24.036285776 +0530
--- file2.txt 2024-10-10 23:49:36.189752640 +0530
***************
*** 1,6 ****
-1
2
3
4
5
!
1,5
2
3
4
5
!6
--- file1.txt 2024-10-10 23:49:24.036285776 +0530
+++ file2.txt 2024-10-10 23:49:36.189752640 +0530
@@ -1,6 +1,5 @@
-1
2
3
4
5
+6
21. Files are different
22. cmp: EOF on file2.txt after byte 10
5
31 | P a g e
23. 1
24. 12
23
56
78
99
25. 12
23
56
78
99
99
78
56
23
12
26. marklist.txt: ASCII text
27.99
28. file.c
29. avocado
Lemon
Mango
Melon
Orange
30. apple
avacado
banana
mango
orange
pineapple
31. 6
32. 3
32 | P a g e
33. 2
3
4
5
34. 1d0
<1
6c5
<
---
>6
file1.txt file2.txt differ: byte 1, line 1
35. file1.txt: ASCII text
36. files matching pattern after wait
day.sh
fact.sh
forloop.sh
large3.sh
multable.sh
natural.sh
37. 9
38. orange apple mango pineapple banana avocado
39. 1 5
14
13
12
11
40. orange
mango
melon
lemon
avacado
(END)
RESULT
The above commands are executed successfully and obtained desired output.
33 | P a g e
Experiment No : 07 Date : 8-10-2024
Demonstrate process management using commands - bg, fg, top, ps, pidof, nice,
renice, df, free, kill, pkill.
PROCEDURE:
1. Write command and start a long-running process (e.g., sleep 300), send it to the
background, and then bring it back to the foreground.
command: sleep 300
command: bg
command: fg
2. Write command and use the top command to identify the process consuming the
most CPU and memory. What are the PIDs of these processes?
command: top
3. Write command and list all processes running under your user account and identify
the PID of the shell you're currently using.
command: ps -u $USER
4. Write command and find the PID of the bash shell and the sshd daemon (if
running). What are the PIDs?
command: ps
command: pidof bash
command: pidof sshd
5. Write command and start a process with a lower priority using nice, then use renice
to increase its priority.
command: nice -n 10 sleep 600
command: ps -o pid,nice,command | grep sleep
34 | P a g e
command: renice -n 0 -p <PID>
command: ps -o pid,nice,command | grep sleep
6. Write command and use the df command to check disk space usage and the free
command to check memory usage.
command: df -h
command: free -h
7. Write command and start a process (e.g., sleep 600), find its PID using ps, and then
terminate it using kill.
command: sleep 600 &
command: ps
command: kill <PID>
8. Write command and start two instances of a process (e.g., sleep 600 & sleep 600
&). Use pkill to terminate both at once.
command: sleep 600 & sleep 600 &
command: pkill sleep
command: ps | grep sleep
9. Write command and Run top and observe the system's load average, memory usage,
and swap usage.
command: top
10. Write command and start a CPU-intensive task (e.g., yes > /dev/null) and use
renice to reduce its priority.
command: yes > /dev/null &
command: ps -o pid,nice,command | grep yes
command: renice 10 -p <PID>
command: top
35 | P a g e
OUTPUT
1.
[1]+ Stopped sleep 300
[1]+ sleep 300 &
2. %CPU and %MEM values displayed for each process in the top output.
3.
PID TTY TIME CMD
1528 pts/0 00:00:00 bash
1586 pts/0 00:00:00 ps
4. 1528
N/A (sshd not running)
5. PID NI COMMAND
1683 10 sleep
1683 0 sleep
6.
Filesystem Size Used Avail Use% Mounted on
devtmpfs 1.9G 0 1.9G 0% /dev
36 | P a g e
7. PID TTY TIME CMD
1683 pts/0 00:00:00 sleep
[1] + Terminated sleep 600
8.
[1] 1685
[2] 1686
9.
Load average: 0.34, 0.22, 0.14
Memory usage: 4Gi used out of 15Gi
Swap usage: 0Gi used
10.
PID NI COMMAND
1688 0 yes
1688 10 yes
RESULT
The above commands are executed successfully and obtained desired output.
37 | P a g e
Experiment No : 08 Date : 9-09-2024
BACK UP AND RECOVERY MANAGEMENT
AIM:
Demonstrate backup and recovery management using commands tar, cpio, dump
THEORY
• Backup and recovery management are essential tasks for safeguarding data. In
Linux tar,
• cpio, and dump are commonly used for creating backups and recovering data.
• tar: Best for general-purpose backups, supports compression.
• cpio: Useful for more complex file selections, often used with find.
• dump/restore: Ideal for full and incremental backups of entire file systems.
1. Using tar for Backup and Recovery
The tar command (short for "tape archive") is widely used for creating archive files,
which can
be compressed and then extracted to recover data.
Backup with tar
• Backup a directory /home/user/data into an archive file.
o Command:
$ tar -cvf backup.tar /home/user/data
Options:
-c: Create a new archive.
-v: Verbose mode, shows progress.
-f backup.tar: Specify the archive file name.
Description: This command creates a backup.tar archive containing the
contents of /home/user/data.
• Compressed Backup:
$ tar -czvf backup.tar.gz /home/user/data
Options:
-z: Compress the archive with gzip.
Recovery with tar
Scenario: Recover (extract) the data from the backup.tar archive to
/home/user/recovery.
Command:
$ tar -xvf backup.tar -C /home/user/recovery
o Options:
38 | P a g e
-x: Extract the archive.
-C /home/user/recovery: Extract files into the specified directory.
39 | P a g e
$ dump -0uf backup.dump /home
o Options:
-0: Perform a level 0 (full) backup.
-u: Update the /etc/dumpdates file after a successful dump.
-f backup.dump: Specify the output file.
o Description: This command creates a full backup of the /home filesystem in
backup.dump.
Incremental Backup with dump
enario: Perform an incremental backup (level 1) of /home.
40 | P a g e
Experiment No : 09 Date : 9-09-2024
1. Definition:
A shell is a command-line interpreter that provides a text-based interface
o
for users to interact with the operating system. o It takes
commands from the keyboard and passes them to the operating system
to perform tasks. 2. Functions of a Shell:
o Command Execution: Executes commands entered by the user. o
Scripting: Allows users to write shell scripts to automate tasks. o
Program Execution: Runs programs and commands. o
Environment Control: Manages environment variables and
configurations.
o Input/output Redirection: Redirects input and output of commands to
files or other commands.
3. Types of Shells in Linux
There are several types of shells available in Linux, each with its own features and
syntax. Here are some of the most common ones:
sh
• Usage: Although it has been largely replaced by more modern shells, sh is still
used in scripting and system scripts due to its portability.
41 | P a g e
2. Bourne-Again Shell (bash)
• Overview: bash (Bourne-Again SHell) is an enhanced version of the original
Bourne Shell. It is the default shell on most Linux distributions.
• Features:
o Command-line editing. o Command history. o Advanced
scripting capabilities (e.g., arrays, functions).
o Supports features like brace expansion, command substitution, and
more. Example:
bash
• Usage: bash is the most widely used shell in the Linux world due to its
powerful features and backward compatibility with sh.
3. C Shell (csh)
csh
• Usage: csh is less commonly used today but is still favored by some developers
who prefer its C-like syntax.
ksh
5. Z Shell (zsh)
• Overview: zsh is a highly customizable and feature-rich shell that combines the
best features of bash, ksh, and tcsh.
• Features:
o Command-line completion.
42 | P a g e
Plugin support (e.g., Oh My Zsh). o
o Advanced scripting features. o
Theme support.
o Built-in spell check and autocorrect.
• Example:
zsh
fish
• Usage: fish is favored by users looking for a more interactive and user-friendly
shell experience.
Comparison of Shells
Feature sh bash csh ksh zsh fish
Default on No Yes No No No No
Linux
Command- No Yes No Yes Yes Yes
line Editing
Command No Yes Yes Yes Yes Yes
History
Scripting Basic Advanced Moderate Advanced Advanced Moderate
Features
Customization No Yes No No Yes Yes
& Plugins
User- Basic Good Basic Good High High
friendliness
43 | P a g e
Demonstration Example
Let's switch between different shells and demonstrate their use:
BASH
$ bash # Starts the
bash shell $ echo
"Hello from bash!"
Hello from bash!
ZSH
$ zsh # Switches to
zsh shell % echo
"Hello from zsh!"
Hello from zsh!
CSH
% csh # Switches to
csh shell % echo
"Hello from csh!"
Hello from csh!
We can return to the default shell by typing exit in any of the shells:
RESULT
44 | P a g e
Experiment No : 10 Date : 8-10-2024
SHELL SCRIPTING
AIM:
To write shell scripts for performing basic arithmetic operations, decision making, looping
constructs, case structures, and function implementation to automate simple tasks.
I. Shell program to add 2 numbers
#!/bin/bash
echo -n "Enter the first number: "
read num1
echo -n "Enter the second number: "
read num2
let sum=num1+num2
echo "The sum = $sum"
II. Shell program to print whether a number is odd or even
#!/bin/bash
read -p "Enter a number: " num
let remainder=num%2
if [ $remainder -eq 0 ]; then
echo "The number is an even number"
else
echo "The number is an odd number"
fi
III. Shell program to print whether a number is positive, negative or zero
#!/bin/bash
read -p "Enter a number: " num
if [ $num -gt 0 ]; then
echo "The number is a positive number"
elif [ $num -lt 0 ]; then
45 | P a g e
echo "The number is a negative number"
else
echo "The number is zero"
fi
IV. Shell program to print the largest of 3 numbers
#!/bin/bash
read -p "Enter the first number: " num1
read -p "Enter the second number: " num2
read -p "Enter the third number: " num3
if [[ $num1 -ge $num2 ]] && [[ $num1 -ge $num3 ]]; then
echo "The largest number is $num1"
elif [[ $num2 -ge $num1 ]] && [[ $num2 -ge $num3 ]]; then
echo "The largest number is $num2"
else
echo "The largest number is $num3"
fi
V. Shell program to print the first N natural numbers
#!/bin/bash
read -p "How many natural numbers: " n
let i=1
while [ $i -le $n ]; do
echo $i
let i++
done
VI. Shell program to print the first N natural numbers in reverse order
#!/bin/bash
read -p "How many natural numbers: " n
let i=$n
46 | P a g e
until [ $i -eq 0 ]; do
echo $i
let i--
done
VII. Shell program to print the first 10 natural numbers using for loop
#!/bin/bash
echo "The first 10 natural numbers:"
for i in {1..10}; do
echo $i
done
VIII. Shell program to print the multiplication table of 8
#!/bin/bash
let j=1
for i in {8..80..8}; do
echo "$j x 8 = $i"
let j++
done
IX. Shell program to print the sum of N numbers
#!/bin/bash
read -p "How many numbers: " n
let sum=0
echo "Enter the numbers:"
for (( i=0; i<$n; i++ )); do
read x
let sum=sum+x
done
echo "The sum of the numbers is: $sum"
X. Shell program to print the day of week given a number between 1 and 7
#!/bin/bash
47 | P a g e
read -p "Enter the day of the week as a number between 1 and 7: " n
case $n in
1) echo "Monday" ;;
2) echo "Tuesday" ;;
3) echo "Wednesday" ;;
4) echo "Thursday" ;;
5) echo "Friday" ;;
6) echo "Saturday" ;;
7) echo "Sunday" ;;
*) echo "Invalid input" ;;
esac
XI. Shell program to print the factorial of a number using function
#!/bin/bash
factorial() {
let n=$1
let fact=1
for ((i=1;i<=$n;i++)); do
let fact=fact*i
done
echo "The factorial = $fact"
}
48 | P a g e
OUTPUT:
2. Enter a number: 4
The number is an even number
Enter a number: 3
The number is an odd number
3. Enter a number: -2
The number is a negative number
Enter a number: 2
The number is a positive number
Enter a number: 0
The number is zero
4. Enter the first number: 7
Enter the second number: 12
Enter the third number: 3
The largest number is 12
5. How many natural numbers: 5
1
2
3
4
5
6. How many natural numbers: 5
5
4
3
2
1
49 | P a g e
7. The first 10 natural numbers:
1
2
3
4
5
6
7
8
9
10
8. 1x8=8
2 x 8 = 16
3 x 8 = 24
4 x 8 = 32
5 x 8 = 40
6 x 8 = 48
7 x 8 = 56
8 x 8 = 64
9 x 8 = 72
10 x 8 = 80
9. How many numbers: 3
Enter the numbers:
2
3
5
The sum of the numbers is: 10
10. Enter the day of the week as a number between 1 and 7: 4
Thursday
50 | P a g e