Lab 1 walkthrough — Study of basic Linux commands and shell scripting
Lab 1 walkthrough — Study of basic Linux commands and shell scripting
Recorded by Dr. Krithika L.B. Slot lab01-walkthrough
Theory link: 1.1 OS basics · 1.3 Services & system calls
Lab 1 walkthrough — Study of basic Linux commands and shell scripting
Recorded by Dr. Krithika L.B. Slot lab01-walkthrough
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.
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.
> sends a command's output to a file (overwrite), >> appends, < reads input from a file.cmd1 | cmd2 connects the output of one process to the input of the next (a pipe() system call underneath).if, loops and case, run by the shell.| Category | Command | What it does |
|---|---|---|
| Directories | pwd | print the current (working) directory |
ls -l, ls -a | list files (long format / including hidden files) | |
cd dir, cd .., cd ~ | change directory (into dir / up one level / home) | |
mkdir dir, mkdir -p a/b/c | create a directory (and parents) | |
rmdir dir | remove an empty directory | |
| Files | touch f | create an empty file / update its time stamp |
cat f, more f, less f | display file contents | |
cp src dst, mv src dst | copy / move or rename | |
rm f, rm -r dir | delete a file / a directory tree (no recycle bin!) | |
head -n 5 f, tail -n 5 f | first / last lines of a file | |
wc f | count lines, words and characters | |
| Permissions | chmod 755 f, chmod u+x f | change read/write/execute permissions |
chown user f | change owner (needs sudo) | |
| Search & filter | grep word f | print lines containing word |
find . -name "*.c" | search for files by name | |
sort f, uniq | sort lines / remove adjacent duplicates | |
cmd > f, cmd >> f, cmd1 | cmd2 | redirect output / append / pipe | |
| Processes | ps, ps -ef | list your / all processes |
top | live view of processes and CPU use (q to quit) | |
sleep 100 &, jobs | run in background / list background jobs | |
kill PID | send a terminate signal to a process | |
| System info | uname -a, uname -r | kernel name and version (Session 1.2) |
whoami, who | current user / logged-in users | |
date, cal | date and time / calendar | |
df -h, du -sh ., free -h | disk space / directory size / memory | |
man ls | manual page for any command |
name=Ravi (no spaces around =) · echo $name · read x reads from the keyboard · $1, $2 are command-line arguments, $# their count.
$((a + b)), $((a * b)), $((a / b)) (integer), $((a % b)).
if [ $a -gt $b ]; then … elif …; else …; fi · numbers: -eq -ne -lt -le -gt -ge · files: -e -f -d -r -w -x.
while [ cond ]; do … done · for i in 1 2 3; do … done · case $x in 1) … ;; *) … ;; esac.
This is the only experiment written in shell script, because shell scripting is the topic. From Lab 2 onward every program is in C.
Type these one at a time and observe each result.
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 .#!/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#!/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"#!/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"#!/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#!/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
donenano arith.sh, paste the code, save with Ctrl+O, Enter, exit with Ctrl+X.chmod() system call — Session 1.3): chmod +x arith.sh./arith.sh (or without execute permission: bash arith.sh). For Program 4 pass the file name: ./fileinfo.sh notes.txt.bash -x arith.sh prints every line as it executes.$ nano arith.sh
$ chmod +x arith.sh largest.sh factorial.sh fileinfo.sh menu.sh
$ ./arith.sha = 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.$'\r': command not found — convert with dos2unix x.sh.$ 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$ ./arith.sh
Enter first number: 17
Enter second number: 5
Sum = 22
Difference = 12
Product = 85
Quotient = 3
Remainder = 2$ ./arith.sh
Enter first number: 8
Enter second number: 0
Sum = 8
Difference = 8
Product = 0
Division by zero is not allowed$ ./largest.sh
Enter three numbers: 12 45 7
Largest number is 45$ ./factorial.sh
Enter a number: 5
Factorial of 5 is 120$ ./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$ ./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
Byelargest.sh to find the smallest of three numbers.for or while loop)..c files are in the current directory (hint: ls *.c | wc -l).strace -c ls and list the three system calls that ls makes most often.Prepare these for the lab evaluation.
Revealed 0A command interpreter — a user-level system program that reads commands and runs them using system calls. Examples: bash, sh, zsh, csh.
> and >>?> creates or overwrites the file; >> appends to it.
chmod 755 file mean?Owner: rwx (7); group: r-x (5); others: r-x (5).
$#, $0 and $1 hold in a script?Number of arguments; the script name; the first argument.
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.