Java beauty [from cainiao to expert drills] using shell scripts in Linux (1), javashell

Source: Internet
Author: User
Tags switch case

Java beauty [from cainiao to expert drills] using shell scripts in Linux (1), javashell
Author: erqing

Personal site: zhangerqing.cn mail: xtfggef@gmail.com Weibo: http://weibo.com/xtfggef


Shell scripts combine multiple commands and write them together to implement multi-functional text through computation and judgment. To learn how to use shell, you must first perform common system management operations and perform them automatically. If you use shell scripts to centralize these operations, you only need to execute this shell script on a regular basis. Secondly, for some complicated text processing, such as log analysis, there may be a lot of requirements. It is not very convenient to execute a single command, therefore, you also need to use shell. Therefore, if you want to learn about linux, shell is a required course. In this chapter, we mainly use bash shell instead of other shells.


HelloWorld: the first shell script that outputs Hello World.



The first line must be #! /Bin/bash (declare that we use bash shell, otherwise the system does not know the shell to be called to execute the script ). Start with #. All subsequent characters are ignored during execution. The main body is the echo-e "Hello World \ a \ n" of the 4th rows.-e indicates that the backslash escape is enabled, \ a indicates that the sound of Doon is output, and \ n indicates the line feed. After writing the script, add the executable permission chmod + x sh01.sh to the script, and then run it with./sh01.sh.



Interactive script

The simplest case is to obtain user input and then output it.



This example is also relatively simple. read is used to obtain the input and assign the value to the variable that follows. The execution result is as follows:



Numerical Calculation

Bash shell only supports simple addition, subtraction, multiplication, and division of integers. $ (express) is required, and express in two parentheses is the calculated expression.



In the seventh row, declare is used to declare a variable sum and $ () is used to calculate the result.



Simplified

1. test command

Check whether the file exists: test-e filename & echo "Exist" | "Not exist". If yes, Exist is output. Otherwise, the Not exist. test command has many parameters.

Command Parameters Explanation Remarks
Test-e filename
Test -E Whether the file name exists Common
  -F Whether the file name exists and is a file Common
  -D Whether the directory name exists and is a directory Common
  -B Whether the file name exists and is a block device  
  -C Whether the file name exists and is a character device  
  -S Whether the file name exists and is a Socket Device  
  -P Whether the file name exists and is a FIFO file  
  -L Whether the file name exists and is a connection file  
File Permission detection, such as test-r filename
  -R Whether the file name exists and has the read permission  
  -W Whether the file name exists and has the write permission  
  -X Whether the file name exists and has executable permissions  
  -U Whether the file name exists and has the SUID attribute  
  -G Whether the file name exists and has the SGID attribute  
  -K Whether the file name exists and has the Sticky bit attribute  
  -S Whether the file name exists and is not a blank File  
For comparison between two files, test file1-nt file2
  -Nt Newer than, determine whether file1 is newer than file2 Common
  -Ot Older than, determine whether file1 is older than file2  
  -Ef Determine whether file1 and file2 are the same file  
Test n1-eq n2
  -Eq The two values are equal.  
  -Ne Not equal to two values  
  -Gt Greater than, n1 greater than n2  
  -Lt Less than, n1 less than n2  
  -Ge Greater than or equal, n1 is greater than or equal to n2  
  -Le Less than or equal, n1 less than or equal to n2  
Judge string
  Test-z string Determines whether the string is 0.  
  Test-n string Judge whether the string is not 0  
  Test str1 = str2 Judge whether the string is equal  
  Test str1! = Str2 Determines whether the string is not equal  
Multi-condition determination, test-r filename-a-x filename
  - Returns true if both conditions are set at the same time.  
  -O Returns true when any condition is set.  
  ! Returns true if the condition is invalid.  

2. [] Operator

In addition to test, we can also use [] (brackets) for some judgment, such as determining whether the HOME environment variable is null:

[-Z $ HOME]; echo $?

Note that the space in the command must be followed by a space before [later]. If = is used, a space (either or none) is required for both sides ). For example, ["aaa" = "bbb"], this is a reasonable method, if ["aaa" = "bbb"] or ["aaa" = "bbb"] is incorrect, an error is returned. When using [] for judgment, pay attention to the following points:

A. Components in brackets [] must be separated by spaces.

B. Enclose variables in parentheses in double quotation marks.

C. It is best to enclose constants in parentheses in single or double quotation marks.


3. Default variables of shell script $0, $1, $2...

When we run a script with parameters, we can obtain some information about the command in the script, such as: sh01.sh aaa bbb, in the script, $0 indicates the Script Name. $1 and $2 are the first and second parameters respectively, and so on. We can summarize them as follows:

$0, Script Name

$1, $2, Script Parameters

$ #, Number of parameters

$ @: All parameters. Each parameter is enclosed in double quotation marks.

$ *, All parameters, separated by Spaces

For example:



Running result:



Conditional Criterion

This is a common if else statement. In the shell script, if then is used to represent the if statement, if then else is used to represent the if else statement, and if is used to end with fi, else if is replaced by elif. Let's look at an example:

1. Check whether the first input parameter is hello.

2. If no parameter exists, the user is prompted to enter a parameter

3. If the input parameter is not "hello", the system prompts the user to enter "hello" as the parameter only.



View results:



Case judgment

We have used switch case statements in programming languages, which are much more convenient than if else in some cases. in shell scripts, there are similar statement blocks called case in esac, in the above example, we use another method:



See the results:



Loop

1. while do... done

This is our common while loop. When the condition is met, the statements in the loop body are known to be executed until the condition is not met and the cycle is introduced. done indicates the end of the loop.





2. until do... done

This statement is the opposite of while do... done. When the condition is not met, the loop is executed and the loop ends when the condition is met.


3. for do... done

A common for loop is to traverse a known array or structure cyclically.



For do... done can also be used to process numerical values. We can use the traditional for loop method: ((;;))





Tracking and debugging of shell scripts

Before executing a shell script, we can first perform a series of syntax checks, which can initially detect some obvious problems.

Sh [-nvx] script. sh

-N: Check the syntax only if no script is executed.

-V: output the script to the screen before executing the script.

-X: displays the script used on the screen to list all execution processes.


This chapter is about to end. It mainly involves some basic shell script syntax Compiling. We can familiarize ourselves with this chapter and practice more, then, let's look at some complex text processing examples, learn more, imitate the writing of complex scripts, and then write complicated scripts.

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.