OS Lab

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Some Simple Working Commands:

present working directory- pwd


make directory- mkdir
change directory- cd
create file- cat>filename
read file- cat filename
copy file- cp file1 file2
append file- cat file>>file
move- mv file1 file2
over write- cat file>file
symbolic link- ln -s dir1/dir2/dir3/file
rename- cat file1>newname
process status- ps
kill process- ps psid
pipeline – cat file2>file3 | file3>file4

Code of Some Problem from Shovon Vai Website:

Calculator:

echo "===========================================";
echo " Calculator";
echo "===========================================";
printf "Enter a number: ";
read num1; #scanning number1

printf "Enter another number: ";


read num2; #scanning number2

#stroing sum of two numbers


sum=$(echo "$num1+$num2" | bc -l);

#stroing substraction of two numbers


sub1=$(echo "$num1-$num2" | bc -l);
sub2=$(echo "$num2-$num1" | bc -l);

#stroing multiplication of two numbers


mul=$(echo "$num1*$num2" | bc -l);

#stroing division of two numbers


div1=$(echo "$num1/$num2" | bc -l);
div2=$(echo "$num2/$num1" | bc -l);

echo ""; #printing blank line


#selecting operation
echo "Select any of the following operation:";
echo "1. "$num1 "+" $num2;
echo "2. "$num1 "-" $num2;
echo "3. "$num2 "-" $num1;
echo "4. "$num1 "x" $num2;
echo "5. "$num1 "/" $num2;
echo "6. "$num2 "/" $num1;
echo "7. All of the above";
echo "8. Exit";

for((i=0;;i++))
do
echo ""; #printing blank line
printf "Enter choice(any integer from 1-8): ";
read choice;

case $choice in
1)
echo $num1 "+" $num2 "=" $sum;
;;
2)
echo $num1 "-" $num2 "=" $sub1;
;;
3)
echo $num2 "-" $num1 "=" $sub2;
;;
4)
echo $num1 "x" $num2 "=" $mul;
;;
5)
echo $num1 "/" $num2 "=" $div1;
;;
6)
echo $num2 "/" $num1 "=" $div2;
;;
7)
#printing all results
echo $num1 "+" $num2 "=" $sum;
echo $num1 "-" $num2 "=" $sub1;
echo $num2 "-" $num1 "=" $sub2;
echo $num1 "x" $num2 "=" $mul;
echo $num1 "/" $num2 "=" $div1;
echo $num2 "/" $num1 "=" $div2;
;;
8)
#exit the program
break;
;;
*)
echo "Invalid Choice";
;;
esac;
done
Check Argument:

printf "Enter command line argument: ";


read cmd; #scanning command line arg
echo "" #printing blank line

#checking if its a file


if [ -f "$cmd" ];
then
echo $cmd "is a valid file.";

#checking if its a directory


elif [ -d "$cmd" ];
then
echo $cmd "is a directory";

#otherwise ifs something else


else
echo $cmd "is neither a file nor a directory";
fi

echo "" #printing blank line


echo "" #printing blank line

Copy File:

echo "" #printing blank line


echo " Multiple File Copier";
echo " ====================";

printf "Enter Destination Directory: ";


read dest; #scanning destination directory

#checking if the destination is available


if [ -d "$dest" ];
then
printf "Enter Filename";
printf "(if multiple keep space between names): ";
read file; #scanning filenames
echo "" #printing blank line
echo "" #printing blank line

#counting files in the destination before start copy


oldcnt=$(find $dest -type f | wc -l);
#copy command
cp=`cp $file $dest`;
#listing command
ls=`ls $dest`;
#counting files in the destination after copy
newcnt=$(find $dest -type f | wc -l);
#counting successful copied number of files
number_of_copied_file=$(($newcnt-$oldcnt));

#if copied file > 0


if [ $number_of_copied_file -ne 0 ];
then
echo $number_of_copied_file "Files Copied Successfully";
echo "Now Direcotry" $dest "Contains:";
echo $ls;
#if copied file = 0
else
echo "No New File Is Copied To" $dest ". Check The Filename Again.";
fi

#if directory not found


else
echo "No Such Directory Found";
fi

echo "" #printing blank line


echo "" #printing blank line

File Listing:

echo "" #printing blank line


echo "" #printing blank line
echo "Listing files";
echo "=============";

echo "" #printing blank line


printf "Enter directory absolute path"
printf "(pwd for current directory): ";
read dirname; #scanning dirname from user
pwd=`pwd`; #extracting current directory

#if user enter pwd then


#change dirname to
#the current directory path

if [ "$dirname" == "pwd" ];
then
dirname=$pwd;
fi
echo "Entered directory: "$dirname;
ls1="ls -1 $dirname"; #creating a string
ls=`$ls1`; #command line argument

lsarray=( $ls ); #creating array from the output string


