Q:- Write a shell script to mount and unmount devices using shell scripting. Also display the list of devices currently available to the system.
Code
#!/bin/bash function mntdev { echo "Currently mounted devices are: " lsblk } function pcidev { echo "Following are the PCI devices: " lspci } function usbdev { echo "Following are the USB devices" lsusb } function mountfunc { echo "Enter the name of the device to be mounted " read devicename echo "Enter the directory present in Home for the destination" read directoryname mkdir $directoryname mount /dev/$devicename $directoryname echo "The device is mounted" cd $dirname ls cd .. } function unmountfunc { lsblk echo "Enter the path for unmounting" read path umount -v $path echo "The device has been unmounted" } while [ true ] do echo "Please Enter a Choice" echo "0]List the currently mounted devices" echo "1]List PCI devices" echo "2]List connected USB devices" echo "3]Mount a device" echo "4]Unmount a device" echo "5]Exit" read ch case $ch in 0)mntdev ;; 1)pcidev ;; 2)usbdev ;; 3)mountfunc ;; 4)unmountfunc ;; 5) echo "Terminating script" break ;; *)echo "Invalid choice entered" ;; esac done
Output
[root@localhost
student]# sh asgn5.sh
Please
Enter a Choice
0]List the
currently mounted devices
1]List PCI
devices
2]List
connected USB devices
3]Mount a
device
4]Unmount
a device
5]Exit
0
Currently
mounted devices are:
NAME
MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda
8:0 0 149G 0 disk
├─sda1
8:1 0 28.8G 0 part
├─sda2
8:2 0 1K 0 part
├─sda3
8:3 0 500M 0 part /boot
├─sda4
8:4 0 67.2G 0 part
│
├─vg-lv_swap (dm-0) 253:0 0 3.9G 0 lvm [SWAP]
│
├─vg-lv_root (dm-1) 253:1 0 42.2G 0 lvm /
│
└─vg-lv_home (dm-2) 253:2 0 21.1G 0 lvm /home
└─sda5
8:5 0 29.3G 0 part
sr0
11:0 1 1024M 0 rom
sdb
8:16 1 7.6G 0 disk
└─sdb1
8:17 1 7.6G 0 part
/run/media/student/MUNAGEKAR1
Please
Enter a Choice
0]List the
currently mounted devices
1]List PCI
devices
2]List
connected USB devices
3]Mount a
device
4]Unmount
a device
5]Exit
1
Following
are the PCI devices:
00:00.0
Host bridge: Intel Corporation 82G33/G31/P35/P31 Express DRAM
Controller (rev 0a)
00:01.0
PCI bridge: Intel Corporation 82G33/G31/P35/P31 Express PCI Express
Root Port (rev 0a)
00:02.0
VGA compatible controller: Intel Corporation 82G33/G31 Express
Integrated Graphics Controller (rev 0a)
00:02.1
Display controller: Intel Corporation 82G33/G31 Express Integrated
Graphics Controller (rev 0a)
00:1b.0
Audio device: Intel Corporation N10/ICH 7 Family High Definition
Audio Controller (rev 01)
00:1c.0
PCI bridge: Intel Corporation N10/ICH 7 Family PCI Express Port 1
(rev 01)
00:1d.0
USB Controller: Intel Corporation N10/ICH 7 Family USB UHCI
Controller #1 (rev 01)
00:1d.1
USB Controller: Intel Corporation N10/ICH 7 Family USB UHCI
Controller #2 (rev 01)
00:1d.2
USB Controller: Intel Corporation N10/ICH 7 Family USB UHCI
Controller #3 (rev 01)
00:1d.3
USB Controller: Intel Corporation N10/ICH 7 Family USB UHCI
Controller #4 (rev 01)
00:1d.7
USB Controller: Intel Corporation N10/ICH 7 Family USB2 EHCI
Controller (rev 01)
00:1e.0
PCI bridge: Intel Corporation 82801 PCI Bridge (rev e1)
00:1f.0
ISA bridge: Intel Corporation 82801GB/GR (ICH7 Family) LPC Interface
Bridge (rev 01)
00:1f.1
IDE interface: Intel Corporation 82801G (ICH7 Family) IDE Controller
(rev 01)
00:1f.2
IDE interface: Intel Corporation N10/ICH7 Family SATA IDE Controller
(rev 01)
00:1f.3
SMBus: Intel Corporation N10/ICH 7 Family SMBus Controller (rev 01)
02:00.0
Ethernet controller: Broadcom Corporation NetLink BCM5784M Gigabit
Ethernet PCIe (rev 10)
Please
Enter a Choice
0]List the
currently mounted devices
1]List PCI
devices
2]List
connected USB devices
3]Mount a
device
4]Unmount
a device
5]Exit
2
Following
are the USB devices
Bus 001
Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002
Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 003
Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 004
Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 005
Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 004
Device 002: ID 413c:2003 Dell Computer Corp. Keyboard
Bus 004
Device 003: ID 0461:4d81 Primax Electronics, Ltd
Bus 001
Device 007: ID 8564:1000
Please
Enter a Choice
0]List the
currently mounted devices
1]List PCI
devices
2]List
connected USB devices
3]Mount a
device
4]Unmount
a device
5]Exit
4
NAME
MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda
8:0 0 149G 0 disk
├─sda1
8:1 0 28.8G 0 part
├─sda2
8:2 0 1K 0 part
├─sda3
8:3 0 500M 0 part /boot
├─sda4
8:4 0 67.2G 0 part
│
├─vg-lv_swap (dm-0) 253:0 0 3.9G 0 lvm [SWAP]
│
├─vg-lv_root (dm-1) 253:1 0 42.2G 0 lvm /
│
└─vg-lv_home (dm-2) 253:2 0 21.1G 0 lvm /home
└─sda5
8:5 0 29.3G 0 part
sr0
11:0 1 1024M 0 rom
sdb
8:16 1 7.6G 0 disk
└─sdb1
8:17 1 7.6G 0 part
/run/media/student/MUNAGEKAR1
Enter the
path for unmounting
/run/media/student/MUNAGEKAR1
Bash Scripting |
The device
has been unmounted
Please
Enter a Choice
0]List the
currently mounted devices
1]List PCI
devices
2]List
connected USB devices
3]Mount a
device
4]Unmount
a device
5]Exit
3
Enter the
name of the device to be mounted
sdb1
Enter the
directory present in Home for the destination
newpendrive
asgn2source+out.doc
div32.asm latest asgn3 mpal My Games Pendrive Backup
STUDENT PROFILE.docx WRITEUPS
Asgn3
entertainment Latestosal mpal total backup My Movies
Resume.doc study
ASSIGNMENTS
hellos.py MozillaFirefox mul32.asm My Videos ssl
to print
Please
Enter a Choice
0]List the
currently mounted devices
1]List PCI
devices
2]List
connected USB devices
3]Mount a
device
4]Unmount
a device
5]Exit
5
Terminating
script
[root@localhost
student]#
No comments:
Post a Comment