Shell O & M automation if-read in today's course, we will learn to judge user output parameters. Example: If you speak ill of others? That's the case. Story 2: When you eat snacks, when the cat calls you, you hear it, and then you divide the snacks in your hands into a piece for the kitten. After the kitten gets the snacks, and you are gone. Obviously, this cat is a loan. Let's split the story and implement it. Let the cat go and let you come. In this story, listening, splitting, and leaving are the keywords that the hero heard. Then, the response is divided into a few points for the cat. Then the cat gets the result and goes through 2 and 1 read shell: read reads a line of user input from the command line. According to programming, the most commonly used is: read-p "your description" ppp meaning: Output a prompt on the command line for user input waiting, then the variable is defined as ppp (the name can be obtained at will. The good habit is that the variable name must be meaningful. For example, why is watermelon not called potato? Because this is common sense.) After the user inputs the data, the user reads the data into the variable ppp. What should we do then? Use this variable. 2.1.sh #! /Bin/sh # code by scpman # read-p "What's your Name:" name echo "Hi," $ name echo "now Time: 'date' "in this script, We prompt the user to enter the name, then output hi, user output name, and output the current system time in the script. 2. 2 if in 2.1, we have received input from the user, then you give the cat a dog, will the cat eat? At any time, we do not trust user input! The cat has the principle that giving a dog is not eating, but eating a mouse (the hero is eating snacks). If you give a mouse, the cat will eat it. Otherwise, it will not eat. Then use if to understand the Syntax: if .... then... if fi is for a mouse, eat if... else .... if fi is not a mouse, but something else, If .... elif... elif .... else... if fi is for a mouse, or for a biscuit, it also uses some fixed expressions to determine some problems: list some: conditional expression file expression if [-f file] if the file exists if [-d...] if the directory contains if [-s file] if the file exists and is not empty if [-r file] if the file exists and is readable if [-w file] if the file exists and can be written if [- x file] if the file exists and an integer variable expression can be executed if [int1-eq int2] if int1 is equal to int2 if [int1-ne int2] if not equal to if [int1-ge int2] if> = If [int1-gt int2] if> if [int1-le int2] If <= if [int1-lt int2] if <string variable expression if [$ a = $ B] If string1 is equal to string2. A string can use an assigned value as the equal sign. if [$ string1! = $ String2] if string1 is not equal to string2 if [-n $ string] if string is not empty (not 0), 0 (true) is returned) if [-z $ string] if string is empty if [$ sting] if string is not empty, the return value is 0 (similar to-n). The conditional expression references a variable with $, and it is best to add "$ variable name" (obtained through the experience of failure) 2.3 instance 1. If the mouse is given, the cat will think about the possible situation: a. If you just waved your hand and didn't give anything to B. If you didn't give A mouse C, if you gave A mouse, you threw it to someone else #! /Bin/sh # code by scpman # cat's food read-p "Please input the cat's food: "food if [-n" $ food "] then echo $ food fi is output if there is input, exit if there is no input, and prompt the user to enter nothing. If you get the input, make a judgment. If you give it to a mouse, say thank you. If it is not a mouse, don't, and prompt the user # vi 2.3.2.sh #! /Bin/sh # code by scpman # cat's food read-p "Please input the cat's food: "food if [-n" $ food "] then echo the food: $ food else echo You give cat a null fi if ["$ food" = 'lastu'] then echo 3Q, cat love laoshu elif ["$ food" = 'pig'] then echo sorry, cat not love pig else echo cat not like others! Fi # sh 2.3.2.sh Please input the cat's food: You give cat a null # Should I prompt You to exit directly after it is null? Cat not like others! # How can this sentence be output? Shell: exit the script # vi 2.3.3.sh #! /Bin/sh # code by scpman # cat's food read-p "Please input the cat's food: "food if [-n" $ food "] then echo the food: $ food else echo You give cat a null exit ### here, # indicates the comment, in this way, when the condition is followed, the system prompts and exits fi if ["$ food" = 'lastu'] then echo 3Q, cat love laoshu elif ["$ food" = 'pig'] then echo sorry, cat not love pig else echo cat not like others! Fi # sh 2.3.3.sh Please input the cat's food: You give cat a null this time on the right # sh 2.3.3.sh Please input the cat's food: pig the food: pig sorry, cat not love pig scpman # sh 2.3.3.sh Please input the cat's food: lkjdslfldsf the food: lkjdslfldsf cat not like others! Scpman # sh 2.3.3.sh Please input the cat's food: laoshu the food: laoshu 3Q, cat love laoshu can't be used, of course, so many lines can be written, all the examples in this book, don't ask how many lines are there. You only need clear thinking and hope you can write less NB code (only a few times you want to understand the code !) Let's take a useful example! 2.4 small instance: judge whether the/usr/test directory is absent. If not, output the file. After creating this directory, read the/etc/passwd file, find all the users in the file and create the file programming corresponding to the username under/usr/test/: first understand what you want to do, then sip your saliva, think about your ideas, and then start in one breath. Layman: Good. This requirement is simple. I have finished writing it in a few minutes. Then I will repeat the request and repeat it. The keyboard is also dignified. # vi 2.4.sh #! /Bin/sh # code by scpman # mkdir, touch file mulu = "/usr/test" if [-d "$ mulu"] then echo $ mulu is have else mkdir $ mulu echo $ mulu create OK fi for username in 'cat /etc/passwd | grep-v "^ #" | awk-F ': ''{print $1}'' do cd $ mulu touch $ mulu/$ username done ls-l $ The for loop section in the mulu script will be discussed tomorrow # sh 2.4.sh/usr/test create OK total 0-rw-r -- 1 root wheel 0 Jan 20 07:42 _ dhcp-rw-r -- 1 root wheel 0 Jan 20 07:42 _ pflogd-rw-r -- r -- 1 root wheel 0 Jan 20 07:42 bin-rw-r -- 1 root wheel 0 Jan 20 07:42 bind-rw-r -- 1 root wheel 0 Jan 20 07:42 daemon -rw-r -- 1 root wheel 0 Jan 20 07:42 games-rw-r -- 1 root wheel 0 Jan 20 07:42 kmem-rw-r -- 1 root wheel 0 Jan 20 07:42 mailnull-rw-r -- 1 root wheel 0 Jan 20 07:42 man-rw-r -- 1 root wheel 0 Jan 20 07:42 news-rw-r -- r -- 1 root wheel 0 Jan 20 07:42 nobody-rw-r -- 1 root wheel 0 Jan 20 07:42 operator-rw-r -- 1 root wheel 0 Jan 20 07:42 pop -rw-r -- 1 root wheel 0 Jan 20 07:42 proxy-rw-r -- 1 root wheel 0 Jan 20 07:42 root-rw-r -- 1 root wheel 0 Jan 20 07:42 scpman-rw-r -- 1 root wheel 0 Jan 20 07:42 smmsp-rw-r -- 1 root wheel 0 Jan 20 07:42 sshd-rw-r -- r -- 1 root wheel 0 Jan 20 07:42 toor-rw-r -- 1 root wheel 0 Jan 20 07:42 tty-rw-r -- 1 root wheel 0 Jan 20 07:42 uucp -rw-r -- 1 root wheel 0 Jan 20 07:42 www