Introduction to several things about shell Programming

Source: Internet
Author: User
Article Title: three tips on shell program design. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
   I. if condition statements 
Format:
If condition expression
Then # execute the following statement when the condition is true
Command list
Else # execute the following statement when false
Command list
Fi
  
If statements can also be nested
  
If condition expression 1
Then
If condition expression 2
Then
Command list
Else
If condition expression 3
Then
Command list
Else
Command list
Fi
Fi
Else
Command list
Fi
  
You can nest an if statement in multiple layers. it must end with a fi to indicate the condition of the layer. otherwise, syntax errors may occur. the preceding example is as follows:
Here we first talk about the command test used in a condition statement to test whether the condition after test is true.
  
If test-f "$1"
Then
Lpr $1
Else
If test-d "$1"
Then
Cd $1
Lpr $1
Else
Echo "$1 is not a file or directory"
Fi
Fi
  
The above example can also be changed to the following:
  
If test-f "$1"
Then
Lpr $1
Elif test-d "$1" # elif is the same as else if
Then
(Cd $1; lpr $1)
Else
Echo "$1 is not a file or directory"
Fi ??????
  
Do you understand the above examples?
If we save this example as prfile
Chmod + x prfile
Execute the program
./Prfile aaa
  
In this example, check whether your input parameter is a file. if it is a file, print it. if it is a directory, convert it to a directory and then print it. if it is not a file or a directory, a prompt is given.
  
   II. Multi-condition test statement case
Format:
Case string in
Mode) command list ;;
Mode) command list ;;
....
Esac
  
The multi-condition statement starts from case and ends with esac. Multiple condition lists can be used to test whether the string matches the pattern in it, the command list mode in execution can also be "*" to represent any string, and the last point in each mode is the heart; double quotation marks are used to end; otherwise, a syntax error occurs.
  
The following is an example:
  
Case $1 in
*. C)
Cc $1
;;
*. Txt)
Lpr $1
;;
*)
Echo "unknown type"
Esac
  
If you save the preceding content in the abc file
  
Chmod + x abc
Execute./abc a. c to compile the file a. c.
Execute./abc readme.txt and the file will be passed through the printer
If I change the preceding content, do you know the execution result?
  
Case $1 in
*)
Cc $1
;;
*. Txt)
Lpr $1
;;
*. C)
Echo "unknown type"
Esac
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.