Shell Scripting Exercises

Source: Internet
Author: User

1, write a script to complete the following functions

(1) Pass a disk device file path to the script to determine whether the device exists;

(2) If present, displays all the partition information on this device;

#!/bin/bash## finddev.sh# pass a disk device file path to the script to determine if this device exists # If present, displays all partition information on this device if [$#-ge 1];then for dev in [email protected] ;d o If [-B $dev];then fdisk-l $dev else errdev= $errdev "" $dev fi Done [[$ (Echo $errdev | WC-C)-GT 1]] && echo-e "\n============ The following devices do not exist ==============\n$errdev" else echo "Please enter an existing disk device" fi


Script Test

[[email protected] script]# bash finddev.sh  Please enter an existing disk device [[email protected]  script]# bash finddev.sh /dev/adb /dev/ma============ the following devices do not exist ============== /DEV/ADB  /dev/ma [[email protected] script]# bash finddev.sh /dev/sdadisk /dev /sda: 85.9 gb, 85899345920 bytes255 heads, 63 sectors/track, 10443  cylindersUnits = cylinders of 16065 * 512 = 8225280  bytessector size  (logical/physical):  512 bytes / 512 bytesi/o size   (minimum/optimal):  512 bytes / 512 bytesdisk identifier: 0x0004d0da    Device Boot      Start          end      blocks   id  system/ Dev/sda1   *           1           64      512000   83  linuxpartition 1  does not end on cylinder boundary./dev/sda2               64       10444     83373056   8e  linux lvm


2, write a script to complete the following functions

Pass a parameter to the script, this parameter is one of gzip, bzip2 or XZ;

(1) If the value of parameter 1 is gzip, use the TAR and gzip archive to compress/etc directories into the/backups directory and name/backups/etc-20160613.tar.gz;

(2) If the value of parameter 1 is bzip2, use tar and bzip2 archive to compress/etc directory to/backups directory, and named/backups/etc-20160613.tar.bz2;

(3) If the value of parameter 1 is XZ, then the tar and XZ archives are used to compress/etc directories into the/backups directory and named/backups/etc-20160613.tar.xz;

(4) Any other value, the error compression tool is displayed, and an abnormal exit is performed;

3. Write a script that accepts a path parameter:

(1) If it is an ordinary document, it can be accessed normally;

(2) If it is a catalog file, then the CD command is available for it;

(3) If the file is a symbolic link, the description is a access path;

(4) Other is impossible to judge;

4, write a script, get the host name of the current host, judge

(1) If the host name is empty or localhost, or "(none)", it is named mail.magedu.com;

(2) Otherwise, display the existing host name can be;

5. Write a script to complete the following tasks:

(1) Sequentially copy each direct file or subdirectory in the/var/log directory to the/TMP/TEST1-TESTN directory, respectively;

(2) The Cp-r command is used only when copying the directory;

(3) use CP command when copying files;

(4) Use the cp-d command when copying a linked file;

(5) All remaining types, using the CP-A command;

6. Please describe in detail the start-up process of the CentOS system (detailed to what each process system does)

7. Add a new piece of hardware to the CentOS 6 running on the virtual machine, providing two primary partitions;

(1) Create a new two primary partition for the hard disk, and install grub for it;

(2) Provide kernel and RAMDisk files for the first primary partition of the hard disk, and provide rootfs for the second partition;

(3) for Rootfs to provide bash, LS, cat programs and the library files that depend on;

(4) Provide the configuration file for grub;

(5) The new hard disk is set as the first boot item and can start the target host normally;

8. Write a script

(1) can accept four parameters: Start, stop, restart, status

Start: Output "starting script name finished."

...

(2) Any other parameters, all error exits;

9, write a script to determine whether a given user is logged into the current system;

(1) If the login, the user login, the script terminates;

(2) Every 3 seconds, check whether the user is logged in;

10, write a script, display the user selected to view the information;

CPU) Display CPU Info

MEM) Display Memory info

Disk) Display Disk info

Quit) quit

Not these four options, the prompt error, and ask the user to re-select, only to give the correct choice;

11. Write a script

(1) Return a user's UID and shell with function implementation; The user name is passed through the parameters;

(2) Prompt the user to enter a user name or enter "quit" to exit;

When the user name is entered, the calling function displays the user information;

Exit the script when the user enters quit, and further: after displaying the user-related information you typed, remind the output user name or quit again:

12, write a script to complete the following functions (using functions)

(1) Prompt the user to enter the name of an executable command; Get all the library files that this command relies on;

(2) Copy the command file to the corresponding Rootfs path in the/mnt/sysroot directory, for example, if the original path of the copied file is/usr/bin/useradd, it is copied to the/mnt/sysroot/usr/bin/directory;

(3) Copy the library files that this command relies on to the corresponding Rootfs path in the/mnt/sysroot directory, and the rules related to the above command;


This article is from the "Allen" blog, so be sure to keep this source http://allen05.blog.51cto.com/7743530/1908251

Shell Scripting Exercises

Related Article

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.