当前位置:网站首页>Shell learning notes (latest update: 2022-02-18)

Shell learning notes (latest update: 2022-02-18)

2022-06-25 12:38:00 haoming Hu

Article reference PHP Chinese net

1. shell brief introduction

shell It's a command line interpreter , Parse user commands into the actual execution of the operating system , Realize the interaction between users and operating system , When you need to repeat several commands , You can assemble commands , Add certain control statements , Become shell Script files , hand shell Batch execution

2. first demo

  • create a file vi test.sh

  • Write output statement

    echo "hello world"
    echo "first demo"
    
  • Authorize scripts chmod +x ./test1.sh

  • Execute the script ./test1.sh

  • Output

3. shell Variable

3.1 Variable definitions

There can be no spaces between variable names and equal signs , This may not be the same as any programming language you are familiar with . meanwhile , The naming of variable names should follow the following rules :

  • The first character must be a letter (a-z,A-Z).
  • No spaces in between , You can use underscores (_).
  • You can't use punctuation .
  • Out of commission bash Keywords in ( You can use help Command to view reserved keywords ).
myname="huhaoming"

3.2 Use defined variables

Use a defined variable , Just precede the variable name with a dollar sign , Such as :

myname="huhaoming"
echo $your_name
echo ${your_name}  #{} not essential , Curly braces are used to help the interpreter identify the boundaries of variables 

Variables defined , Can be redefined

myname="tom"
echo $myname
myname="alibaba"
echo $myname

A read-only variable use readonly modification

myname="tom"
readonly myname
myname="huhaoming"  

Delete variables

Use unset Command to delete variables , Variable cannot be used again after being deleted .unset Command cannot delete read-only variables .

echo "huhaoming hello world"
myname="huhaoming"
readonly myname
echo $myname
unset myname
echo $myname

echo "huhaoming hello world"
myname="huhaoming"
echo $myname
unset myname
echo $myname

3.3 shell character string

Restrictions on single quoted string :

  • Any character in a single quotation mark will be output as is , Variables in a single quoted string are invalid ;
  • A single quotation mark cannot appear in a single quotation mark string ( You can't escape a single quotation mark ).

The advantages of double quotes :

  • You can have variables in double quotes
  • Escape characters can appear in double quotes
echo "huhaoming hello world"
myname="huhaoming"
str="Hello, I know your are \"$myname\"! \n"
echo $str

String concatenation

echo "huhaoming hello world"
myname="huhaoming"
greeting="hello, "$myname" !"
greeting_1="hello, ${myname} !"
echo $greeting $greeting_1

Get string length

echo "huhaoming hello world"
myname="huhaoming"
echo ${
    #myname}

Extract substring

echo "huhaoming hello world"
myname="huhaoming"
echo ${myname:1:4} 

3.4 Array

bash Supports one dimensional array ( Multidimensional arrays are not supported ), And there's no limit to the size of the array . Similar to C Language , The subscripts of array elements are 0 Numbered starting . To get the elements in an array, you need to use subscripts , Subscripts can be integers or arithmetic expressions , Its value should be greater than or equal to 0.

Define an array

stay Shell in , Use parentheses to represent arrays , For array elements " Space " Symbol split . The general form of defining an array is : Array name =( value 1 value 2 … value n)

array_name=(value0 value1 value2 value3)

Individually define

array_name[0]=value0
array_name[1]=value1
array_name[n]=valuen

Read array

${ Array name [ Subscript ]}

Use @ The symbol can get all the elements in the array , for example :

echo ${array_name[@]}

The length of the array

#  Get the number of array elements 
length=${
    #array_name[@]}
#  perhaps 
length=${
    #array_name[*]}
#  Get the length of a single element of an array 
lengthn=${
    #array_name[n]}
echo "huhaoming hello world"
myname=(0,1,2,3,4)
echo ${
    #myname[*]}
echo ${
    #myname[@]}
echo ${
    #myname[1]}

4. Shell Pass parameters

In execution Shell Script time , Pass parameters to script , The format of obtaining parameters in the script is :$n.n Represents a number ,1 For executing the first parameter of the script ,2 For executing the second parameter of the script , And so on ……, among $0 For the filename of the execution :

echo "huhaoming hello world"
echo " File name of execution :$0"
echo " The first parameter is zero :$1";
echo " The second parameter is :$2";
echo " The third parameter is zero :$3";

A few special characters are used to handle parameters :

Processing parameters explain
$# The number of parameters passed to the script
$* Display all the parameters passed to the script in a single string . Such as "$*“ use 「”」 In a nutshell 、 With "$1 $2 … $n" Output all parameters in the form of .
$$ The current process the script runs ID Number
$! Of the last process running in the background ID Number
[email protected] And ∗ phase Same as , but yes send use when Add lead Number , and stay lead Number in return return Every time individual ginseng Count . Such as " * identical , But use quotation marks , And return each parameter in quotation marks . Such as " phase Same as , but yes send use when Add lead Number , and stay lead Number in return return Every time individual ginseng Count . Such as "@“ use 「”」 In a nutshell 、 With "$1" “ 2 " … " 2" … " 2""n” Output all parameters in the form of .
$- Show Shell Current options used , And set Same command function .
$? Display the exit status of the last command .0 No mistakes , Any other value indicates an error .

5. shell Operator

                            |

| @ ∣ And @ | And @ And * identical , But use quotation marks , And return each parameter in quotation marks . Such as "[email protected]“ use 「”」 In a nutshell 、 With "$1" “ 2 " … " 2" … " 2""n” Output all parameters in the form of . |
| $- | Show Shell Current options used , And set Same command function . |
| $? | Display the exit status of the last command .0 No mistakes , Any other value indicates an error . |

5. shell Operator

原网站

版权声明
本文为[haoming Hu]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202200528576207.html

随机推荐