Get the value of the expression

The shell gets the value of the expression, usually using backquotes. The following is an example:

cat 1.txt | while read line do pid=`echo $line | awk '{print $1}'` dt=`echo $line | awk '{print $2,$3}'` echo `clickhouse-client --multiline -h ip --port port --database database --query="select count(*), sum(money), count(distinct open_id) from v_match_base_fix_table_8kw where pid='$pid' and dt <='$dt';" ` $pid>>2.txt doneCopy the code

Read the document 1.txt, and get the values of the first column and the second and third columns separately, and pass them to the SQL statement as parameters, and then save the results of SQL query and pid value in the document 2.txt

Two, conditional judgment expression

2.1. Basic syntax of if:

if [ condition ]&&[ condition ]&&[ condition ]; then
    command
elif [ condition ]; then
    command
else
    command
fi
Copy the code

2.2. Determine files/folders

[-b FILE] True if FILE exists and is a block-specific FILE. [-c FILE] True if FILE exists and is a one-word special FILE. [-d DIR] True if FILE exists and is a directory. [-e FILE] True if FILE exists. [-f FILE] True if FILE exists and is a common FILE. [-g FILE] True if FILE exists and SGID has been set. [-k FILE] True if the FILE exists and the sticky bit has been set. [-p FILE] True if FILE exists and is a name pipe (F if O). [-r FILE] True if the FILE exists and is readable. [-s FILE] True if the FILE exists and the size is not 0. [-t FD] True if the file descriptor FD is open and points to a terminal. [-u FILE] True if FILE exists and SUID (set user ID) is set. [-w FILE] True if the FILE exists and is writable. [-x FILE] True if the FILE exists and is executable. [-o FILE] True if FILE exists and belongs to a valid user ID. [-g FILE] True if the FILE exists and belongs to a valid user group. [-l FILE] True if FILE exists and is a symbolic link. [-n FILE] True if FILE exists and has been mod If IED since it was last read. [-s FILE] True if FILE exists and is a socket. [file1-nt FILE2] This is true if FILE1 has been changed more recently than FILE2, or if FILE1 exists and FILE2 does not. [file1-ot FILE2] True if FILE1 is older than FILE2 or FILE2 exists but FILE1 does not exist. [FILE1 -ef FILE2] True if FILE1 and FILE2 point to the same device and node number.Copy the code

2.3 string judgment

[-z STRING] If the length of a STRING is zero, it is true. [-n STRING] If the length of a STRING is non-zero, it is true. [STRING1 = STRING2] True if two strings are the same; [STRING1!= STRING2] True if the strings are different; [STRING1] True if the string is not empty, similar to -nCopy the code

2.4. Numerical judgment

Int1-eq INT2 If INT1 and INT2 are equal,= int1-ne INT2 if INT1 and INT2 are not equal,<> int1-gt INT2 if INT1 is greater than INT1,> int1-ge INT2 <</div> INT1 -le INT2 </div> INT1 -le INT2 </div> INT1 -le INT2 </div> INT2Copy the code

2.5. Complex logic judgment

With && # # - a - o or | | #! nonCopy the code

2.6, the sample

if [ "$stimes" -ge "100" ]&&[ "$money" -ge "10000" ]&& [ "$people" -ge "50" ]; then
    echo $pid >>success.txt
else
    echo $pid >>failed.txt
fi
Copy the code

Shell if [[]] and [] the difference between | | &&

[] and the test

They are the same, test expr and [expr] have the same effect on the command line. The three basic functions of test are to judge files, to judge strings, and to judge integers. Supports concatenation of expressions using “and or not”. The only comparison operators available in test are == and! =, both are always string comparison, the supplement is used for integer comparison, integer after all can only use -eq,-gt form. Never use greater-than-less-than signs in either string or integer comparisons. Of course, if you really want to, the corresponding string can escape Angle brackets, after all, if you compare “ab” to “BC” : [ac \< BC], the result is true, which returns status 0.

[[]]

This is a command built into the shell and is much more powerful than test. Support for pattern matching of strings (and even shell regular expressions when using the =~ operator). Logical combination can not use the test – a, – o and use &&, | |. String comparisons can use the right-hand string as a pattern (this is if the right-hand string is not quoted). If the string on the right is double quoted, it is considered a literal string), not just a string, such as [[hello == hell?]], which is true. [[1==2]] = “false”; [[1==2]] = “true”;

Let and (())

The two are the same (or almost the same, with double parentheses slightly weaker than let). Mainly for arithmetic operations (neither of the above works), but also for integer comparisons, using the familiar <, > and other comparison operators. Situations where you can use a variable name like var without needing $var. Support multiple expression of a semicolon # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

  1. First of all, although they are similar, conceptually they are at different levels. [[, is the keyword, many shells (ash, BSH) do not support this, KSH, bash(reportedly introduced support for [[from 2.02 onwards), etc.

  2. The [[]] structure is more generic than the bash version []. All characters between [[and]] are not separated by file extensions or tokens, but are replaced by parameter references and commands. Test structure with [[…]] than with […] It also prevents many logic errors in the script. For example, &&, | |, <, and > operator can be in a [[]] through the test, but there was an error in [] structure.

  3. The (()) structure extends and evaluates an arithmetic expression. If the expression value is 0, it returns 1 or false as the exit status code. A non-zero expression returns a 0 or true as an exit status code. This structure is the opposite of the previous discussion of the test command and the [] structure.

  4. [[…]] is a shell command, so the expressions in it should be its command-line arguments, so the string comparison operators > and < must be escaped or become the IO redirect operators. There is no need to escape < and > in [[.

  5. [[…]], while […] Don’t do

  6. [[… &&… &&…]] and [… -a… -a…] No, [[]] is a logical short-circuit operation, and [] does not exercise logic

Test in KSH

You can use “let”, “(())”, and “+”, “-“, “*”, “/”, and “%” to operate numbers. It is not recommended to use “expr”

reference

www.cnblogs.com/liudianer/p…