Exercise: write a script (using the function). 1. prompt the user to enter the device file of a disk device. If the device file does not exist, prompt the user to enter the file again until the user inputs the file correctly; after the user gives the correct block device: 1. the user enters the block device and prompts the user that subsequent operations will damage all files on the device, allow the user to choose whether to continue 2. If the user inputs y, continue the subsequent operations; 1. If the user selects y, erase all partitions on the specified block device; 1. Create two primary partitions on the above Disk: (1) 50 m (2) 512m2, all formatted as ext4 file system; 3. Mount to/mnt/boot and/mnt/sysroot1 respectively, and install grub on this device. 2. Create the directories required for the root file system under the/mnt/sysroot directory; 1. Port multiple applications, including at least Bash and ifconfig. 3. If you enter N, the user selects stop and exits the script. 4. Enter any other characters, then let the user re-select; 2. the user can enter quit to exit;
#!/bin/bash########################################################################## File Name: 007.sh# Author: LookBack# Email: [email protected]# Licence: GNU General Public Licence# Created Time: Wed 23 Jul 2014 04:11:57 AM CST#########################################################################mntboot="/mnt/boot"mntsysroot="/mnt/sysroot"Program="bash ifconfig ls df ping"checkInput() {clearuntil [ -n "$(fdisk -l $DiskName)" -a -n "echo $(fdisk -l $DiskName) | grep -o ‘[0-9]‘" ]; doread -p "Plz Enter A Device File Name : " DiskNamedone}printDisk() {clearfdisk -l $DiskNameecho "============================================================================================="echo "==== ===="echo "==== Subsequent actions will damage all the files on the device, ask whether to continue ===="echo "==== If you continue, please enter: y|Y|Yes|YES|yes|yES|yEs|YeS|yeS ===="echo "==== If you do not want to enter the: n|N|No|NO|no|nO ===="echo "==== ===="echo "============================================================================================="echo ""read XDisk}makePartition() {dd if=/dev/zero of=$DiskName bs=512 count=1fdisk $DiskName << EOFnp1+50Mnp2+512MwEOF}mke2fsPartition() {mke2fs -t ext4 ${DiskName}{1,2}}mountPartition() {[ ! -d "$mntboot" ] && mkdir -p "$mntboot" && mount ${DiskName}1 $mntboot[ ! -d "$mntsysroot" ] && mkdir -p $mntsysroot && mount ${DiskName}2 $mntsysroot}installGrub() {grub-install --root-directory=/mnt $DiskName}makeFHSdir() {mkdir {bin,dev,etc,home,lib/modules,lib64,opt,proc,root,sbin,selinux,sys,usr/{bin,sbin,lib,lib64},mnt,media,tmp,var/lib}}transplantProgram() {for I in echo $Program; do[ ! -d ${mntsysroot}$(dirname $I) ] && mkdir -p ${mntsysroot}$(dirname $I)[ ! -f ${mntsysroot}$(which --skip-alias $I) ] && cp $(which --skip-alias $I) ${mntsysroot}$(dirname $I)for II in $(ldd $(which --skip-alias $I) | grep -oE ‘/[^[:space:]]+‘); do[ ! -d ${mntsysroot}$(dirname $II) ] && mkdir -p ${mntsysroot}$(dirname $II)[ ! -f ${mntsysroot}$II ] && cp $II ${mntsysroot}/donedone}nextScript() {printDiskcase $XDisk iny|Y|Yes|YES|yes|yES|yEs|YeS|yeS)makePartitionmke2fsPartitionmountPartitioninstallGrubmakeFHSdirn|N|No|NO|no|nO)clearexit 0 ;;*)nextScriptesac}read -p "Plz Enter A Device File Name : " DiskNamecase $DiskName inq|Q|quit|QUIT)clearexit 0;;*)checkInputnextScriptesac
Shell script Exercise 4