Shell Scripting Exercises

Source: Internet
Author: User

Title requirements:

Write a script to enter personal information/root/bin/information.sh, the requirements are as follows:

1. Prompt the user to enter username (need to check the name of the character, the user name can not be blurred contains any of the system is already in the existence of the user name, such as cannot be HELLOROOT1,123BIN,GGNTP2. and requires username to start with a capital letter, at least 5 characters in length.

2. Prompts the user to enter the gender, only can enter male or female, input other prompts the user to retry.

3. Prompt the user to enter a birthday, the format must be YYYY-MM-DD, and the legality of the date to check, if you enter 2018-02-30 and other than the reality of the date, the date is wrong , please try again.

4. Prompt the user to enter the mobile phone number, the length can only 11 bits, and must start with 13x,18x,15x,17x, if the error prompts the user to re-enter.

5. Prompt the user to enter the ID number, the length can be 18 bits, the first 17 digits of the format are numbers. The contents of 7th to 14th are required to match the birthday of the 3rd step. At the same time, if the number of the 17th digit is odd, then the 2nd person should be male, whereas the even should be female. The last 1 bits can be numbers or X.

6. Enter the above information in: As a delimiter, save to/root/info.txt, each time you enter a user, will be appended a row.

such as zhangsan:male:1999-12-20:13588291219:4101011999122030051


  1 #!/bin/bash  2 read -p  "Please enter username: "  username   3  #提示用户输入username (need to check the name of the character, the user name can not be blurred contains any one of the system is already in existence   4  #要求username要以大写字母开头, length of at least 5 characters   5 cat /etc/passwd |cut -d: -f1 > /root/passwd  6  while read line  7 do  8     if [[   $username  != *${line}* ]] && [[  $username  == [[:upper:]] ???? * ]] ;then  9         continue 12          exit 1 13     fi 14  done < /root/passwd 15  16  #提示用户输入性别, enter male or female only, enter additional prompts for user retry   17 while read -p  "Please enter gender: "  gender 18 do 19      if [  $gender  == male ] | |  [  $gender  == female ] ;then 20          break 21     else 22          echo  "Please retry"  23         continue 24      fi 25 done 26  27  #提示用户输入生日, the format must be YYYY-MM-DD, And check the legality of the date, if you enter the date of 2018-02-30 and other discrepancies with the reality, 28  #则提示日期错误, please retry  29 while read -p  " Please enter a birthday (YYYY-MM-DD):  " birthday 30 do 31          if [[  $birthday  == [1-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] ]]  && [[  $birthday  <  ' date +%f '  ]]  && cal $ (echo  $birthday  |sed -r  ' s/(. *)-(. *)-(. *)/\3 \2 \1/')  &> /dev/null ;then  32         break 33      else 34         echo  "Date error, Please re-enter " 35         continue 36      fi 37 done 38  39  #提示用户输入手机号, the length can only 11 bits, and must start with 13x,18x,15x,17x, if the error prompts the user to re-enter  40 while read -p  "Please enter phone number: "  telnum 41 do 42      if [[  $telnum  == 1[3578]x[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9] ] ] ;then 43         break 44      else 45         echo  "Please re-enter"  46          continue 47     fi 48  done 49  50 #Prompts the user to enter the social security number, the length can be 18 bits, the format first 17 digits is the number. The contents of 7th to 14th are required to match the birthday of the 3rd step. At the same time requires 17th  51  #所填的数字若为奇数, then the 2nd step to fill in the gender should be male, and vice versa should be female. The last 1 digits can be a number or x 52 read -p  "Please enter your ID number: "  ID 53 case  $gender  in  54     male)  55         if  [[  $ID  == [0-9][0-9][0-9][0-9][0-9][0-9] ' echo  $birthday  |tr -d  '-' [ 0-9][0-9][13579][0-9x] ]] ;then 56              echo  "input correct"  57         else 58              echo  "entered the wrong ID number"  59              exit 3 60          fi 61         ;;  62 &nbsP;   female)  63         if [[  $ID  == [0-9][0-9][0-9][0-9][0-9][0-9] ' echo  $birthday  |tr -d  '-' [0-9][0-9][02468][ 0-9x] ]] ;then 64              echo  "input correct"  65         else 66              echo  "The ID number entered is incorrect"  67              exit 3 68          fi 69         ;;  70     *)  71         echo   "Gender mismatch"  72         exit 4 73          ;;  74 esac 75  76  #将以上输入的信息以: As a delimiter, save to/root/info.txt, each time you enter a user, a row will be appended  77  cat << eof >> /root/info.txt 78  $username: $gender: $birthday: $telnum: $ID  79 eof 80 rm -f /root/passwd


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.