Experiment 1: Basic Unix Commands
$ pwd
/home/indranil
$ ls -a
. .. .bashrc .profile notes.txt script.sh
$ mkdir testdir && rmdir testdir
$ cp file1.txt file2.txt
$ cat file1.txt
Hello World!
This is Unix Lab.
$ touch newfile.txt
$ mv newfile.txt renamed.txt
$ rm renamed.txt
$ whoami
indranil
$ date
Mon Apr 21 18:05:43 IST 2025
Experiment 2: Basic Network Commands
$ ifconfig
eth0: inet 192.168.0.105 netmask 255.255.255.0
$ ping -c 2 google.com
64 bytes from google.com: icmp_seq=1 ttl=115 time=10 ms
$ hostname
indranil-VirtualBox
$ netstat -tuln
Proto Local Address State
tcp 0.0.0.0:22 LISTEN
$ traceroute google.com
1 192.168.0.1 ...
2 172.217.194.100 ...
$ curl ifconfig.me
103.25.231.80
$ dig google.com
;; ANSWER SECTION:
google.com. 300 IN A 142.250.192.14
$ nslookup yahoo.com
Name: yahoo.com
Address: 98.137.11.163
$ wget http://example.com
$ arp -a
gateway (192.168.0.1) at ab:cd:ef:12:34:56 on eth0
Experiment 3: Unix Editor
$ vi sample.txt
i -> Insert mode
Type: This is a test file.
ESC -> :wq -> Save and quit
$ dd
# Deletes current line
$ yy then p
# Copies and pastes line
$ :%s/old/new/g
# Replaces all 'old' with 'new'
$ :5
# Goes to line 5
$ :set nu
# Shows line numbers
$ :set nonu
# Hides line numbers
$ :w filename.txt
# Save file as filename.txt
$ :q!
# Quit without saving
$ :help
# Opens help screen
Experiment 4: Process Management
$ ps
PID TTY TIME CMD
1234 pts/0 00:00:00 bash
2345 pts/0 00:00:01 vi
$ kill 2345
$ gedit &
[1] 3456
$ pstree
systemd─┬─bash───top
└─NetworkManager
$ top
# Interactive real-time process viewer
$ jobs
[1]+ Running gedit &
$ fg %1
# Brings job 1 to foreground
$ bg %1
# Sends job 1 to background
$ nohup ./longjob.sh &
# Runs job immune to hangups
$ nice -n 10 myprocess
# Run with modified priority
Experiment 5: Basic Shell Programming
$ echo "Hello World"
Hello World
$ read -p "Enter a: " a; read -p "Enter b: " b; echo "Sum = $((a + b))"
Enter a: 5
Enter b: 7
Sum = 12
$ if [ -e "myfile.txt" ]; then echo "Exists"; else echo "Not found"; fi
Exists
$ for i in {1..5}; do echo "Number: $i"; done
Number: 1 ... Number: 5
$ echo $((3 + 7))
10
$ while [ $count -le 3 ]; do echo "Count: $count"; ((count++)); done
Count: 1 ... Count: 3
$ until [ $a -gt 5 ]; do echo $a; ((a++)); done
$ case $var in 1) echo "One";; 2) echo "Two";; *) echo "Other";; esac
$ arr=(1 2 3 4 5); for i in "${arr[@]}"; do echo $i; done
$ echo "Enter name:"; read name; echo "Hi $name"
Experiment 6: Advanced Shell Programming
$ myfunc() { echo "Inside function"; }; myfunc
Inside function
$ echo -e "1\n2\n3" | while read line; do echo $line; done
1
2
3
$ trap "echo Ctrl+C Pressed!" SIGINT
# Now press Ctrl+C to see the message
$ export VAR="Hello"; bash -c 'echo $VAR'
Hello
$ [[ 5 -gt 3 ]] && echo "Yes" || echo "No"
Yes
$ IFS=":" read -r name id <<< "Alice:100"
$ echo $name $id
Alice 100
$ for file in *.txt; do echo $file; done
file1.txt
notes.txt
$ cat << EOF
> This is a
> multiline string
> EOF
This is a
multiline string
$ ((sum=2+3)); echo $sum
5
$ test -d /home && echo "Directory exists"
Directory exists
Experiment 7: Filter Commands (grep, sed, awk)
$ echo -e "apple\nbanana\napricot" | grep "ap"
apple
apricot
$ cat file.txt | sed 's/apple/orange/g'
$ echo -e "1\n2\n3\n4\n5" | awk '{sum += $1} END {print sum}'
15
$ grep -i "hello" myfile.txt
$ sed -n '2,4p' data.txt
# Prints lines 2 to 4
$ awk '{print $2}' scores.txt
# Prints second column
$ grep -v "error" logfile.log
# Excludes lines with "error"
$ sed '2d' file.txt
# Deletes second line
$ awk '$1 > 50 {print $1}' marks.txt
# Prints lines where first column > 50
$ grep -c "entry" access.log
# Count matching lines
Experiment 8: Looping and Control Statements in Shell
$ for i in {1..3}; do echo "Loop $i"; done
Loop 1
Loop 2
Loop 3
$ while [ $n -lt 3 ]; do echo $n; ((n++)); done
0
1
2
$ until [ $a -ge 3 ]; do echo $a; ((a++)); done
0
1
2
$ for name in John Mike Ana; do echo "Hello $name"; done
Hello John
Hello Mike
Hello Ana
$ for ((i=0; i<5; i++)); do echo $i; done
0
1
2
3
4
$ count=1; while true; do echo $count; ((count++)); [ $count -gt 3 ] && break; done
$ for file in *.sh; do chmod +x "$file"; done
$ x=5; if [ $x -eq 5 ]; then echo "Equal"; fi
Equal
$ read -p "Enter age: " age; if [ $age -ge 18 ]; then echo "Adult"; fi
Adult
$ echo "Select an option: 1|2"; read opt; case $opt in 1) echo "One";; 2) echo "Two";; esac
Experiment 9: Use of sed and awk
$ sed 's/unix/linux/g' sample.txt
# Replace "unix" with "linux"
$ sed -n '/start/,/end/p' notes.txt
# Print lines between 'start' and 'end'
$ awk '{print $1, $NF}' students.txt
# Print first and last field
$ awk -F ":" '{print $1}' /etc/passwd
# Print usernames
$ sed -e '1d' -e '$d' data.txt
# Delete first and last line
$ awk 'NR%2==0' file.txt
# Print even-numbered lines
$ sed '/^$/d' myfile.txt
# Remove empty lines
$ awk 'BEGIN{sum=0}{sum+=$1}END{print sum}' numbers.txt
# Sum first column
$ sed 's/[0-9]//g' data.txt
# Remove all digits
$ awk '{if($3>40) print $0;}' marks.txt
# Print rows with 3rd column > 40
Experiment 10: Perl Scripting Basics
$ perl -e 'print "Hello Perl\n";'
Hello Perl
$ perl -e '$a=5;$b=10;$c=$a+$b;print "$c\n";'
15
$ perl -e 'foreach(1..5){print "$_\n";}'
1
2
3
4
5
$ perl -e 'if(3 > 2){print "True\n";}'
True
$ perl -e '$str="hello";print uc($str);'
HELLO
$ perl -e '@arr=(a..d); print "@arr\n";'
abcd
$ perl -e '%h = ("name", "Indranil", "id", 101); print "$h{name}\n";'
Indranil
$ perl -e 'open(F,"<file.txt"); while(<F>){print $_;} close(F);'
$ perl -e 'sub greet{print "Hi $_[0]\n";} greet("World");'
Hi World
$ perl -e '$x=0; while($x<3){print "$x\n"; $x++;}'
0
1
2