OSY - Model Notes
OSY - Model Notes
(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.
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.
# 'exit' command removed, so the script will continue to the next commands
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 '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 -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 +%Y-%m-%d option (display date in YYYY-MM-DD format):
2023-11-10
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
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
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 -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 -m option (display Monday as the first day of the week):"
cal -m
Output :
Executing 'date' command:
[output of the current date and time]
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]
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 -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
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!`
Q9. Write a shell script using vi editor to check given char is vowel or consonant.
factorial=1
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
# Print asterisks
for ((j = 1; j <= 2 * i - 1; j++)); do
echo -n "* "
Done
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
Make sure to give execute permissions to the script using the following command:
chmod +x filename.sh
./filename.sh