len=${#lsarray[@]}; #getting array length

if [ $len -ne 0 ]; #show the files if length is not 0


then
echo "Total files in "$dirname "directory = "$len;
echo "The files are:"; #showing the files
for((i=0;i<$len;i++))
do
echo ${lsarray[$i]};
done
fi
echo "" #printing blank line

Power Calculation:

echo "===========================================";
echo " p^q Generator";
echo "===========================================";

#function to calculate the power


pow()
{
tmpp=$1;
tmpq=$2;
res=1;
for((i=1;i<=$tmpq;i++))
do
res=$(($res*$tmpp));
done
return $res; #returning the result
}

printf "Enter p: ";


read p; #scanning p

printf "Enter q: ";


read q; #scanning q

pow $p $q; #calling the function for p^q


result1=$?; #store the returned value of p^q

pow $q $p; #calling the function for q^p


result2=$? #storing the returned value of q^p
echo ""; #printing blank line
echo "Select any of the following operation:";
echo "1. "$p "^" $q;
echo "2. "$q "^" $p;
echo "3. All the above";
echo "4. Exit";

echo ""; #printing blank line


for((i=0;;i++))
do

printf "Enter choice: ";


read choice;

case $choice in
1)
echo $p "^" $q "=" $result1; #printing p^q
echo ""; #printing blank line
;;
2)
echo $q "^" $p "=" $result2; #printing q^p
echo ""; #printing blank line
;;
3)
echo $p "^" $q "=" $result1; #printing p^q
echo $q "^" $p "=" $result2; #printing q^p
echo ""; #printing blank line
;;
4)
break;
;;
esac

done

Reverse Matrix:

printf "Enter number of rows of a matrix: ";


read number_of_row; #scanning number of rows

printf "Enter number of columns of a matrix: ";


read number_of_column; #scanning number of columns

echo "Enter a ("$number_of_row "x" $number_of_column") matrix: ";

total_variable=$(($number_of_row*$number_of_column));

for((i=1;i<=$number_of_row;i++))
do
for((j=1;j<=$number_of_column;j++))
do
printf "Enter Matrix ["$i"]["$j"]: ";
pos="$i$j";
read matrix[$pos];
done
done

echo "";
echo "The reverse matrix:";
for((i=1;i<=$number_of_column;i++))
do
for((j=1;j<=$number_of_row;j++))
do
pos="$j$i";
printf " %9s" ${matrix[$pos]};
done
echo "";
done

echo "";
echo "The original matrix:";
for((i=1;i<=$number_of_row;i++))
do
for((j=1;j<=$number_of_column;j++))
do
pos="$i$j";
printf " %9s" ${matrix[$pos]};
done
echo "";
done

Salary:

echo "=====================================";
echo " Salary Calculator";
echo "=====================================";

for((i=0;;i++))
do
printf "Enter basic salary(0 to exit): ";
read basic;
if [ $basic == 0 ];
then
break;
else
a=.1;
b=.9;
c=.98;

if [ $basic -le 1500 ];


then
hra=$(echo "$basic * $a" | bc -l);
da=$(echo "$basic * $b" | bc -l);
else
hra=500;
da=$(echo "$basic * $c" | bc -l);
fi

total=$(echo "$basic + $hra + $da" | bc -l);


echo "";
echo "Calculated Gross Salary";
echo "=======================";
echo "Basic Salary = "$basic;
echo "+ HRA = "$hra;
echo "+ DA = "$da;
echo "=======================";
echo " Total = "$total;
echo "";
fi
done

Upper Case:

echo ""; #printing blank line


echo " UPPER CASE CONVERTER";
echo " ====================";

printf "Enter Filename";


printf "(if multiple keep a space): ";
read filename; #scanning filename
filearray=($filename);
len=${#filearray[@]};

echo ""; #printing blank line


echo ${filearray[@]} "are converted successfully";
echo "We are going to read each file in console to test it.";
echo ""; #printing blank line
for((i=0;i<$len;i++))
do
tmpfile=${filearray[$i]};
filetext=`tr ['a-z'] ['A-Z'] < $tmpfile`;
echo "$filetext" > $tmpfile;
echo "Filename:" $tmpfile ":";
echo "======================================================";
cat $tmpfile;
echo "======================================================";
echo ""; #printing blank line
done
Upper Case:

printf "Enter filename: ";


read filename; #scanning filename
echo "" #printing blank line

wc=`wc $filename`; #command sustitute


wcarray=($wc); #creating array with output string
len=${#wcarray[@]}; #getting array length

if [ $len -ne 0 ]; #showing result if len>0


then
echo "" #printing blank line
echo "Details of "$filename;
echo "=====================";
echo "" #printing blank line

#printing number of lines in the file


echo "Total lines = "${wcarray[0]};
#printing number of words in the file
echo "Total words = "${wcarray[1]};
#printing number of characters in the file
echo "Total characters = "${wcarray[2]};

echo "" #printing blank line


echo "" #printing blank line
fi

You might also like