Download pdf - Click Here
ASSI 1
SET A:
1) How can you find out how much memory Linux is using?
You can find out the memory usage in Linux using several commands:
- `free -h`: This shows the total, used, and available memory,
with the `-h` option
providing human-readable formats like MB or GB.
- `top`: This command gives a dynamic real-time
view of system processes and memory
usage.
- `vmstat`: Displays system performance details
, including memory statistics.
- `htop`: Similar to `top`, but with a more user-friendly interface. It’s not
installed by default in all distributions, so you might need to install it using your
package manager.
2) How do you switch from one desktop environment to another, such as switching from
KDE to Gnome?
To switch from one desktop environment like KDE to Gnome:
- Log out of your current session.
- At the login screen, you’ll typically see an option to choose the session type.
Look for a gear icon or a menu near the login prompt.
- Select the desired desktop environment (such as Gnome or KDE) from the menu.
- Log in again, and you’ll be using the newly selected desktop environment.
If the desktop environment is not installed, you can install it using your package
manager. For example, to install Gnome on a system using apt (Debian/Ubuntu), you’d run:
bash
sudo apt install gnome
3) How do you execute more than one command or program from a single command line entry?
You can execute multiple commands in a single line in Linux by using different
operators:
- Using semicolon (`;`): Each command is executed one after another, regardless of
whether the previous one succeeded or failed.
bash
command1; command2; command3
- Using double ampersand (`&&`): The next command will only be executed if the
previous one succeeded (exit status 0).
bash
command1 && command2
- Using double pipe (`||`): The next command will only execute if the previous one
failed (exit status non-zero).
bash
command1 || command2
SET B:
1) How can you install multiple packages using a single command?
You can install multiple packages at once by listing them in a single command
separated by spaces. For example, using `apt` package manager:
bash
sudo apt install package1 package2 package3
Replace `package1`, `package2`, and `package3` with the actual package names.
2) How to remove software that was installed using `.deb`?
To remove software installed via a `.deb` file, you can use the following command:
bash
sudo apt remove package_name
Replace `package_name` with the actual name of the software. If you don’t remember
the package name, you can use `dpkg` to find it:
bash
dpkg --list
Then find and remove the package.
3) Install all Python packages (from `requirements.txt` or from pip):
If you have a `requirements.txt` file containing the list of Python packages to be
installed, use:
bash
pip install -r requirements.txt
If you want to install all available Python packages, you could use:
bash
pip install --upgrade pip setuptools wheel
For more specific packages, list the desired ones explicitly.
---
SET C:
1) Install the following software using `sudo apt install`:
Before installing any software, it's good practice to update the package list:
bash
sudo apt update
Then install the following packages with a single command:
bash
sudo apt install python3-pip gcc default-jdk libapache2-mod-php apache2 mysql-server
- `python3-pip`: Installs the pip package manager for Python 3.
- `gcc`: Installs the GNU C Compiler.
- `default-jdk`: Installs the default Java Development Kit.
- `libapache2-mod-php`: Installs the Apache module for PHP.
- `apache2`: Installs the Apache web server.
- `mysql-server`: Installs the MySQL database server.
After running the command, follow any additional prompts that may appear for
configuration.
ASSI 2
# SET A:
1) What is the default desktop for Ubuntu?
- The default desktop environment for Ubuntu is GNOME (starting from version 17.10).
Prior to that, Unity was the default desktop environment.
2) Enlist different Desktop Environments. Also, choose one for your operating system
and apply it.
Some popular desktop environments include:
- GNOME (default for Ubuntu)
- KDE Plasma
- Xfce
- LXDE
- MATE
- Cinnamon
To install and switch to another desktop environment, you can use:
bash
sudo apt install <desktop-environment-name>
After installation, log out and choose the new desktop environment from the login
screen.
3) Configure panel for your system.
To configure the GNOME panel or any other desktop environment’s panel:
- Right-click on the panel and choose Settings or Panel Preferences.
- From there, you can adjust the panel size, position, and add or remove items
(such as app launchers or system monitors).
4) Enable Local Menus in Ubuntu.
To enable local menus (menus shown in the application window rather than the top
panel):
- Open Settings → Appearance → Behavior.
- Change Show the menus for a window to In the window's title bar.
5) Launch desktop launcher on Ubuntu.
- You can launch applications from the desktop launcher by clicking the Activities
button in the top-left corner, or pressing the Super (Windows) key, and typing the
name of the application you want to run.
- Alternatively, you can create a custom launcher by right-clicking on the desktop
and selecting Create Launcher.
---
# SET B:
1) Change your desktop background and wallpaper.
- Right-click on the desktop and select Change Background.
- From the window that opens, choose a background image or a solid color, then
click Apply.
2) Select color box and select the desktop color of your choice and apply it.
- Go to Settings → Background.
- Select the Colors tab and choose the color you prefer for your desktop.
3) Select a screensaver from the Preferences menu.
- Go to System → Preferences → Screensaver.
- From the list of available screensavers, choose one you like and click Apply.
4) Customize your window borders and icons.
- Go to Settings → Appearance → Themes.
- Select the Windows tab to customize window borders, and the Icons tab to change
the icon set.
5) Specifying theme name and description for your system and view an application in a
modified theme.
- Go to Settings → Appearance → Themes.
- Select the theme you want to apply. You can also modify a theme by editing the
theme files or downloading new themes. Once done, launch any application to view the
changes under the modified theme.
---
SET C:
1) Launch the Screen Resolution Preferences Dialogue Box and change the default
resolution.
- Go to Settings → Displays.
- In the Resolution dropdown, select your desired screen resolution and click
Apply.
2) Change the time and date of your system.
- Open Settings → Time & Date.
- Disable Automatic Time if enabled, then manually adjust the time and date
according to your preference.
- Click Apply to confirm the changes.
Ass 3
# SET A:
1) Explore all the UNIX commands given in this manual.
- You can explore Unix commands by using the `man` (manual) command. For example,
to see the manual for a specific command like `ls`:
bash
man ls
- To list all available commands, you can explore manuals by navigating
through sections with:
bash
man -k .
This will give a list of commands and their brief descriptions.
2) Create a directory.
- To create a directory, use the `mkdir` command:
bash
mkdir directory_name
3) Create a subdirectory in the directory created.
- After creating the main directory, you can navigate to it and create a
subdirectory:
bash
mkdir directory_name/subdirectory_name
Or, if you are inside the directory already:
bash
cd directory_name
mkdir subdirectory_name
4) Change your current directory to the subdirectory.
- To change your current directory to the subdirectory, use the `cd` command:
bash
cd directory_name/subdirectory_name
5) Display the calendar for the current month.
- Use the `cal` command to display the calendar for the current month:
bash
cal
6) Get a directory listing of the parent directory.
- To list the contents of the parent directory, you can use the `ls` command
with the `..` symbol (which represents the parent directory):
bash
ls ..
7) How many users were logged onto your system?
- Use the `who` or `w` command to see the currently logged-in users:
bash
who
- To count the number of users, you can use:
bash
who | wc -l
8) Display your name in the form of a banner.
- Use the `banner` command to display text as a banner (in large letters). This
may not be installed by default on some systems:
bash
banner your_name
9) Display the device name of your terminal.
- You can find out the device name of your terminal by using the `tty` command:
bash
tty
10) Move to the root directory.
- Use the `cd` command to move to the root directory:
bash
cd /
SET B:
1) Create a directory named `FYBCA` under that create 3 directories `CO`, `RDBMS`,
and `O.S.` Create files under subdirectories `CO` and `RDBMS` and move these files
to `O.S`.
bash
mkdir FYBCA
cd FYBCA
mkdir CO RDBMS O.S
touch CO/file1.txt RDBMS/file2.txt
mv CO/file1.txt O.S/
mv RDBMS/file2.txt O.S/
2) Display username, user ID, hostname, kernel name along with system information.
bash
echo "Username: $(whoami)"
echo "User ID: $(id -u)"
echo "Hostname: $(hostname)"
uname -a
3) Display list of all files ending with `.txt` from the current working directory.
bash
ls *.txt
4) Copy the file `MyFile.txt` to directory `assignment1` and rename it to `t_1.txt`.
bash
mkdir assignment1
cp MyFile.txt assignment1/t_1.txt
5) Display the last modification and access time of a particular file.
- Use the `stat` command to display detailed file information:
bash
stat filename.txt
6) Create file `student.txt` and display the size of the file in bytes.
bash
touch student.txt
ls -l student.txt | awk '{print $5}'
7) Display the first and last 5 lines of `student.txt`.
bash
head -n 5 student.txt
tail -n 5 student.txt
8) Prepare two text files and check the output of `diff`, `comm`, and `cmp` commands.
- Create two text files:
bash
echo "This is file 1" > file1.txt
echo "This is file 2" > file2.txt
- Compare using `diff` (shows differences):
bash
diff file1.txt file2.txt
- Compare using `comm` (shows common and unique lines):
bash
comm file1.txt file2.txt
- Compare using `cmp` (byte-by-byte comparison):
bash
cmp file1.txt file2.txt
---
SET C:
1) Make a list of all filenames in `/etc` that contain the string `samba`.
bash
grep -l samba /etc/*
2) Make a sorted list of all files in `/etc` that contain the case-insensitive string
`samba`.
bash
grep -il samba /etc/* | sort
3) Write a line that receives `cities.txt` file and sort that file.
bash
sort cities.txt
4) Accept a text file and display the second to the seventh character of that file.
bash
cut -c 2-7 filename.txt
5) Create a text file which contains names of cities including the city name Pune
in that file. Display Pune along with the previous and next city name.
bash
echo -e "Mumbai\nNashik\nPune\nNagpur" > cities.txt
grep -A 1 -B 1 "Pune" cities.txt
6) Put a sorted list of all logged-on users in `onlineusers.txt`.
bash
who | awk '{print $1}' | sort > onlineusers.txt
7) Accept the file and display that file along with line numbers.
bash
nl filename.txt
Ass 4
SET A:
1) Create any text file and count the number of bytes, words, lines, and the length
of the longest line in the file.
- Create a file:
bash
echo "This is a sample file.\nIt has multiple lines." > sample.txt
- Use the `wc` (word count) command to count:
bash
wc sample.txt
- To find the length of the longest line:
bash
wc -L sample.txt
2) Count the number of files in the current working directory.
bash
ls -l | grep -v '^d' | wc -l
3) Redirect output of long listing of directories into `abc.txt`.
bash
ls -l > abc.txt
4) Count the number of users who are logged in and store the output in `a.txt`.
bash
who | wc -l > a.txt
5) Display a list of directories and how much space they consume, sorted from the
largest to the smallest.
bash
du -h --max-depth=1 | sort -rh
6) Concatenate two files `a.txt` and `b.txt` into a third file `c.txt`.
bash
cat a.txt b.txt > c.txt
---
SET B:
1) Create the following text file `a.txt` and write commands based on it:
bash
echo -e "Unix distributed 05 server\nLinux virtual 3 server\nUnix
distributed 05 server\nDistributed processing 6 system" > a.txt
a) Sort the file in alphabetical order:
bash
sort a.txt
b) Sort the file in descending order:
bash
sort -r a.txt
c) Sort the file in numerical order (based on any numbers in the file):
bash
sort -n a.txt
d) Sort the file based on the second field:
bash
sort -k 2 a.txt
e) Sort the file based on the second and fourth field in reverse order:
bash
sort -k 2,2r -k 4,4r a.txt
f) Remove duplicate entries from the file and store in `c.txt`:
bash
sort -u a.txt > c.txt
2) Create the following text file `a.txt` and write commands based on it:
bash
echo -e "welcome to ostechnix\nwelcome to ostechnix\nLinus is the creator
of Linux.\nLinux is secure by default\nLinus is the creator of Linux.
\nTop 500 super computers are powered by Linux" > a.txt
a) Remove consecutive duplicate lines:
bash
uniq a.txt
b) Display only unique lines:
bash
sort a.txt | uniq
c) Display only duplicate lines:
bash
sort a.txt | uniq -d
d) Display the number of occurrences of each line:
bash
sort a.txt | uniq -c
e) Limit the comparison to the first 4 characters of each line and display
repeated lines:
bash
sort a.txt | uniq -w 4 -d
f) Avoid the comparison with the first 4 characters of each line:
bash
sort a.txt | uniq -s 4
3) Create the following text file `a.txt` and write commands based on it:
bash
echo -e "This is line 1 UNIX UNIX\nThis is line 2 unix\nThis is line 3 Unix
Unix\nThis is line 4 hello" > a.txt
a) Display lines that contain the search pattern “unix”:
bash
grep "unix" a.txt
b) Display lines that contain the search pattern “unix” in a case-insensitive
manner:
bash
grep -i "unix" a.txt
c) Display line numbers that contain the search pattern “unix”:
bash
grep -n "unix" a.txt
d) Display the count of lines that contain the search pattern “unix”:
bash
grep -c "unix" a.txt
e) Display lines that do not contain the pattern “unix”:
bash
grep -v "unix" a.txt
f) Display lines that contain a starting letter "U" and an ending letter "x":
bash
grep "^U.*x$" a.txt
SET C:
1) Create file `file.txt` as follows and write commands for it:
bash
echo -e "unix or linux os\nis unix good os\nis linux good os" > file.txt
a) Write a Linux command to print characters at the 4th position:
bash
cut -c 4 file.txt
b) Write a Linux command to print characters by range 4-7:
bash
cut -c 4-7 file.txt
c) Write a Linux command that prints the second field in each line by treating
space as the delimiter:
bash
cut -d ' ' -f 2 file.txt
---
2) Create file with the following content:
bash
echo -e "Linux\nUnix\nSolaris\nHPUX\nAIX" > file.txt
a) Write a Linux command to join all lines separated by a tab:
bash
paste -s file.txt
b) Write a Linux command to join all lines separated by a comma:
bash
paste -s -d ',' file.txt
c) Write a Linux command to merge the file by pasting the data into 2 columns:
bash
paste - - < file.txt
d) Write a Linux command to merge the file by pasting the data into 2 columns
using a colon separator:
bash
paste -d ':' - - < file.txt
Assi 5
SET A:
1) Create three text files `one.txt`, `two.txt`, and `three.txt`. Check the
permissions of the created files.
bash
touch one.txt two.txt three.txt
ls -l one.txt two.txt three.txt
2) Change the permission of `one.txt` to give read, write, execute access for the
owner, read, write for the group, and only read for others.
bash
chmod 764 one.txt
3) Change the permission of `two.txt` to give read, write, and execute access for
the owner, group, and others.
bash
chmod 777 two.txt
4) Change the permission of `three.txt` to give read, write access for the owner,
read, execute for the group, and only execute for others.
bash
chmod 751 three.txt
5) Create a directory `Assignment`. Create two subdirectories `Ass1` and `Ass2`
under it. Check the permission of the directory.
bash
mkdir -p Assignment/Ass1 Assignment/Ass2
ls -ld Assignment Assignment/Ass1 Assignment/Ass2
6) Change the permission of the `Assignment` directory to give read, write,
and execute access for the owner, group, and others, including subdirectories.
bash
chmod -R 777 Assignment
7) Change the owner of `one.txt` to a specific user (replace `(username)`
with the actual username).
bash
sudo chown username one.txt
8) Change the group of `two.txt` to a specific user group (replace `(username)`
with the actual username).
bash
sudo chgrp username two.txt
---
SET B:
1) Check the processes that are running on the system. Write the details of at
least 3 processes below.
- Use the `ps` command to see running processes:
bash
ps aux
- Example of 3 processes:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.1 16932 1328 ? Ss 09:45 0:01 init
user 101 0.2 0.5 29528 5220 pts/0 R+ 09:46 0:00 bash
root 110 0.1 0.3 42100 3140 ? Ss 09:46 0:01 sshd
2) See the details of a particular process by specifying the Process ID (PID).
- Replace `PID` with the actual process ID (for example, 101):
bash
ps -p PID -f
Example:
bash
ps -p 101 -f
3) Terminate a process.
- Replace `PID` with the actual process ID:
bash
kill PID
4) Check the free and used memory status of the system in MB and GB.
bash
free -m # Memory status in MB
free -g # Memory status in GB
5) Change the priority of a process to `-5`.
- Replace `PID` with the actual process ID:
bash
sudo renice -5 PID
6) Execute the `top` command and see the output.
bash
top
- The `top` command will show real-time system statistics like CPU usage, memory
usage, running processes, and more. You can exit the `top` view by pressing `q`.
SET A:
7) Create a `trial.txt` file. Type 3 echo statements, `date`, and `ls` commands in
that file. Schedule the execution of that file at 15:30 (3:30 PM).
- First, create the `trial.txt` file with the commands:
bash
echo -e "echo 'Hello World'\necho 'This is a test'\necho 'Another echo
statement'\ndate\nls" > trial.txt
- Make the file executable:
bash
chmod +x trial.txt
- Schedule the execution using `at` command:
bash
echo "./trial.txt" | at 15:30
8) Check the schedule status of `trial.txt`.
- Use the following command to check scheduled jobs:
bash
atq
- To see the details of the scheduled job, use:
bash
at -c <job_id>
---
SET C:
1) Check the IP address of your system. Mention the command with the IP address.
- Use the `ip` command to check your IP address:
bash
ip addr show
- Example output:
inet 192.168.1.10/24
2) Check if a system with a specific IP address is running in the LAN.
- Use the `ping` command to check if the system is reachable:
bash
ping <IP_address>
- Example:
bash
ping 192.168.1.10
3) Write a command to find the domain name of an IP address connected in LAN.
Mention the domain name also.
- Use the `nslookup` or `host` command:
bash
nslookup <IP_address>
- Example:
bash
nslookup 192.168.1.10
- Another example using `host`:
bash
host <IP_address>
4) Use `ftp` to upload `one.txt` to a remote server.
- Use `ftp` to connect to the remote server:
bash
ftp <remote_server_ip>
- Once connected, use the following commands to upload:
bash
put one.txt
5) Download a file from a remote server using `ftp`.
- Use the `ftp` command to connect to the server:
bash
ftp <remote_server_ip>
- To download a file (for example, `file.txt`), use:
bash
get file.txt
ASSI 6
# Set A:
1) Create a file by the name `Mycollege.txt` with at least 25 lines using the `vi`
editor's input commands "a" and "i". Also, try the replace mode by examining the
toggle feature of the "i" character.
- Open the `vi` editor:
bash
vi Mycollege.txt
- To insert content using the "i" (insert mode):
- Press `i` to enter insert mode and start typing.
- After typing some content, press `Esc` to exit insert mode.
- To append content using the "a" (append mode):
- Press `a` to append after the current cursor position.
- After typing, press `Esc` to exit.
- To toggle replace mode:
- After entering insert mode with `i`, press `R` to toggle to replace mode,
then type to overwrite the text.
- To save and exit:
bash
:wq
2) Create a file with at least 25 lines using `vi` editor's input commands "a" and
"i". Also, try the search command on the file.
- Open the file:
bash
vi filename.txt
- Add 25 lines using the `i` (insert) and `a` (append) commands as before.
- To search for text:
- Press `/` followed by the word you want to search (for example, `/word`).
- To move to the next occurrence of the word, press `n`.
- Save and exit:
bash
:wq
---
Set B:
1) Create a file `Input devices.txt` with at least 25 lines using the `vi` editor’s
input commands. Perform the following tasks:
a) Move to the first line of the file:
- In command mode, press `gg` to go to the first line of the file.
b) Insert a blank line below the 3rd line:
- Move to the third line by typing `3G`.
- Type `o` to insert a blank line below the current line and enter insert mode.
c) Delete any 3 lines:
- To delete the 3rd, 4th, and 5th lines, type `3dd` in command mode.
d) Join any two lines together:
- Move to the first of the two lines and press `J` to join them.
e) Restore the single deleted line:
- Type `u` in command mode to undo the last delete operation and restore the
deleted line.
f) Search the keyword "dev" from the file:
- Type `/dev` in command mode to search for the word "dev".
---
2) Create a file with five lines and execute the following commands in `vi` editor:
- Open the file:
bash
vi five_lines.txt
a) Move to the first line:
- Command: `gg`
- Result: Moves the cursor to the first line of the file.
b) Move to the last line:
- Command: `G`
- Result: Moves the cursor to the last line of the file.
c) Delete the current line:
- Command: `dd`
- Result: Deletes the current line where the cursor is placed.
d) Undo the previous action:
- Command: `u`
- Result: Restores the deleted line by undoing the last action.
e) Redo the previous action:
- Command: `Ctrl + r`
- Result: Repeats the last action (re-does the undo operation).
f) Save and exit:
- Command: `:wq`
- Result: Saves the file and exits the `vi` editor.
Set C:
1) Create a file named `My_country.txt` with at least 25 lines using the `vi` editor's
input commands and try the following commands:
- Open the `vi` editor:
bash
vi My_country.txt
- Enter insert mode by pressing `i` or `a` and type at least 25 lines about your
country. After entering your text, press `Esc` to exit insert mode.
a) Move to the first line of the file:
- In command mode, press:
bash
gg
- This command moves the cursor to the first line of the file.
b) Replace each occurrence of the word “College” with “Institute”:
- Type the following command in command mode:
bash
:%s/College/Institute/g
- This command searches for all occurrences of "College" and replaces them
with "Institute".
c) Move to the beginning of the 2nd line:
- In command mode, type:
bash
2G
- This command moves the cursor to the start of the second line.
d) Apply control command on the file:
- For example, to sort the lines, you can use:
bash
:sort
- This command sorts all lines in the file alphabetically.
e) Search for the keyword “India” overall in the file:
- In command mode, type:
bash
/India
- This command searches for the word "India" and highlights its occurrences.
Press `n` to go to the next occurrence.
f) Copy and paste any 4 lines from the file:
- Move to the first line you want to copy and enter command mode. Type:
bash
4yy
- This command yanks (copies) 4 lines starting from the current line.
- Move to the desired location where you want to paste the copied lines and type:
bash
p
- This command pastes the copied lines below the current line.
---
Summary of Commands:
- Open file: `vi My_country.txt`
- Move to first line: `gg`
- Replace text: `:%s/College/Institute/g`
- Move to the beginning of 2nd line: `2G`
- Sort lines: `:sort`
- Search for keyword: `/India`
- Copy 4 lines: `4yy`
- Paste: `p`
- Save and exit: `:wq`
ASSI 7
Set A:
1) Create a file named `Mycollege.txt` with at least 25 lines using the `vi`
editor's input commands "a" and "i". Also, try the replace mode by examining the
toggle feature of the "i" character.
- Open the `vi` editor:
bash
vi Mycollege.txt
- To insert content using the "i" (insert mode):
- Press `i` to enter insert mode and start typing your content. For example:
Line 1: Welcome to My College
Line 2: It is a place of learning.
Line 3: We have dedicated teachers.
...
Line 25: Let's strive for excellence.
- After typing your content, press `Esc` to exit insert mode.
- To append content using the "a" (append mode):
- Press `a` to append after the current cursor position.
- Continue typing your content, then press `Esc` to exit.
- To toggle replace mode:
- After entering insert mode with `i`, press `R` to enter replace mode. Type
to overwrite existing text.
- To save and exit:
bash
:wq
2) Create a file with at least 25 lines using the `vi` editor's input commands "a"
and "i". Also, try the search command on the file.
- Open the file:
bash
vi filename.txt
- Add 25 lines using the `i` (insert) and `a` (append) commands as before.
- To search for text:
- Press `/` followed by the word you want to search (for example, `/word`).
- Press `n` to move to the next occurrence of the word.
- To save and exit:
bash
:wq
---
Set B:
1) Create a file `Input devices.txt` with at least 25 lines using the `vi` editor’s
input commands. Perform the following tasks:
- Open the file:
bash
vi "Input devices.txt"
a) Move to the first line of the file:
- In command mode, press:
bash
gg
b) Insert a blank line below the 3rd line:
- Move to the third line:
bash
3G
- Press `o` to insert a blank line below the current line and enter insert mode.
c) Delete any 3 lines:
- To delete the 3rd, 4th, and 5th lines, type:
bash
3dd
d) Join any two lines together:
- Move to the first of the two lines you want to join and press:
bash
J
e) Restore the single deleted line:
- Type:
bash
u
f) Search for the keyword "dev" in the file:
- Type:
bash
/dev
---
2) Create a file with five lines and execute the following commands in `vi` editor:
- Open the file:
bash
vi five_lines.txt
a) Move to the first line:
- Command:
bash
gg
- Result: Moves the cursor to the first line of the file.
b) Move to the last line:
- Command:
bash
G
- Result: Moves the cursor to the last line of the file.
c) Delete the current line:
- Command:
bash
dd
- Result: Deletes the current line where the cursor is placed.
d) Undo the previous action:
- Command:
bash
u
- Result: Restores the deleted line by undoing the last action.
e) Redo the previous action:
- Command:
bash
Ctrl + r
- Result: Repeats the last action (re-does the undo operation).
f) Save and exit:
- Command:
bash
:wq
- Result: Saves the file and exits the `vi` editor.
Here are the shell scripts for the tasks you requested:
Set C:
1. Write a shell script to display the lines that are not matched with the
specified search string pattern.
You can use the `grep` command with the `-v` option, which inverts the
match to display lines that do not contain the specified pattern.
Script: `exclude_lines.sh`
bash
#!/bin/bash
# Check if the correct number of arguments is provided
if [ $# -ne 2 ]; then
echo "Usage: $0 <pattern> <filename>"
exit 1
fi
PATTERN=$1
FILENAME=$2
# Check if the file exists
if [ ! -f "$FILENAME" ]; then
echo "File not found!"
exit 1
fi
# Display lines that do not match the specified pattern
grep -v "$PATTERN" "$FILENAME"
How to Use:
- Save the script as `exclude_lines.sh`.
- Give execute permission:
bash
chmod +x exclude_lines.sh
- Run the script:
bash
./exclude_lines.sh "search_string" filename.txt
---
2. Write a shell script to copy all files from the source directory to the destination
directory.
You can use the `cp` command to copy files from one directory to another.
Script: `copy_files.sh`
bash
#!/bin/bash
# Check if the correct number of arguments is provided
if [ $# -ne 2 ]; then
echo "Usage: $0 <source_directory> <destination_directory>"
exit 1
fi
SOURCE_DIR=$1
DEST_DIR=$2
# Check if the source directory exists
if [ ! -d "$SOURCE_DIR" ]; then
echo "Source directory does not exist!"
exit 1
fi
# Check if the destination directory exists, if not create it
if [ ! -d "$DEST_DIR" ]; then
mkdir -p "$DEST_DIR"
fi
# Copy files from source to destination directory
cp "$SOURCE_DIR"/* "$DEST_DIR"
echo "Files copied from $SOURCE_DIR to $DEST_DIR."
How to Use:
- Save the script as `copy_files.sh`.
- Give execute permission:
bash
chmod +x copy_files.sh
- Run the script:
bash
./copy_files.sh /path/to/source /path/to/destination
Here are the shell scripts for the tasks outlined in Set A, Set B, and Set C:
Set A:
1. Shell Script to Check Whether Two Numbers are Same or Different
bash
#!/bin/bash
echo "Enter two numbers: "
read num1 num2
if [ $num1 -eq $num2 ]; then
echo "The numbers are the same."
else
echo "The numbers are different."
fi
2. Shell Script to Check Whether a Given Number is Odd or Even
bash
#!/bin/bash
echo "Enter a number: "
read number
if [ $((number % 2)) -eq 0 ]; then
echo "$number is even."
else
echo "$number is odd."
fi
3. Shell Script to Display Time-Based Greetings
bash
#!/bin/bash
hour=$(date +"%H")
if [ $hour -lt 12 ]; then
echo "Good Morning"
elif [ $hour -lt 18 ]; then
echo "Good Afternoon"
else
echo "Good Evening"
fi
4. Shell Script to Test File Type
bash
#!/bin/bash
echo "Enter the file name: "
read filename
if [ -b "$filename" ]; then
echo "$filename is a block device."
elif [ -c "$filename" ]; then
echo "$filename is a character device."
elif [ -f "$filename" ]; then
echo "$filename is a normal file."
else
echo "$filename is not a valid file type."
fi
5. Shell Script to Convert File Contents to Uppercase
bash
#!/bin/bash
echo "Enter the file name: "
read filename
if [ -f "$filename" ]; then
tr '[:lower:]' '[:upper:]' < "$filename" > "upper_$filename"
echo "Converted $filename to uppercase and saved as upper_$filename."
else
echo "File does not exist."
fi
6. Shell Script to Create Directory if it Doesn't Exist
bash
#!/bin/bash
echo "Enter directory name: "
read dirname
if [ ! -d "$dirname" ]; then
mkdir "$dirname"
echo "Directory $dirname created."
else
echo "Directory $dirname already exists."
fi
---
Set B:
1. Shell Script for Directory Operations Based on Argument
bash
#!/bin/bash
if [ "$1" == "current" ]; then
pwd
elif [ "$1" == "parent" ]; then
pwd/..
elif [ "$1" == "root" ]; then
ls /
else
echo "Invalid argument. Use 'current', 'parent', or 'root'."
fi
2. Shell Script to Display Contents of Files with Given Extension
bash
#!/bin/bash
echo "Enter the file extension (e.g., txt): "
read extension
if ls *.$extension 1> /dev/null 2>&1; then
cat *.$extension
else
echo "No files with the .$extension extension found."
fi
3. Shell Script to Move Files by Extension
bash
#!/bin/bash
echo "Enter the file extension (e.g., .txt): "
read extension
if [ ! -d "$extension" ]; then
mkdir "$extension"
fi
mv *"$extension" "$extension"/
echo "Moved all files with the $extension extension to the $extension directory."
4. Shell Script to Display File Details if It Exists
bash
#!/bin/bash
echo "Enter the file name: "
read filename
if [ -f "$filename" ]; then
ls -l "$filename"
else
echo "File $filename does not exist."
fi
5. Shell Script to Check if Two Files are Same and Delete Second if They Are
bash
#!/bin/bash
if [ $# -ne 2 ]; then
echo "Usage: $0 file1 file2"
exit 1
fi
if cmp -s "$1" "$2"; then
echo "Files are the same. Deleting $2."
rm "$2"
else
echo "Files are different."
fi
6. Shell Script to Check File Permissions
bash
#!/bin/bash
echo "Enter the file name: "
read filename
if [ -r "$filename" ]; then
echo "$filename is readable."
else
echo "$filename is not readable."
fi
if [ -w "$filename" ]; then
echo "$filename is writable."
else
echo "$filename is not writable."
fi
if [ -x "$filename" ]; then
echo "$filename is executable."
else
echo "$filename is not executable."
fi
---
Set C:
1. Shell Script to Compute Gross Salary
bash
#!/bin/bash
echo "Enter basic salary: "
read basic
if [ $basic -lt 1500 ]; then
HRA=$(echo "scale=2; $basic * 0.10" | bc)
DA=$(echo "scale=2; $basic * 0.90" | bc)
else
HRA=500
DA=$(echo "scale=2; $basic * 0.98" | bc)
fi
gross_salary=$(echo "scale=2; $basic + $HRA + $DA" | bc)
echo "Gross Salary: $gross_salary"
2. Shell Script to Display Lines Between Given Line Numbers
bash
#!/bin/bash
if [ $# -ne 3 ]; then
echo "Usage: $0 filename start_line end_line"
exit 1
fi
sed -n "$2,$3p" "$1"
3. Shell Script to Check Number of Digits
bash
#!/bin/bash
echo "Enter a number: "
read number
case $number in
[0-9])
echo "The number is a single digit."
;;
[1-9][0-9])
echo "The number is a two-digit number."
;;
[1-9][0-9][0-9])
echo "The number is a three-digit number."
;;
*)
echo "The number has more than three digits or is not a valid number."
;;
esac
Here are the menu-driven shell scripts for the tasks outlined:
4. Menu-Driven Program for Arithmetic Operations
bash
#!/bin/bash
while true; do
echo "Select an operation:"
echo "1. Addition (+)"
echo "2. Subtraction (-)"
echo "3. Multiplication (*)"
echo "4. Division (/)"
echo "5. Exit"
read -p "Enter your choice: " choice
if [ "$choice" -eq 5 ]; then
echo "Exiting the program."
break
fi
read -p "Enter first number: " num1
read -p "Enter second number: " num2
case $choice in
1)
result=$(echo "$num1 + $num2" | bc)
echo "Result: $result"
;;
2)
result=$(echo "$num1 - $num2" | bc)
echo "Result: $result"
;;
3)
result=$(echo "$num1 * $num2" | bc)
echo "Result: $result"
;;
4)
if [ "$num2" -ne 0 ]; then
result=$(echo "scale=2; $num1 / $num2" | bc)
echo "Result: $result"
else
echo "Division by zero is not allowed."
fi
;;
*)
echo "Invalid choice. Please select again."
;;
esac
done
---
5. Menu-Driven Program for Date, Time, and System Operations
bash
#!/bin/bash
while true; do
echo "Select an option:"
echo "1. Show today's date and time"
echo "2. Show files in current working directory"
echo "3. Show calendar"
echo "4. Start editor to write letters"
echo "5. Exit"
read -p "Enter your choice: " choice
case $choice in
1)
echo "Today's date and time: $(date)"
;;
2)
echo "Files in current directory:"
ls -l
;;
3)
echo "Current month's calendar:"
cal
;;
4)
echo "Starting the text editor (nano):"
nano
;;
5)
echo "Exiting the program."
break
;;
*)
echo "Invalid choice. Please select again."
;;
esac
done
---
6. Menu-Driven Program for File and Directory Operations
bash
#!/bin/bash
while true; do
echo "Select an option:"
echo "1. Create directory"
echo "2. Create file"
echo "3. Display contents of file"
echo "4. Copy file into another file"
echo "5. Display files in directory"
echo "6. Exit"
read -p "Enter your choice: " choice
case $choice in
1)
read -p "Enter directory name: " dirname
mkdir "$dirname"
echo "Directory '$dirname' created."
;;
2)
read -p "Enter file name: " filename
touch "$filename"
echo "File '$filename' created."
;;
3)
read -p "Enter file name to display: " filename
if [ -f "$filename" ]; then
cat "$filename"
else
echo "File '$filename' does not exist."
fi
;;
4)
read -p "Enter source file name: " srcfile
read -p "Enter destination file name: " destfile
if [ -f "$srcfile" ]; then
cp "$srcfile" "$destfile"
echo "Copied '$srcfile' to '$destfile'."
else
echo "Source file '$srcfile' does not exist."
fi
;;
5)
echo "Files in current directory:"
ls -l
;;
6)
echo "Exiting the program."
break
;;
*)
echo "Invalid choice. Please select again."
;;
esac
done
---
How to Run the Scripts
1. Copy each script into a separate file (e.g., `arithmetic.sh`,
`system_operations.sh`, `file_operations.sh`).
2. Make the scripts executable:
bash
chmod +x arithmetic.sh system_operations.sh file_operations.sh
3. Run the scripts:
bash
./arithmetic.sh
./system_operations.sh
./file_operations.sh
ASSI 9
Here are the shell scripts for the tasks outlined in Set A, Set B, and Set C:
Set A:
1. Find Factorial of a Number
bash
#!/bin/bash
read -p "Enter a number: " num
factorial=1
for (( i=1; i<=num; i++ )); do
factorial=$((factorial * i))
done
echo "Factorial of $num is $factorial"
2. Find Sum of Digits
bash
#!/bin/bash
read -p "Enter a number: " num
sum=0
while [ $num -gt 0 ]; do
digit=$((num % 10))
sum=$((sum + digit))
num=$((num / 10))
done
echo "Sum of digits is $sum"
3. Print Multiplication Table Using Command Line Arguments
bash
#!/bin/bash
if [ $# -ne 1 ]; then
echo "Usage: $0 <number>"
exit 1
fi
num=$1
for (( i=1; i<=10; i++ )); do
echo "$num * $i = $((num * i))"
done
4. Sum of Command Line Arguments Greater Than 10
bash
#!/bin/bash
sum=0
for num in "$@"; do
if [ "$num" -gt 10 ]; then
sum=$((sum + num))
fi
done
echo "Sum of numbers greater than 10 is $sum"
5. Power of First Number Raised to Second Number
bash
#!/bin/bash
if [ $# -ne 2 ]; then
echo "Usage: $0 <base> <exponent>"
exit 1
fi
base=$1
exponent=$2
result=1
for (( i=0; i<exponent; i++ )); do
result=$((result * base))
done
echo "$base raised to the power of $exponent is $result"
6. Display First 10 Odd Numbers and Their Sum
bash
#!/bin/bash
sum=0
count=0
num=1
echo "First 10 odd numbers are:"
while [ $count -lt 10 ]; do
echo $num
sum=$((sum + num))
num=$((num + 2))
count=$((count + 1))
done
echo "Sum of first 10 odd numbers is $sum"
---
Set B:
1. Print Numbers 1-10 with Even or Odd
bash
#!/bin/bash
for (( num=1; num<=10; num++ )); do
if [ $((num % 2)) -eq 0 ]; then
echo "$num is even"
else
echo "$num is odd"
fi
done
2. Display All *.conf Files in /etc Directory
bash
#!/bin/bash
echo "Configuration files in /etc directory:"
ls /etc/*.conf 2>/dev/null
3. Display Days of the Week with Labels
bash
#!/bin/bash
for day in {1..7}; do
case $day in
1) echo "Day $day : Mon (weekday)" ;;
2) echo "Day $day : Tue (weekday)" ;;
3) echo "Day $day : Wed (weekday)" ;;
4) echo "Day $day : Thu (weekday)" ;;
5) echo "Day $day : Fri (weekday)" ;;
6) echo "Day $day : Sat (WEEKEND)" ;;
7) echo "Day $day : Sun (WEEKEND)" ;;
esac
done
4. Report Number of Lines in Each File in Current Directory
bash
#!/bin/bash
for file in *; do
if [ -f "$file" ]; then
line_count=$(wc -l < "$file")
echo "$file: $line_count lines"
fi
done
5. Print Command Line Arguments in Reverse Order
bash
#!/bin/bash
echo "Arguments in reverse order:"
for (( i=$#; i>=1; i-- )); do
echo "${!i}"
done
6. Print Each Entry in a Directory with Size or Item Count
bash
#!/bin/bash
if [ -d "$1" ]; then
for entry in "$1"/*; do
if [ -f "$entry" ]; then
size=$(du -h "$entry" | cut -f1)
echo "$entry: $size"
elif [ -d "$entry" ]; then
count=$(ls -1 "$entry" | wc -l)
echo "$entry: $count items"
fi
done
else
echo "Provided argument is not a directory."
fi
---
Set C:
1. List Empty Subfolders
bash
!/bin/bash
if [ -d "$1" ]; then
find "$1" -type d -empty > empty_subfolders.txt
echo "List of empty subfolders saved to empty_subfolders.txt"
else
echo "Provided argument is not a directory."
fi
2. Delete All Empty Subfolders
bash
#!/bin/bash
if [ -d "$1" ]; then
find "$1" -type d -empty -delete
echo "All empty subfolders deleted from $1"
else
echo "Provided argument is not a directory."
fi
3. Remove Duplicate Lines from a File
bash
#!/bin/bash
if [ -f "$1" ]; then
awk '!seen[$0]++' "$1" > temp && mv temp "$1"
echo "Duplicate lines removed from $1"
else
echo "File does not exist."
fi
4. Delete Lines Containing a Specified Word
bash
#!/bin/bash
if [ $# -lt 2 ]; then
echo "Usage: $0 <word> <file1> [<file2> ...]"
exit 1
fi
word=$1
shift
for file in "$@"; do
if [ -f "$file" ]; then
sed -i "/$word/d" "$file"
echo "Lines containing '$word' deleted from $file"
else
echo "$file does not exist."
fi
done
5. Menu for File Actions
bash
#!/bin/bash
if [ ! -f "$1" ]; then
echo "File does not exist."
exit 1
fi
while true; do
echo "Select an option:"
echo "1. Display file contents"
echo "2. Display file size in blocks"
echo "3. Display number of words in file"
echo "4. Display last five lines of the file"
echo "5. Display first ten lines of the file"
echo "6. Exit"
read -p "Enter your choice: " choice
case $choice in
1) cat "$1";;
2) ls -s "$1";;
3) wc -w < "$1";;
4) tail -n 5 "$1";;
5) head -n 10 "$1";;
6) echo "Exiting."; exit;;
*) echo "Invalid choice.";;
esac
done
6. Menu for System Information
bash
#!/bin/bash
while true; do
echo "Select an option:"
echo "1. Display the number of users logged in"
echo "2. Display the login ID of users logged in"
echo "3. Display the present working directory"
echo "4. Display the home directory of the logged-in user"
echo "5. Display the PATH"
echo "6. Exit"
read -p "Enter your choice: " choice
case $choice in
1) who | wc -l;;
2) who | awk '{print $1}';;
3) pwd;;
4) echo $HOME;;
5) echo $PATH;;
6) echo "Exiting."; exit;;
*) echo "Invalid choice.";;
esac
done
How to Run the Scripts
1. Copy each script into a separate file (e.g., `factorial.sh`, `sum_of_digits.sh`,
etc.).
2. Make the scripts executable:
bash
chmod +x filename.sh
3. Run the scripts:
bash
./filename.sh