linux shell script knowledge

Recent projects, a task linux shell script to write. Because before I am not very familiar face in the process a lot harder to find a lot of information, but also gain a lot. The following is a summary of common knowledge linux shell script.

1 basic grammar

1.1 Variable

Use a variable defined earlier, as long as the dollar plus sign in front of variable name
your_name="qinjx"
echo $your_name
echo ${your_name}

1.2 String

String in single quotes, double quotes may be used, may be practiced without quotes.

apostrophe

Any characters in single quotes are directly output variables in single quoted strings are invalid;
single quoted string can not appear in single quotes (single quotes nor escape after use).

Double quotes

Double quotes can have variable

Double quotes can appear escape character

1.3 pass parameters

$# It represents the number of script execution arguments passed

$* Displays all the parameters passed to the script to a single string.

Such as "$ *" with "" "enclosed in the case, the form of" $ 1 $ 2 ... $ n "output of all parameters.

$$ It indicates that the script is running the current process ID number

$@ $ * The same, but the use of quotation marks, and returns each parameter in quotation marks.

Such as "$ @" with "" "enclosed in the case, the form of" $ 1 "" $ 2 "..." $ n "output of all parameters.

$0 That the implementation of the script name

$1 Indicates that the first parameter

$?Displays exit status of the last command. 0 means no error, any other value indicates an error.

1.4 basic operators

Native bash does not support simple mathematical operations, but can be achieved through other commands, such as awk and expr, expr most commonly used.

expr is an expression calculation tool, use it to complete the evaluation of an expression operation.

val=`expr 2 + 2`

The conditional expression to be placed between square brackets, and have a space, for example: [$ a == $ b] is wrong and must be written [$ a == $ b].

Multiplication sign (*) must be added in front of a backslash () can be achieved multiplication;

if ... then ... fi is conditional statements, follow-up will be covered.

expr syntax in the MAC shell is: $ ((expression)), where the expression "*" symbol does not need to escape "."

2 commonly used commands

2.1printf command

printf command syntax:

printf format-string [arguments...]

printf "%-10s %-8s %-4s\n" 姓名 性别 体重kg

% S% c% d% f format are placeholders

%-10s Refers to a width of 10 characters (- represents a left alignment, right alignment indicates that there is no), no characters are displayed in 10 characters wide characters, if space is insufficient to fill automatically, over all displayed content will also .

%-4.2f It refers to the decimal format, wherein the means .2 2 decimal places.

2.2test command

test command is used to check if a condition is established, it can be tested three values, character and files.

Files expression

-e filenameIf filename exists, True
-d filenameif the filename is a directory, True
-f filenameif the filename is a regular file, True
-L filenameif the filename is a symbolic link, True
-r filenameif readable filename True
-w filenameif filename can write True
-x filenameif executable filename true
-s filenameif file size is not 0, true
-h filenameif file is a soft link, true
filename1 -nt filename2if filename1 than filename2 new true.
filename1 -ot filename2If filename1 older than filename2 True.

String variable expression

If [ $a = $b ]If string1 string2 equals True (string allows the use of an assignment made equal)
if [ $string1 != $string2 ], if not equal string1 string2 True
if [ -n $string ]if non-empty string (non-zero), and returns 0 (to true)
if [ -z $string ]if the string is empty, True
if [ $sting ]If the string is not empty, return 0 (-n and the like)

2.3read command

-n (no line breaks) -p (precautionary statements) -n (number of characters) -t (waiting time) -s (no echo)
, such as:

read -p "Enter your name:" name

3 compression and decompression

Decompression: tar -zxvf FileName.tar
Compression:tar -czvf FileName.tar DirName

3.1 five independent command

Compression decompression have to use one of them, but can be used in conjunction with other command can only use one of them.

-c: Establish compressed file
-x: Extract
-t: View content
-r: append files to the archive
-u: update of the original file compression package

3.2 five optional parameters

Needed alternative when compressed or decompressed file.
-z: There gzip attribute
-j: attribute has bz2
-Z: There compress attribute
-v: show all process
-O: the extracted files to standard output

3.3 a Required parameter

-f: Use the file name, remember, this parameter is the last parameter, behind only access the file name

Guess you like

Origin www.cnblogs.com/leton/p/11895964.html