Shell's scripting Exercises

Source: Internet
Author: User

make a demand flow chart requirement Analysis-Program Design-specific implementation of training pointssed,Echo, Test,if, while,Shift1. Demand Analysis: Practice Test-d Scheme design: reflect the idea, first define a variable, with the conditions to determine the specific implementation: input: The following [[email protected] --shiyanSH]#Cat>Mytesta=/root/SH/sedif[ -D $a] ThenEcho "$a is a directory"fioutput: correct satisfaction [[email protected] --shiyanSH]#chmodu+x mytest[[email protected] --shiyanSH]# ./mytest/root/SH/sedis a directory2Requirements Analysis: Although the previous step was implemented, there was only one directory, and was dead and inflexible. And if it is not a directory, there is no corresponding output. Scheme design: Replace directory with file, add non-directory output. Specific implementation: Use SED to modify the source file directly on the command line. You can point to:-E, multiple edits to use. Input: below [[email protected] --shiyanSH]#sed-i-e's/sed/mem/'-E'/echo/a\\else echo "$a is a file"'mytest Output: correct satisfaction [[email protected] --shiyanSH]# ./mytest/root/SH/mem is afile3Requirements Analysis: The directory in the script to death, inconvenient, how to go to the command line to pass the file name parameters, so it is convenient. Project design: Concrete implementation: using SED to directly modify the source file in the command line, comment out the first line of input: [[email protected] --shiyanSH]#sed-i-e'1s/a/#&/'-E's/\ $a/\$1/'Mytest[[email protected] --shiyanSH]#Catmytest#a=/root/SH/Memif[-D $1 ] ThenEcho "is a directory"Else Echo "$ is a file"fioutput: correct satisfaction [[email protected] --shiyanSH]#./mytest for foris afile[[Email protected] --shiyanSH]#./mytestawkawkis a directory4Requirements Analysis: If you can test multiple files or directories on the command line, the scheme design: Add a condition to judge each cycle, and output accordingly. Implementation: Using while loop and shift to realize the uncertainty of the number of positional parameters, the main point: until loop and while loop structure is basically the same, but until is to judge the condition expression is false to continue the loop! Difficulty:-N is the $ that is used to test whether a string is empty*is to display all the parameters passed to the script in a single string, which, unlike positional variables, can have more than 9 parameters that contain all the input command-line parameter values. If you run showrpm openssh.rpm w3m.rpm webgrep.rpm at this time $* Contains the3a string, openssh.rpm, w3m.rpm and webgrep.rpm. Input: [email protected] --shiyanSH]#VIMytest1 while[-N"$*" ] Doif[-D $1 ] ThenEcho "is a directory"Else Echo "$ is a file"fiShift Doneoutput: correct satisfaction [[email protected] --shiyanSH]#./mytest1awk  forFrsedselawkis a directory foris afileFR is afilesedIs a directorysel is afile

Shell's 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.