Winter Semester 2026–27 · VIT-AP · BACSE106 · Operating Systems · Dr. Krithika L.B

Lab 1: Study of basic Linux commands and shell scripting

Theory link: 1.1 OS basics · 1.3 Services & system calls

Experiment 1 of 15 Module 1 2 hours

Video walkthrough

Lab 1 walkthrough — Study of basic Linux commands and shell scripting

Video coming soon

Lab 1 walkthrough — Study of basic Linux commands and shell scripting

Recorded by Dr. Krithika L.B. Slot lab01-walkthrough

Aim

Aim. To use basic Linux commands for files, directories, permissions, processes and system information, and to write simple shell scripts using variables, input, arithmetic, conditions, loops and case.

After this lab you can: navigate and manage the file system from the shell; inspect processes and the kernel; combine commands with pipes and redirection; write, run and debug small bash scripts.

Requirements: any Linux system (lab machine, Ubuntu in VirtualBox, or WSL on Windows) with bash.

Explanation

In Session 1.3 we saw that the command interpreter is a system program that reads a command, finds the program for it and runs it. On Linux that interpreter is a shell, usually bash. When you type ls -l, the shell creates a new process for /bin/ls, and ls in turn uses system calls such as openat(), getdents64(), stat() and write() to do its work. The shell is therefore your window onto the OS services: files, processes, devices and protection.

Three ideas that make the shell powerful
  • Redirection — > sends a command's output to a file (overwrite), >> appends, < reads input from a file.
  • Pipes — cmd1 | cmd2 connects the output of one process to the input of the next (a pipe() system call underneath).
  • Scripts — a text file of commands, with variables, if, loops and case, run by the shell.

Part A — Basic commands

CategoryCommandWhat it does
Directoriespwdprint the current (working) directory
ls -l, ls -alist files (long format / including hidden files)
cd dir, cd .., cd ~change directory (into dir / up one level / home)
mkdir dir, mkdir -p a/b/ccreate a directory (and parents)
rmdir dirremove an empty directory
Filestouch fcreate an empty file / update its time stamp
cat f, more f, less fdisplay file contents
cp src dst, mv src dstcopy / move or rename
rm f, rm -r dirdelete a file / a directory tree (no recycle bin!)
head -n 5 f, tail -n 5 ffirst / last lines of a file
wc fcount lines, words and characters
Permissionschmod 755 f, chmod u+x fchange read/write/execute permissions
chown user fchange owner (needs sudo)
Search & filtergrep word fprint lines containing word
find . -name "*.c"search for files by name
sort f, uniqsort lines / remove adjacent duplicates
cmd > f, cmd >> f, cmd1 | cmd2redirect output / append / pipe
Processesps, ps -eflist your / all processes
toplive view of processes and CPU use (q to quit)
sleep 100 &, jobsrun in background / list background jobs
kill PIDsend a terminate signal to a process
System infouname -a, uname -rkernel name and version (Session 1.2)
whoami, whocurrent user / logged-in users
date, caldate and time / calendar
df -h, du -sh ., free -hdisk space / directory size / memory
man lsmanual page for any command

Part B — Shell-script building blocks

Variables & input

name=Ravi (no spaces around =) · echo $name · read x reads from the keyboard · $1, $2 are command-line arguments, $# their count.

Arithmetic

$((a + b)), $((a * b)), $((a / b)) (integer), $((a % b)).

Conditions

if [ $a -gt $b ]; then … elif …; else …; fi · numbers: -eq -ne -lt -le -gt -ge · files: -e -f -d -r -w -x.

Loops & case

while [ cond ]; do … done · for i in 1 2 3; do … done · case $x in 1) … ;; *) … ;; esac.

Lab 1 uses shell, not C

This is the only experiment written in shell script, because shell scripting is the topic. From Lab 2 onward every program is in C.

Code

Part A — practice session

Type these one at a time and observe each result.

commands.txt — Part A practice sequence
pwd
mkdir -p oslab/lab1
cd oslab/lab1
echo "operating systems" > notes.txt
echo "linux kernel" >> notes.txt
echo "shell scripting" >> notes.txt
cat notes.txt
wc notes.txt
grep kernel notes.txt
sort notes.txt
cp notes.txt backup.txt
ls -l
chmod 600 backup.txt
ls -l backup.txt
ps
uname -r
df -h .

Part B — scripts

Program 1 · Arithmetic on two numbers

arith.sh
#!/bin/bash
# arith.sh - read two numbers and print basic arithmetic results
echo -n "Enter first number: "
read a
echo -n "Enter second number: "
read b
echo "Sum        = $((a + b))"
echo "Difference = $((a - b))"
echo "Product    = $((a * b))"
if [ "$b" -ne 0 ]; then
    echo "Quotient   = $((a / b))"
    echo "Remainder  = $((a % b))"
else
    echo "Division by zero is not allowed"
fi

Program 2 · Largest of three numbers

largest.sh
#!/bin/bash
# largest.sh - find the largest of three numbers
echo -n "Enter three numbers: "
read a b c
if [ "$a" -ge "$b" ] && [ "$a" -ge "$c" ]; then
    largest=$a
elif [ "$b" -ge "$c" ]; then
    largest=$b
else
    largest=$c
fi
echo "Largest number is $largest"

Program 3 · Factorial using a while loop

factorial.sh
#!/bin/bash
# factorial.sh - factorial of a number using a while loop
echo -n "Enter a number: "
read n
fact=1
i=1
while [ $i -le $n ]
do
    fact=$((fact * i))
    i=$((i + 1))
done
echo "Factorial of $n is $fact"

