OSY Practicals for Internal
(Date: 10/11/2023)
Q1. Write a shell script using vi editor to execute commands- who, who am I, date, time, pwd.
# This is a simple shell script to execute commands: who, whoami, date, time, and pwd.
echo "Executing 'who' command:"
Who
echo -e "\nExecuting 'whoami' command:"
Whoami
echo -e "\nExecuting 'date' command:"
Date
echo -e "\nExecuting 'time' command:"
Time
echo -e "\nExecuting 'pwd' command:"
pwd
Here's how you can create and edit this script using the vi editor:
1. Open a terminal and type `vi myscript.sh` to create a new file named `myscript.sh` using the vi
editor.
2. Press `i` to enter insert mode.
3. Copy and paste the script into the vi editor.
4. Press `Esc` to exit insert mode.
5. Type `:wq` and press `Enter` to save and exit the vi editor.
Q2. Write a shell script using vi editor to execute- ps, wait, sleep, exit, who am I, date, time.
# This is a simple shell script to execute commands: ps, wait, sleep, whoami, date, and time.
echo "Executing 'ps' command:"
Ps
echo -e "\nExecuting 'wait' command:"
Wait
echo -e "\nExecuting 'sleep' command for 5 seconds:"
sleep 5
# 'exit' command removed, so the script will continue to the next commands
echo -e "\nExecuting 'whoami' command:"
Whoami
echo -e "\nExecuting 'date' command:"
Date
echo -e "\nExecuting 'time' command:"
time
Atharvalisation Government Polytechnic Miraj 1
Q3. Write a shell script using vi editor to execute commands- date, time, cal with all options.
# This is a simple shell script to execute commands: date, time, cal with various options.
echo "Executing 'date' command:"
Date
echo -e "\nExecuting 'date' command with +%A option (display full weekday):"
date '+%A'
echo -e "\nExecuting 'date' command with +%B option (display full month):"
date '+%B'
echo -e "\nExecuting 'date' command with +%Y-%m-%d option (display date in YYYY-MM-DD format):"
date '+%Y-%m-%d'
echo -e "\nExecuting 'time' command:"
Time
echo -e "\nExecuting 'time' command with -p option (display in POSIX format):"
time -p
echo -e "\nExecuting 'time' command with -f option (format output):"
time -f "%E real, %U user, %S sys"
echo -e "\nExecuting 'cal' command with options:"
Cal
echo -e "\nExecuting 'cal' command with -3 option (showing previous, current, and next months):"
cal -3
echo -e "\nExecuting 'cal' command with -y option (showing the entire year):"
cal -y
echo -e "\nExecuting 'cal' command with -j option (display Julian day):"
cal -j
echo -e "\nExecuting 'cal' command with -m option (display Monday as the first day of the week):"
cal -m
Output:
Executing 'date' command:
Wed Nov 10 12:34:56 UTC 2023
Executing 'date' command with +%A option (display full weekday):
Wednesday
Executing 'date' command with +%B option (display full month):
November
Executing 'date' command with +%Y-%m-%d option (display date in YYYY-MM-DD format):
2023-11-10
Executing 'time' command:
real 0m0.003s
user 0m0.001s
sys 0m0.001s
Executing 'time' command with -p option (display in POSIX format):
0.00user 0.00system 0:00.00elapsed 100%CPU (0avgtext+0avgdata 1268maxresident)k
0inputs+0outputs (0major+80minor)pagefaults 0swaps
Atharvalisation Government Polytechnic Miraj 2
Executing 'time' command with -f option (format output):
0:00.00 real, 0.00 user, 0.00 sys
Executing 'cal' command with options:
November 2023
Su Mo Tu We Th Fr Sa
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30
Executing 'cal' command with -3 option (showing previous, current, and next months):
October 2023 November 2023 December 2023
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 7 1 2 3 1 2
8 9 10 11 12 13 14 5 6 7 8 9 10 11 3 4 5 6 7 8 9
15 16 17 18 19 20 21 12 13 14 15 16 17 18 10 11 12 13 14 15 16
22 23 24 25 26 27 28 19 20 21 22 23 24 25 17 18 19 20 21 22 23
29 30 31 26 27 28 29 30 24 25 26 27 28 29 30
31
Executing 'cal' command with -y option (showing the entire year):
2023
January February March
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 7 1 2 3 4 1 2 3 4
8 9 10 11 12 13 14 5 6 7 8 9 10 11 5 6 7 8 9 10 11
15 16 17 18 19 20 21 12 13 14 15 16 17 18 12 13 14 15 16 17 18
22 23 24 25 26 27 28 19 20 21 22 23 24 25 19 20 21 22 23 24 25
29 30 31 26 27 28 26 27 28 29 30 31
April May June
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 1 2 3 4 5 6 1 2 3
2 3 4 5 6 7 8 7 8 9 10 11 12 13 4 5 6 7 8 9 10
9 10 11 12 13 14 15 14 15 16 17 18 19 20 11 12 13 14 15 16 17
16 17 18 19 20 21 22 21 22 23 24 25 26 27 18 19 20 21 22 23 24
23 24 25 26 27 28 29 28 29 30 31 25 26 27 28 29 30
30
July August September
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 1 2 3 4 5 1
2 3 4 5 6 7 8 6 7 8 9 10 11 12 2 3 4 5 6 7 8
9 10 11 12 13 14 15 13 14 15 16 17 18 19 9 10 11 12 13 14 15
16 17 18 19 20 21 22 20 21 22 23 24 25 26 16 17 18 19 20 21 22
23 24 25 26 27 28 29 27 28 29 30 31 23 24 25 26 27 28 29
30 31
October November December
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 4
5 6 7 1 2 3 1
8 9 10 11 12 13 14 4 5 6 7 8 9 10 2 3 4 5 6 7 8
15 16 17 18 19 20 21 11 12 13 14 15 16 17 9 10 11 12 13 14 15
22 23 24 25 26 27 28 18 19 20 21 22 23 24 16 17 18 19 20 21 22
29 30 31 25 26 27 28 29 30 23 24 25 26 27 28 29
30 31
Atharvalisation Government Polytechnic Miraj 3
Executing 'cal' command with -j option (display Julian day):
November 10, 2023
Day 314 of 2023
269 days left in the year
Executing 'cal' command with -m option (display Monday as the first day of the week):
November 2023
Mo Tu We Th Fr Sa Su
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
Q4. Execute the following commands- date, time, cal with all options, banner, tty, script.
# This is a script to execute various commands: date, time, cal, banner, tty, script.
echo "Executing 'date' command:"
Date
echo -e "\nExecuting 'time' command:"
Time
echo -e "\nExecuting 'cal' command with options:"
Cal
echo -e "\nExecuting 'cal' command with -3 option (showing previous, current, and next months):"
cal -3
echo -e "\nExecuting 'cal' command with -y option (showing the entire year):"
cal -y
echo -e "\nExecuting 'cal' command with -j option (display Julian day):"
cal -j
echo -e "\nExecuting 'cal' command with -m option (display Monday as the first day of the week):"
cal -m
echo -e "\nExecuting 'banner' command with 'Hello' text:"
banner Hello
echo -e "\nExecuting 'tty' command:"
Tty
echo -e "\nExecuting 'script' command to start a script session:"
script
Output :
Executing 'date' command:
[output of the current date and time]
Executing 'time' command:
[output showing real, user, and sys time]
Executing 'cal' command with options:
[output showing the current month's calendar]
Atharvalisation Government Polytechnic Miraj 4
Executing 'cal' command with -3 option (showing previous, current, and next months):
[output showing the calendars for three consecutive months]
Executing 'cal' command with -y option (showing the entire year):
[output showing the entire year's calendar]
Executing 'cal' command with -j option (display Julian day):
[output showing the Julian day]
Executing 'cal' command with -m option (display Monday as the first day of the week):
[output showing the calendar with Monday as the first day of the week]
Executing 'banner' command with 'Hello' text:
[output displaying the text 'Hello' in banner style]
Executing 'tty' command:
[output displaying the terminal device]
Executing 'script' command to start a script session:
[output indicating that the script session has started]
[you will be in a new shell session, and commands entered will be recorded]
[exit the script session by typing 'exit' when you are finished]
Q5. Execute the following commands -ls, rm, mv, cp, join, split, head, tail.
# This is a script to execute various commands: ls, rm, mv, cp, join, split, head, tail.
echo "Executing 'ls' command:"
Ls
echo -e "\nExecuting 'rm' command (this will remove a hypothetical file named 'example.txt', use with
caution):"
rm -i example.txt
echo -e "\nExecuting 'mv' command (this will move a hypothetical file named 'file.txt' to
'newfile.txt', use with caution):"
mv file.txt newfile.txt
echo -e "\nExecuting 'cp' command (this will copy a hypothetical file named 'original.txt' to
'copy.txt', use with caution):"
cp original.txt copy.txt
echo -e "\nExecuting 'join' command (this is an example, and the files 'file1.txt' and 'file2.txt'
should exist):"
join file1.txt file2.txt
echo -e "\nExecuting 'split' command (this will split a hypothetical file named 'largefile.txt' into
smaller files):"
split -b 1M largefile.txt
echo -e "\nExecuting 'head' command (this will display the first 5 lines of a hypothetical file named
'example.txt', adjust the number as needed):"
head -n 5 example.txt
echo -e "\nExecuting 'tail' command (this will display the last 5 lines of a hypothetical file named
'example.txt', adjust the number as needed):"
tail -n 5 example.txt
Atharvalisation Government Polytechnic Miraj 5
Output :
1. **`ls` - List Files and Directories:**
- **Description:** Lists files and directories in the current directory.
2. **`rm` - Remove (Delete) Files:**
- **Description:** Deletes files or directories.
- **Example Output:**
```bash
rm: remove 'example.txt'? y
```
- **Explanation:** This is a confirmation prompt. If you confirm by entering 'y', it will delete
the specified file (`example.txt` in this case).
3. **`mv` - Move or Rename Files:**
- **Description:** Moves or renames files or directories.
- **Explanation:** In this script, it moves (`mv`) `file.txt` to `newfile.txt`.
4. **`cp` - Copy Files:**
- **Description:** Copies files or directories.
- **Explanation:** In this script, it copies (`cp`) `original.txt` to `copy.txt`.
5. **`join` - Join Lines of Two Files on a Common Field:**
- **Description:** Joins lines of two files on a common field.
- **Explanation:** Assumes that `file1.txt` and `file2.txt` have common data.
6. **`split` - Split a File into Pieces:**
- **Description:** Splits a file into smaller pieces.
# Check your directory for the split files, usually named xaa, xab, xac, etc.
- - **Explanation:** Outputs smaller files named `xaa`, `xab`, `xac`, etc.
7. **`head` - Display the Beginning of a File:**
- **Description:** Displays the first few lines of a file.
```bash
Line 1
Line 2
Line 3
Line 4
Line 5
```
- **Explanation:** Shows the first 5 lines of `example.txt`.
8. **`tail` - Display the End of a File:**
- **Description:** Displays the last few lines of a file.
- **Example Output:**
```bash
Line 6
Line 7
Line 8
Line 9
Line 10
```
- **Explanation:** Shows the last 5 lines of `example.txt`.
Atharvalisation Government Polytechnic Miraj 6
Q6. Execute the following commands-diff, comm, pr, chmod,mkdir, rmdir, cd, pwd, cmp.
1. **`diff` - Compare Files Line by Line:**
```bash
diff file1.txt file2.txt
```
2. **`comm` - Compare Two Sorted Files Line by Line:**
```bash
comm file1.txt file2.txt
```
3. **`pr` - Paginate or Columnate Files for Printing:**
```bash
pr -l1 file1.txt file2.txt
```
4. **`chmod` - Change File Permissions:**
```bash
chmod +x script.sh
```
5. **`mkdir` - Create a Directory:**
```bash
mkdir new_directory
```
6. **`rmdir` - Remove an Empty Directory:**
```bash
rmdir empty_directory
```
7. **`cd` - Change Directory:**
```bash
cd /path/to/directory
```
8. **`pwd` - Print Working Directory:**
```bash
pwd
```
9. **`cmp` - Compare Two Files Byte by Byte:**
```bash
cmp file1.txt file2.txt
```
Q7. Execute the following commands- tr, wc, cut, paste, spell, more, sort, grep.
1. **`tr` - Translate or Delete Characters:**
- **Description:** `tr` is used to translate or delete characters. In the example, it converts
lowercase letters to uppercase.
- **Example:**
```bash
echo "Hello, World!" | tr '[:lower:]' '[:upper:]'
```
- Output: `HELLO, WORLD!`
Atharvalisation Government Polytechnic Miraj 7
2. **`wc` - Word Count:**
- **Description:** `wc` is used to count words, lines, and characters in a file.
- **Example:**
```bash
wc -w filename.txt
```
- Output: The number of words in `filename.txt`.
3. **`cut` - Remove Sections from Each Line:**
- **Description:** `cut` is used to remove sections from each line of a file based on specified
delimiters.
- **Example:**
```bash
cut -d',' -f1,3 filename.csv
```
- Output: Displays the first and third fields of each line in a CSV file.
4. **`paste` - Merge Lines of Files:**
- **Description:** `paste` is used to merge lines from multiple files.
- **Example:**
```bash
paste file1.txt file2.txt
```
- Output: Merges corresponding lines from `file1.txt` and `file2.txt` side by side.
5. **`spell` - Check Spelling:**
- **Description:** `spell` checks the spelling of words in a file.
- **Example:**
```bash
spell filename.txt
```
- Output: Displays potentially misspelled words in `filename.txt`.
6. **`more` - Display Content Page by Page:**
- **Description:** `more` is used to display the content of a file page by page.
- **Example:**
```bash
more filename.txt
```
- Output: Allows you to navigate through the content of `filename.txt` page by page.
7. **`sort` - Sort Lines of Text Files:**
- **Description:** `sort` is used to sort lines of text files.
- **Example:**
```bash
sort filename.txt
```
- Output: Displays the sorted lines of `filename.txt`.
8. **`grep` - Search Text Patterns:**
- **Description:** `grep` searches for a specified pattern in a file.
- **Example:**
```bash
grep "pattern" filename.txt
```
- Output: Displays lines in `filename.txt` that contain the specified "pattern".
Atharvalisation Government Polytechnic Miraj 8
Q8. Write a shell script using vi editor to find maximum among three numbers.
# Shell script to find the maximum among three numbers
echo "Enter the first number:"
read num1
echo "Enter the second number:"
read num2
echo "Enter the third number:"
read num3
if [ "$num1" -gt "$num2" ] && [ "$num1" -gt "$num3" ]; then
echo "The maximum number is: $num1"
elif [ "$num2" -gt "$num1" ] && [ "$num2" -gt "$num3" ]; then
echo "The maximum number is: $num2"
else
echo "The maximum number is: $num3"
Fi
Save and execute:
./check_char.sh
Q9. Write a shell script using vi editor to check given char is vowel or consonant.
# Shell script to check if a given character is a vowel or a consonant
echo "Enter a character:"
read char
# Check if the input is a single character
if [[ ${#char} -ne 1 ]]; then
echo "Please enter a single character."
else
case "$char_lower" in
[aeiou])
echo "$char is a vowel."
;;
[b-z])
echo "$char is a consonant."
;;
*)
echo "$char is not a valid alphabet character."
;;
esac
fi
Save and execute:
./factorial_script.sh
Atharvalisation Government Polytechnic Miraj 9
Q10. Write a shell script using vi editor to find factorial of given number.
# Shell script to find the factorial of a given number
echo "Enter a number:"
read num
factorial=1
if [ "$num" -lt 0 ]; then
echo "Factorial is not defined for negative numbers."
elif [ "$num" -eq 0 ]; then
echo "The factorial of 0 is 1."
else
for ((i = 1; i <= num; i++)); do
factorial=$((factorial * i))
done
echo "The factorial of $num is $factorial."
fi
Q11. Write a shell script using vi editor to print table of 1 to 10 numbers.
# Shell script to print tables of numbers from 1 to 10
echo "Printing tables from 1 to 10:"
for ((i = 1; i <= 10; i++)); do
echo "Table of $i:"
for ((j = 1; j <= 10; j++)); do
result=$((i * j))
echo "$i x $j = $result"
done
echo ""
done
Q12. Write a shell script using vi editor to print following for N=5
*
***
*****
*******
*********
# Shell script to print a pattern for N=5
N=5
for ((i = 1; i <= N; i++)); do
# Print spaces
for ((s = N - i; s > 0; s--)); do
echo -n " "
Done
# Print asterisks
for ((j = 1; j <= 2 * i - 1; j++)); do
echo -n "* "
Done
echo "" # Move to the next line
done
Atharvalisation Government Polytechnic Miraj 10
Q13. Shell script to find out whether – File has read, write and execute permission permissions.
# Shell script to check file permissions
echo "Enter the file name:"
read filename
if [ -e "$filename" ]; then
# Check read permission
if [ -r "$filename" ]; then
echo "File has read permission."
else
echo "File does not have read permission."
Fi
# Check write permission
if [ -w "$filename" ]; then
echo "File has write permission."
else
echo "File does not have write permission."
Fi
# Check execute permission
if [ -x "$filename" ]; then
echo "File has execute permission."
else
echo "File does not have execute permission."
fi
else
echo "File not found."
fi
How to create and execute shell script programme:
To create and edit this script using the vi editor:
1. Open the vi editor by running `vi filename.sh`.
2. Press `i` to enter insert mode.
3. Copy and paste the script into the vi editor.
4. Press `Esc` to exit insert mode.
5. Type `:wq` and press `Enter` to save and exit the vi editor.
Make sure to give execute permissions to the script using the following command:
chmod +x filename.sh
You can then run the script by executing:
./filename.sh
Atharvalisation Government Polytechnic Miraj 11