OS Practical Notes
OS Practical Notes
bash
CopyEdit
#!/bin/bash
echo "Hello, Shell Scripting"
�Variables
bash
CopyEdit
name="John"
echo "Hello $name"
Special Variables:
o $$ – Process ID
o $0 – Script name
o $1...$9 – Positional parameters
o $# – Number of arguments
o $? – Exit status
o $! – PID of last background process
�Arrays
bash
CopyEdit
arr=(one two three)
echo ${arr[1]} # Output: two
�Operators
�Conditionals
bash
CopyEdit
if [ $a -gt $b ]
then
echo "A is greater"
else
echo "B is greater"
fi
bash
CopyEdit
case $var in
1) echo "One" ;;
2) echo "Two" ;;
*) echo "Default" ;;
esac
�Loops
bash
CopyEdit
# while loop
i=1
while [ $i -le 5 ]
do
echo $i
i=$((i+1))
done
bash
CopyEdit
# for loop
for i in 1 2 3
do
echo $i
done
�Functions
bash
CopyEdit
function greet() {
echo "Hello, $1"
}
greet "Alice"
int main() {
pid_t pid = fork();
if (pid == 0) {
printf("Child process, PID = %d\n", getpid());
execlp("/bin/ls", "ls", NULL); // Executes 'ls'
} else {
printf("Parent process, PID = %d\n", getpid());
}
return 0;
}
�mkdir(), rmdir()
c
CopyEdit
#include <sys/stat.h>
#include <unistd.h>
int main() {
mkdir("newdir", 0777);
rmdir("newdir");
return 0;
}