LabManual
LabManual
#!/bin/bash
flag_val=0;
done
2. Write a shell script to reverse a given number and check whether it is a
palindrome.
#!/bin/bash
copy_val=$num
rev_val=0
while((copy_val>0)); do
#echo "$copy_val"
mod_val=$((copy_val%10))
rev_val=$((rev_val*10 + mod_val));
copy_val=$((copy_val/10))
done
#!/bin/bash
sum_of_digits(){
local n=$1
if (( n == 0 )); then
echo 0
else
local digit=$(( n % 10))
local rest=$(( n/10 ))
local rec_sum=$(sum_of_digits $rest)
echo $(( digit + rec_sum ))
fi
}
copy_val=num
sum=0
#!/bin/bash
read choice
case $choice in
1) ls -la;;
2) pwd;;
3) uname -a;;
4) whoami;;
5) date;;
6) ps aux;;
7) uptime;;
8) ifconfig;;
9) df -h;;
10) who;;
*)echo "Invalid choice"
Esac
5. Write a shell script that displays a list of all the files in the current
directory to which the user has read, write and execute permissions.
#!/bin/bash
#!/bin/bash
cp "$src" "$des"
7. Write a shell script to copy a file between two directories.
#!/bin/bash
mkdir "$dir_name"
cp "$src" ./"$dir_name/$des"
8. Write a shell script to create two data files and compare them to display
unique and common entries.
#!/bin/bash
echo -e "\n\n\n"
#!/bin/bash
#!/bin/bash
echo "$new_string"
11. Write a shell script to accept a word and perform pattern matching in a
given file.
#!/bin/bash
#!/bin/bash
factorial(){
if [[ $1 -le 1 ]]; then
echo 1
else
local prev=$(factorial $(( $1 - 1 )))
echo $(( $1 * prev ))
fi
result=$(factorial $num)
#!/bin/bash
show_menu(){
echo -e "\n------------------------\n"
echo "1. Create zombie process"
echo "2. Create orphan process"
echo "3. exit"
echo -e "\n------------------------\n"
}
create_zombie(){
echo -e "\ncreating zombie process\n"
{
echo "child (PID $$) running"
exit 0
}&
child_pid=$!
echo "Parent (PID $$) sleeping for 10 seconds without waiting for child"
sleep 10
create_orphan(){
echo -e "\n creating orphan process\n"
{
sleep 5
echo "orphan child (PID $$) running with new parent (PID: $(ps -o
ppid= -p $$))"
}&
while true; do
show_menu
echo "Enter choice"
read choice
case $choice in
1) create_zombie ;;
2) create_orphan ;;
3) echo "Exiting .."; break ;;
*) echo "Invalid choice" ;;
esac
done