Linux Practical
Linux Practical
Sr.
No
1. Working with vi editor?
1
Q1. working with vi editor?
Ans :
1. Opening a File:
• To open a file with Vi, open your terminal and type `vi` followed by the filename. For
example: `vi filename.txt`.
• If the file doesn't exist, Vi will create a new file with that name when you save.
2. Modes:
• Vi has different modes:
• Normal mode: This is the default mode for navigation and issuing commands.
• Insert mode: In this mode, you can insert or edit text.
• Command line mode: This mode allows you to save, quit, and perform otherfilerelated
actions.
Remember that Vi is a powerful text editor with many more commands and features. These are
just the basics to help you get started. The more you use it, the more proficientyou will become.
Q2. find the factorial of any number using awk command in unix?
Ans:
echo "Enter a number: "
read num
./calculate_factorial.awk
Q3. Write menu driven shell script to execute 5 basic command of unix?
Ans :
#!/bin/bash
while true; do
clear
echo "Basic Unix Command Menu"
echo " "
echo "1. List Files (ls)"
echo "2. Print Working Directory (pwd)"
echo "3. Display Date and Time (date)"
echo "4. List Users (who)"
echo "5. Display Calendar (cal)"
echo "6. Exit"
case $choice in
1)
echo "Listing Files:"ls
;;
2)
echo "Current Directory:"
pwd
;;
3)
echo "Date and Time:"
date
;;
4)
echo "Calendar:"
cal
;;
6)
Ans :
Certainly! Here's a simple menudriven shell script in Unix that allows you to executefive basic
Unix commands: `ls`, `pwd`, `date`, `who`, and `cal`. You can customize it further or add more
commands as needed:
```bash
#!/bin/bash
while true; do
clear
echo "Basic Unix Command Menu"
echo " "
echo "1. List Files (ls)"
echo "2. Print Working Directory (pwd)"
echo "3. Display Date and Time (date)"
echo "4. List Users (who)"
echo "5. Display Calendar (cal)"
echo "6. Exit"
echo "Calendar:"cal;;
1. It displays a menu with five basic Unix commands and an option to exit.
2. It reads your choice and performs the corresponding action using a `case` statement.
3. After executing a command, it prompts you to press Enter to continue and clears thescreen
for the next iteration.
9
Q5. display the out put of ls l command in user friendly way?Ans
:
#!/bin/bash
It runs the ls l command and captures its output in the ls_output variable.
It checks if the output is empty (indicating no files in the directory) and handles that case.It prints
a header line to label the columns.
It then processes each line of the ls l output using a while loop, extracting and formattingthe
relevant information (permissions, owner, group, size, modification date, and name) for each file
or directory.
Finally, it prints the formatted output.
Save this script to a file (e.g., ls_friendly.sh), make it executable with chmod +x ls_friendly.sh,
and then run it using ./ls_friendly.sh in the directory you want to list. Thisscript will display the
ls l output in a more userfriendly format.
Q6 . Find greatest among three no using shell script?Ans
:
#!/bin/bash
Example: pwd
cd: Change directory.
Example: cd /path/to/directory
mkdir: Create a new directory.
Example: rm file.txt
find: Search for files and directories.
case "$input" in
"vowel" | "unix" | "unix")
echo "The string \"$input\" is valid."
;;
)
echo "The string \"$input\" is not valid."
;;
esac
}
# Input
read p "Enter a twocharacter string: " user_input
It defines a function checkstring that checks whether the input string matches "vowel,""unix," or
"UNIX" (caseinsensitive).
If the input is two characters long, it calls the checkstring function to verify whether itmatches
one of the specified values. If it matches, it outputs that the string is valid; otherwise, it's not
valid.
Save this script to a file (e.g., check_string.sh), make it executable with chmod +x
check_string.sh, and then run it with ./check_string.sh. It will prompt you to enter a two
character string and tell you whether it matches the specified values
Q10. Write a script to check the no is prime or not?
Ans :
Certainly! Here's a simple menudriven shell script in Unix that allows you to executefive basic
Unix commands: `ls`, `pwd`, `date`, `who`, and `cal`. You can customize it further or add more
commands as needed:
```bash
#!/bin/bash
while true; do
clear
echo "Basic Unix Command Menu"
echo " "
echo "1. List Files (ls)"
echo "2. Print Working Directory (pwd)"
echo "3. Display Date and Time (date)"
echo "4. List Users (who)"
echo "5. Display Calendar (cal)"
echo "6. Exit"
19
pwd
;;
echo "Calendar:"cal;;