Script Exercise 2

Source: Internet
Author: User

Tag: Shell

Exercise: write a script to complete the following functions:

1. List all disk devices on the current system;

2. Select a disk device and display all the partition information on the specified device;

3. Ask the user if they want to erase all existing partitions on the disk and then re-Add the three partitions;

Y or yes: continue

N or no: Abort the script

Other characters remind the user to re-enter valid characters

4. Execute the corresponding partition operation after the user selects Yes

Create three partitions

Primary partition 1: 512 M, ext4

Primary partition 2: 512 M, swap

Primary partition 3: 2g, ext4

5. format the created partitions as described above as the corresponding file system;

6. Mount primary partition 1 to the/mnt/boot directory, and primary partition 3 to the/mnt/sysroot directory;


Extension: After the above 3rd steps start, check whether any partition on the device is mounted. If yes, uninstall it first;


#! /Bin/bash #################################### ###################################### file name: 002.sh# Author: lookback # Email: [email protected] # created time: fri 11 Jul 2014 05:43:44 pm CST ################################# ######################################## bootdir = "/mnt/Boot" sysroot = "/mnt/sysroot" clearecho "The following disk is available on the system, select a disk for change. "Echo $ (fdisk-L | grep-oe '^ disk [[: Space:] +/dev/[S | H] d [A-Z] '| grep-oe'/dev/[S | H] d [A-Z] '| sort-u) read-P "Enter the full name of a disk device (for example,/dev/sdb ): "sdnoclearwhile [-z" $ (echo "$ sdno" | grep-oe '^/dev/[S | H] d [A-Z] $') "]; doecho "The following disk is available on the system. Select a disk to change it. "Echo $ (fdisk-L | grep-o '^/dev/[S | H] d [A-Z]' | sort-u) read-P" input error, plz input agian: "sdnodoneclear & Echo" $ The following table lists the partition information of an sdno Disk: "parted $ sdno print #====================================== ============================ cat <EOF the script program will partition, format, and mount the disk $ sdno. Next the operation may cause loss of all your data. Do you want to continue? If you want to continue, enter y | y, if you want to exit, enter n | n. eofread-P "Enter your option:" nextuntil [["$ next" = ~ [Yynn]; doclearread-P "input error, plz input agian: "nextdoneif [" $ next "=" Y "-o" $ next "=" Y "]; thenmountcheck = 'mount | grep-oe "^ $ sdno [0-9] +" 'If [-n "$ mountcheck"]; thenclearecho "detects that $ sdno has Mount partitions, the script is uninstalling these partitions and deleting them ...... "For I in 'echo" $ mountcheck "'; do umount $ I; sleep 1; donewhile! Mke2fs-T ext4-F $ {sdno} &>/dev/NULL; do DMSetup remove_all; donefiecho-e "n \ NP \ N1 \ n + 512m \ NN \ NP \ N2 \ n + 512m \ NN \ NP \ N3 \ n + 2G \ NT \ N2 \ n82 \ nw "| fdisk $ {sdno} &>/dev/nullfor I in $ (fdisk-L $ sdno | grep-oe" ^ $ {sdno }[ 0-9] + "); do while [-n "$ (CAT/proc/Partitions | grep-e" $ I ")"]; dopartx-A $ sdno & kpartx-AF $ sdno; sleep 1 donedonewhile! Mke2fs-T ext4 $ {sdno} 1 &>/dev/NULL; do DMSetup remove_all; donemke2fs-T ext4 $ {sdno} 3 &>/dev/nullmkswap $ {sdno} 2 [-d "$ bootdir"] | mkdir-p $ bootdir [-d "$ sysroot "] | mkdir-p $ sysroot [-z" $ (Mount | grep "$ bootdir ") "] | umount $ bootdir [-z" $ (Mount | grep "$ sysroot ") "] | umount $ sysrootmount $ {sdno} 1 $ bootdirmount $ {sdno} 3 $ sysroot # Swapon $ {sdno} 2 &>/dev/nullecho" execution ended... "fi


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.