Linux FAQ 2

Source: Internet
Author: User

Linux FAQ 2 1. The for/do/done Shell script has a very different for loop structure from the C language. It is similar to the foreach loop in some programming languages. Example :#! /Bin/sh for FRUIT in apple banana pear; do echo "I like $ FRUIT" doneFRUIT is a circular variable. The first round of $ FRUIT is apple, and the second round is banana, the third value is pear. For another example, you need to change the names of chap0, chap1, and chap2 in the current directory to chap0 ~, Chap1 ~, Chap2 ~ ~ The file name of the character indicates a temporary file). This command can be written as follows: $ for FILENAME in chap ?; Do mv $ FILENAME ~; Done can also write: $ for FILENAME in 'ls chap? '; Do mv $ FILENAME ~; The usage of done2, while/do/done while is similar to that of C language. For example, a password verification Script :#! /Bin/sh echo "Enter password:" read TRYwhile ["$ TRY "! = "Secret"]; do echo "Sorry, try again" read TRYdone the following example controls the number of cycles through arithmetic operations :#! /Bin/sh COUNTER = 1 while ["$ COUNTER"-lt 10]; do echo "Here we go again" COUNTER =$ ($ COUNTER + 1 )) doneShell also has the until loop, similar to do in C language... while loop. This chapter is omitted. 3. Many special variables such as location parameters and special variables are automatically assigned by Shell. We have met $? And $1. Now, the common location parameters and special variables $0 are equivalent to argv [0] $1, $2 in C main function... these are called Positional Parameter, which is equivalent to argv [1] and argv [2] of the C main function... $ # is equivalent to the argc-1 of the C main function. Note that # is not followed by a comment $ @ indicating the parameter list "$1" "$2 "..., for example, it can be used after in a for loop. $? The Exit Status $ parameter of the previous command can be left shifted using the shift command. For example, shift 3 indicates that the original $4 is changed to $1, and the original $5 is changed to $2. The original $1, $2, and $3 are discarded, and $0 is not moved. The shift command without parameters is equivalent to shift 1. Example :#! /Bin/sh echo "The program $0 is now running" echo "The first parameter is $1" echo "The second parameter is $2" echo "The parameter list is $ @ "shiftecho" The first parameter is $1 "echo" The second parameter is $2 "echo" The parameter list is $ @ "4. The function is similar to The C language, shell also has the function concept, but the function definition does not return values or has no parameter list. Example :#! /Bin/sh foo () {echo "Function foo is called ";} echo "-= start =-" fooecho "-= end =-" NOTE: The left curly braces of the function body {There must be spaces or line breaks between the left curly braces and the subsequent commands, if you write the last command and the right curly braces} In the same line, the command must end with a; sign. When defining the foo () function, you do not execute commands in the function body. Just like defining variables, you just need to define the foo name, when the foo function is called later (note that no parentheses are required for function calling in Shell), the command in the function body is executed. Functions in Shell scripts must be defined and called first. Generally, function definitions are written before the script, and function calls and other commands are written at the end of the script (similar to the main function in C language, this is where the entire script actually starts executing the command ). If a Shell function does not have a list of parameters, it does not mean that parameters cannot be passed. In fact, a function is like a mini script. When a function is called, any parameter can be passed, $0, $1, and $2 are also used in the function to extract parameters. The location parameter in the function is equivalent to the local variable of the function, changing these variables does not affect $0, $1, $2, and other variables outside the function. You can use the return command to return a function. If return is followed by a number, it indicates the Exit Status of the function. The following script creates multiple directories at a time. Each directory name is passed in through the command line parameter. The script tests each directory one by one to check whether the directory exists. If the directory does not exist, print the information and try to create the directory. #! /Bin/sh is_directory () {DIR_NAME = $1 if [! -D $ DIR_NAME]; then return 1 else return 0 fi} for DIR in "$ @"; do if is_directory "$ DIR" then: else echo "$ DIR doesn' t exist. creating it now... "mkdir $ DIR>/dev/null 2> & 1 if [$? -Ne 0]; then echo "Cannot create directory $ DIR" exit 1 fi fidone Note: is_directory () returns 0, indicating that true return 1 indicates false.

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.