Program 4 · File information from a command-line argument

fileinfo.sh
#!/bin/bash
# fileinfo.sh - report on a file name given as a command-line argument
if [ $# -ne 1 ]; then
    echo "Usage: $0 <filename>"
    exit 1
fi
f=$1
if [ ! -e "$f" ]; then
    echo "$f does not exist"
elif [ -d "$f" ]; then
    echo "$f is a directory containing $(ls "$f" | wc -l) entries"
else
    echo "$f is a regular file"
    echo "Lines: $(wc -l < "$f")  Words: $(wc -w < "$f")  Characters: $(wc -c < "$f")"
    [ -r "$f" ] && echo "Readable: yes" || echo "Readable: no"
    [ -w "$f" ] && echo "Writable: yes" || echo "Writable: no"
    [ -x "$f" ] && echo "Executable: yes" || echo "Executable: no"
fi

Program 5 · Menu-driven system information using case

menu.sh
#!/bin/bash
# menu.sh - menu-driven system information using case
while true
do
    echo "----- MENU -----"
    echo "1. Today's date"
    echo "2. Logged-in user"
    echo "3. Current directory listing"
    echo "4. Disk usage of current directory"
    echo "5. Exit"
    echo -n "Enter your choice: "
    read ch
    case $ch in
        1) date ;;
        2) whoami ;;
        3) ls ;;
        4) du -sh . ;;
        5) echo "Bye"; exit 0 ;;
        *) echo "Invalid choice" ;;
    esac
done

How to run

  1. Open a terminal (Ctrl+Alt+T on Ubuntu, or start WSL on Windows).
  2. Create the script with an editor: nano arith.sh, paste the code, save with Ctrl+O, Enter, exit with Ctrl+X.
  3. Make it executable (a chmod() system call — Session 1.3): chmod +x arith.sh
  4. Run it: ./arith.sh (or without execute permission: bash arith.sh). For Program 4 pass the file name: ./fileinfo.sh notes.txt.
  5. Debug if needed: bash -x arith.sh prints every line as it executes.
Terminal
$ nano arith.sh
$ chmod +x arith.sh largest.sh factorial.sh fileinfo.sh menu.sh
$ ./arith.sh
Common mistakes
  • a = 5 fails — no spaces around = in assignments.
  • if [$a -gt $b] fails — spaces are required inside [ ].
  • Permission denied when running ./x.sh — run chmod +x x.sh first.
  • Script saved on Windows shows $'\r': command not found — convert with dos2unix x.sh.

Sample input & output

Part A — sample session (abridged)

Input
commands typed as shown in the practice sequence
Output
Output
$ cat notes.txt
operating systems
linux kernel
shell scripting
$ wc notes.txt
 3  6 47 notes.txt
$ grep kernel notes.txt
linux kernel
$ sort notes.txt
linux kernel
operating systems
shell scripting
$ ls -l backup.txt
-rw------- 1 student student 47 Jan  6 10:15 backup.txt
$ uname -r
6.5.0-35-generic

Program 1 · arith.sh

Input
17 and 5
Output
Output
$ ./arith.sh
Enter first number: 17
Enter second number: 5
Sum        = 22
Difference = 12
Product    = 85
Quotient   = 3
Remainder  = 2

Program 1 · arith.sh (divide by zero)

Input
8 and 0
Output
Output
$ ./arith.sh
Enter first number: 8
Enter second number: 0
Sum        = 8
Difference = 8
Product    = 0
Division by zero is not allowed

Program 2 · largest.sh

Input
12 45 7
Output
Output
$ ./largest.sh
Enter three numbers: 12 45 7
Largest number is 45

Program 3 · factorial.sh

Input
5
Output
Output
$ ./factorial.sh
Enter a number: 5
Factorial of 5 is 120

Program 4 · fileinfo.sh

Input
file name notes.txt, then a missing file
Output
Output
$ ./fileinfo.sh notes.txt
notes.txt is a regular file
Lines: 3  Words: 6  Characters: 47
Readable: yes
Writable: yes
Executable: no
$ ./fileinfo.sh nofile.txt
nofile.txt does not exist

Program 5 · menu.sh

Input
choices 2, 9, 5
Output
Output
$ ./menu.sh
----- MENU -----
1. Today's date
2. Logged-in user
3. Current directory listing
4. Disk usage of current directory
5. Exit
Enter your choice: 2
student
----- MENU -----
...
Enter your choice: 9
Invalid choice
----- MENU -----
...
Enter your choice: 5
Bye

Try it yourself

  1. Modify largest.sh to find the smallest of three numbers.
  2. Write a script that prints the multiplication table of a number entered by the user (use a for or while loop).
  3. Write a script that checks whether a number is a palindrome (e.g. 121).
  4. Write a script that counts how many .c files are in the current directory (hint: ls *.c | wc -l).
  5. Run strace -c ls and list the three system calls that ls makes most often.

Viva questions

Prepare these for the lab evaluation.

Revealed 0
What is a shell? Name two shells.

A command interpreter — a user-level system program that reads commands and runs them using system calls. Examples: bash, sh, zsh, csh.

What is the difference between > and >>?

> creates or overwrites the file; >> appends to it.

What does chmod 755 file mean?

Owner: rwx (7); group: r-x (5); others: r-x (5).

What do $#, $0 and $1 hold in a script?

Number of arguments; the script name; the first argument.

What happens internally when you type ls | wc -l?

The shell creates a pipe, then two child processes; ls writes into the pipe and wc reads from it — process creation (Lab 3) and IPC (Module 3) in one line.