osLab
osLab
Write a shell Script to assign a file permission to the given file using Symbolic
Mode/Absolute Mode.
AIM: Assign file permissions to a given file using Symbolic Mode or Absolute Mode.
ALGORITHM:
1.Accept the file name and the desired permission mode (Symbolic or Absolute) as inputs.
2.Check if the file exists. If not, display an error message and exit.
3.If the file exists, check if the permission mode is symbolic or absolute.
4.If the permission mode is symbolic, accept the symbolic permissions as input.
5.Use the chmod command with the appropriate options and the symbolic permissions to assign
the file permissions.
6.If the permission mode is absolute, accept the octal permissions as input.
7.Use the chmod command with the appropriate options and the octal permissions to assign the
file permissions.
9.If any error occurs during the process, display an error message.
Program:
echo "Enter the file
ls -l $file
$file chmod g+wx $file chmod o-r $file echo "File permission
ls -l $file
echo "Changing permission using absolute
mode"
chmod u=rw $file
ls -l $file
Output:
Result : The above algorithm and program clearly satisfy the desired aim of assigning
permission to a given file using symbolic mode or absolute mode .
2. Write a shell script to create two directories and store five files in one directory using the
related commands and to transfer all the files to another directory.
AIM: Create two directories, store five files in one directory, and transfer all the files to another
directory using related commands.
ALGORITHM:
4.Use the touch command to create the files within the `source_dir`.
5.Transfer all the files from the `source_dir` to the `destination_dir`.
is " pwd
$name" cd $name
of current directory" ls
$name1" cd ..
cd $name1
echo "Current directory is `pwd`" echo
"Display contents of $name1 " ls
Output:
Result : The above algorithm and program clearly satisfy the desired aim to Create two
directories, store five files in one directory, and transfer all the files to another directory using
related commands
3. Write a shell script to accept a file name as input and display whether it exists or not. If it
exits, then give the details of its attributes like access permission, it’s size etc.
AIM: Accept a file name as input, display whether it exists or not, and if it exists, provide the
details of its attributes such as access permissions and size.
ALGORITM:
Program:
echo "Enter the
filename" read
name echo "File
Details" if
[ $name ] then
$name ] then
then
echo "File is
-w $name ]
then
executable" fi else
exist" fi
Output :
Result : The above algorithm and program clearly satisfy the desired aim to Accept a file name
as input, display whether it exists or not, and if it exists, provide the details of its attributes such
as access permissions and size.
4. To compresses a file using gzip and pack commands
ALGORITHM:
2.Check if the file exists. If not, display an error message and exit.
3.If the file exists, use the `gzip` command to compress the file.
4.Use the `pack` command to pack the compressed file into an archive.
6.If any error occurs during the process, display an error message.
Program:
echo "Enter the filename to be
compressed" read f
size is" wc -c $f
echo "Compress
gunzip $f.gz
is " wc -c $f
Output:
Result : The above algorithm and program clearly satisfy the desired aim of compressing a file
using gzip and pack command.
5. To find a given pattern in a list of files of current directory using grep and fgrep
commands.
AIM: Find a given pattern in a list of files in the current directory using grep and fgrep
commands.
ALOGRITHM:
5.If no matches are found, display a message indicating no matches were found.
Program:
echo "Enter file
name: " read f f1
echo "Searching
using grep." opt1=y
while [ $opt1 = y ] do echo "Enter the pattern you
want to search: " read pat echo "Output of grep
search: " grep "$pat" $f $f1 echo "Do you want to
enter one more pattern for grep (y/n):" read opt1
done opt2=y echo "Searching using fgrep. " while
[ $opt2 = y ] do echo "Enter pattern you want to
searh: "
read p fgrep "$p" $f $f1 echo "Do you want to
enter one more pattern for fgrep" read opt2
done
Output :
Result : The above algorithm and program clearly satisfy the desired aim of finding a given
pattern in a list of files in the current directory using grep and fgrep commands.
6. Shell Script to find out the sum of the given numbers using command line argument.
ALOGITHM:
Program:
#!/bin/bash
7. Write a shell script to find the largest among the 3 given numbers.
AIM: Write a shell script to find the largest and smallest numbers among the 3 given numbers.
ALGORITHM:
• If the second number is larger than `largest`, update `largest` with the second number.
• If the second number is smaller than `smallest`, update `smallest` with the second
number.
• If the third number is larger than `largest`, update `largest` with the third number.
• If the third number is smaller than `smallest`, update `smallest` with the third number.
Program:
echo "Enter Num1" read num1 echo "Enter
$num3 fi
Output :
Result : The above algorithm and program clearly satisfy the desired aim to Write a shell script
to find the largest and smallest numbers among the 3 given numbers.
8. Shell Script which works similar to the Unix commands Head Tail.
AIM: Create a shell script that functions similarly to the Unix commands `head` and
`tail`.
ALGORITHM:
2.Check if the file exists. If not, display an error message and exit.
3.If the file exists, check if the user wants to display the head or tail of the file.
7.Use the `tail` command with the specified number of lines to display the tail of the file.
8.If any error occurs during the process, display an error message.
PROGRAM: #!/bin/bash
exi
t1
fi
# Check if the user wants the head or
tail if [[ $3 == "head" ]]; then #
Display the head of the file head -n
$num_lines $filename elif [[ $3 ==
"tail" ]]; then # Display the tail of the
file
tail -n $num_lines $filename
else
Result : The above algorithm and program clearly satisfy the desired aim to Create a shell script
that functions similarly to the Unix commands `head` and `tail`.
ALGORITHM:
• Multiply the `reversed` variable by 10 and add the last digit to it.
Program:
temp=$number
Result : The above algorithm and program clearly satisfy the desired aim to reverse a number
provided by the user.
10. Write the shell script to find the sum, the average and the product of the four integers
entered.
AIM: Write a shell script to find the sum, average, and product of four integers entered by the
user.
ALGORITHM:
Program:
echo "Enter four integers with space
between" read a b c d
sum=`expr $a + $b + $c +
dec=`expr $sum % 4`
dec=`expr \( $dec \*
1000 \) / 4` product=`expr
$a \* $b \* $c \* $d` echo
Sum = $sum echo Average
= $product Output:
Result : The above algorithm and program clearly satisfy the desired aim to find the sum,
average, and product of four integers
11. Write a shell script to find how many terminals has this user logged in.
Program:
AIM: Write a shell script to find the number of terminals a user has logged in.
ALGORITHM:
2.Use the `who` command with the `-u` option to obtain the list of logged-in users.
5.Display the number of terminals the user has logged in. Program
echo "Enter the
LOGNAME" read
$lgname | wc -l` if [ $x -
eq 0 ] then echo "user
Output:
Result : The above algorithm and program clearly satisfy the desired aim to number of terminals
a user has logged in.
8.Display the sum of digits.
Program:
echo "Enter a
Number" read
num sum=0
while [ $num -
gt 0 ] do
Output :
Result : The above algorithm and program clearly satisfy the desired aim to find the sum of
digits of a given number.
12. Write a script to find the value of one number raised to the power of another.
AIM: Write a shell script to find the value of one number raised to the power of another.
ALGORITHM:
• Use a loop to multiply the base number with itself `exponent` number of times.
• Use the `bc` command to calculate the power using the` ^` operator.
Program :
echo "Input number" read
$counter ] do ans=`echo
counter=`echo $counter +
1 | bc` done
Output:
Result : The above algorithm and program clearly satisfy the desired aim to find the value of
one number raised to the power of another
13. Write a shell script, which will receive any number of filename as arguments. The shell
script should check whether such files already exists.
AIM: Write a shell script that receives any number of filenames as arguments and checks
whether the files already exist.
ALGORITHM:
3.Check if each file exists using the `-e` option with the `test` command.
Program:
#!/bin/bash for file in
"$@"; do if [ -e "$file" ];
exist."
fi
done
Output:
Result : The above algorithm and program clearly satisfy the desired aim to receives any number
of filenames as arguments and checks whether the files already exist.
14. Write a shell program to add, subtract and multiply the 2 given numbers passed as
command line arguments.
AIM: Write a shell program to add, subtract, and multiply two given numbers passed as
command-line arguments.
ALGORITHM:
2.Check if exactly two arguments are provided. If not, display an error message and exit.
3.Assign the first argument to the variable `num1` and the second argument to `num2`.
Program:
add=`expr $1 + $2`
sub=`expr $1 - $2`
mul=`expr $1 \* $2`
Result : The above algorithm and program clearly satisfy the desired aim to add, subtract, and
multiply two given numbers passed as command-line arguments.
15. Shell Script to Count and Report the Number of Entries in Each Subdirectory.
AIM: Write a shell script to count and report the number of entries (files and directories) in each
subdirectory mentioned in the path provided as a command-line argument.
ALGORITHM:
2.Check if the directory exists using the `-d` option with the `test` command.
6.Count the number of entries (files and directories) within each subdirectory.
8.Repeat steps 4-7 for all subdirectories within the main directory.
PROGRAM: #!/bin/bash
fi
ALGORITHM:
2.Check if the file exists using the `-f` option with the `test` command.
6.Redirect the reversed output to the temporary file using the` >` operator.
7.Overwrite the original file with the contents of the temporary file using the `mv` command.
PROGRAM:
#!/bin/bash
OUTPUT:
Result : The above algorithm and program clearly satisfy the desired aim to Write a shell script
to reverse the contents of a file.
17. Write a menu driven shell script for Copy a file, Move a file.
AIM: Write a menu-driven shell script to copy a file, remove a file, and move a file.
ALGORITHM:
1.Display a menu with options for file operations.
4.Repeat the menu and prompt until the user chooses to exit.
6.Display appropriate messages for successful file operations or any errors encountered.
Program:
echo "Menu " echo
Choice case
"$Choice" in
else
exist" fi
;;
f2 if [ -f $f1 ] then if [ -d
Output:
;;
4)