当前位置:网站首页>A quick start to Shell Scripting

A quick start to Shell Scripting

2020-11-09 14:29:18 Zhu Zilong 2018

1.#! The beginning of the script

#!/bin/bash

2. Script properties

Add executable properties ,chmod +x Or use “.” function , For example, run the current directory of a.sh Executable command “. ./a.sh” The file format should be unix, stay linux There is no problem with the file created under , stay windows Write the script to pay special attention to the file format View file format vim Open file set ff View file format set ff=type Format file ,type Is the file format

3. Variable

Letters start with an underline , It can be followed by letters, underscores and numbers assignment : a=1 There can be no spaces on either side of the equal sign ; Cancel variables unset Using weak variables , There can be no type , If you don't define a type, treat it as a string , If you don't add local modification , Then it's a global variable , Works across the entire script use export You can export script variables as environment variables Positional arguments $0 For the script name $n For the script's section n Parameters $? Command and script return values $* or $# Indicates the number of command line parameters A read-only variable readonly

4. Array

Definition :a=('1' '2'); a The value of is enclosed in brackets , Members are enclosed in single quotation marks , Members are separated by spaces Assign member assignment a=([2]=1 [3]=2); quote :${a[1]} ${array[@]} ${array[*]} Get the entire array element Same output , but @ The result is the elements separated by spaces ,* The output is an entire string Array truncation :${array[@]:1:2} Take the first to second elements of the array Linked array c=(${a[@]} ${b[@]}) Replacement elements c=(${c[@]/str1/str2})

5. Paraphrase and quote

\ Using escape characters makes \ The following characters are used only as characters " " Part quote , In part of the quotation $ `( The quotation marks ) \ It's still going to be interpreted as having a special meaning ' ' Full quote , There are many characters in full references, which are just ordinary characters

6. Command substitution

Assign the standard output of the command as an assignment to a variable Use (`) or $() $() Only in bash shell With effective With date Command as an example DATE=`date' DATE=$(date)

7. Operator

1. Arithmetic operator + - * / % ** ( Power operation )+= -= *= /= %= ++ -- Use let Command calculation let a=1+2 2. An operator , << >> & | ^ ~ Also use let Command to operate 3. Use $[] or $(()) operation echo $[2+3] 4. Use expr operation expr 1 + 1 Ask the operands and operator times to be separated by spaces 5.declare command use declare Declare variable type , You can do it directly later declare -i a; a=1+1; There must be no spaces on either side of the operator 6. Use bc Do floating point operations e=$("scale=3;$a/$b"|bc); scale Set the actual number of floating-point numbers , If it is not set, it will be displayed as an integer result

8. Test and judge

test [ ] Format test expression perhaps [ expression ] Notice that there are spaces on both sides of the expression ; 1. File test -b FILE Returns true if the file exists and is a block device -c FILE Returns true if the file exists and is a character device -d FILE Returns true when the file exists and is a directory -e FILE The existence of a file or directory returns true -f FILE Return true when the file exists and is a normal file -x FILE If it is true, the executable file is returned -w FILE Returns true if the file exists and is writable -r FILE Returns true when the file exists and is readable -l FILE Returns true when the file exists and is a link -p FILE Returns true if the file exists and is a pipe -s FILE File exists and size is not 0 Back to true -S FILE The file exists and is socket Back to true -g FILE The file exists and is set to SGID Back to true -u FILE The file exists and is set to SUID Back to true -k FILE The file exists and is set to sticky Back to true -G FILE True is returned when the file exists and belongs to a valid user group -O FILE True is returned when the file exists and belongs to a valid user FILE1 -nt FILE2 When file 1 Than file 2 New time returns to true FILE1 -ot FILE2 When file 1 Than file 2 It's true to return to the old days

2. String test -z “string” Returns true when the string is empty -n “string” Returns true when the string is not empty “string1”= “string2” The same returns true “string1”!= “string2” Different return true “string1” > “string2” Dictionary sort ,string1 be ranked at string2 Back before, really “string1”< “string2” Dictionary sort ,string1 be ranked at string2 And then back to true 3. Integer comparison "num1" -eq "num2" equal "num1" -gt "num2" Greater than "num1" -lt "num2" Less than "num1" -ge "num2" Greater than or equal to "num1" -le "num2" Less than or equal to "num1" -ne "num2" It's not equal to 4. Logic tester !expression Not expression1 -a expression2 And expression1 -o expression2 or 5. Logical operators ! Logic is not , Take the opposite of true and false && Logic and , Join two expressions , Only when both expressions are true , The result is true || Logic or

9. Logical structure The branch of judgment

1.if

// The basic structure 
if expression;then
    command1
    command2
fi

//if/else structure 
if expression;then
    command1
    command2
else
    command3
fi

//if/elif/else structure 
if expression;then
    command
elif expression2;then
    command
fi

2.case

case VAR in
var1) command;;
var2) command;;
var3) command;;
*)command;;
esac

10. Logical structure loop

1.for

for VARIABLE in (list) // If the variable list is a continuous number , It can be written as  {1..n} $(seq 1 n)  perhaps $(seq 1 2 100)  From 1 To 100, Each time 2, namely 1 To 100 The odd number 
do
    command
done

// class C Of for loop 
for((expression1;expression2;expression))
do
        command
done

2.while

while expression
do
    command
done

//while  Dead cycle 
while (1)
while true
while

3.until

unitl wxpression
do
    command
done
//until Dead cycle 
until ((0))
until false

4.slect And single parameter for be similar , It can also be used. if case Realization

select varible in (list)
do
    command
done

5. Cycle control break continue break One by one break, Jump out of current loop ,break n, Jump out of the present n Layer cycle continue End the current cycle , Enter next cycle ,continue n, End the present n Layer cycle ,

11. function

1. Definition

function FUNCTION_NAME()       // keyword function It can be omitted 
{ 
    command1;
    command2;
    return n;                // When a function has a return value , adopt $? To receive the return value 
}

2. Function parameter passing Even global variables , Not passed to the function , Functions cannot be used , contrary , If the variable inside the function is not declared local, Then the variables inside the function can be used in the whole script Function transfer parameter form

  function $a

版权声明
本文为[Zhu Zilong 2018]所创,转载请带上原文链接,感谢