Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the knowledge points of Linux shell?

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

Xiaobian to share with you what Linux shell knowledge points, I hope you have something to gain after reading this article, let's discuss it together!

Input parameters and default variables

For shell scripts, there are things that are specifically designed to handle parameters, and they all have specific meanings, such as:

/home/shouwang/test.sh para1 para2 para3 $0 $1 $2 $3 Script Name *** parameters the third parameter

Where $0 represents the script name to execute,$1,$2 represent ***, and the second parameter, respectively. In addition, there are other default variables, such as:

#represents the number of parameters followed by the script. In the previous example, there are 3 parameters $@ representing all parameters and can be traversed.$* represents all parameters, and as a whole, it is very similar to $*, but the difference is that $$represents the process ID of the current script $? Represents the exit status of the previous command

variables

To assign values to variables, use the equal sign, but do not have spaces on both sides of the equal sign. Strings with spaces on the right side of the equal sign must also be enclosed in quotation marks:

para1="hello world" #String assigned directly to variable para1

Unset is used to cancel variables. For example:

unset para1

How to use variables? When using variables, you need to add $to the variable, for example, to print the contents of the previous para1:

echo "para1 is $para1" #will print para1 is hello world

Or add braces around the variable name:

echo "para1 is ${para1}! " #will print para1 is hello world!

command execution

Executing commands in the shell usually requires just executing them as if they were in a terminal, but that doesn't work if you want the command results printed. Therefore, shell commands are often:

a=`ls` #`is the upper-left ~ key, not single quotes

Or use $, followed by the command executed in parentheses:

echo "current path is $(pwd)" #

In addition, the first two methods are also not feasible for calculating expressions, and the following methods should be adopted:

echo "1+1=$((1+1))" #Print: 1+1=2

That is, the expression to be evaluated is enclosed in double brackets after $.

What if the command to execute is stored in a variable? The previous methods are not feasible, of course, the contents of parentheses are still valid as commands. To do this, for example:

a="ls" echo "$($a)"

However, if the string is multiple commands, the above method is not feasible, and the following method should be adopted:

a="ls;pwd" echo "$(eval $a)"v

This is using eval, which executes the contents of a as a command.

conditional branch

Generally speaking, if the command is executed successfully, the return value is 0, otherwise it is non-0. Therefore, the execution result of the previous command can be judged in the following way:

if [ $? -eq 0 ] then echo "success" elif [ $? -eq 1 ] then echo "failed,code is 1" else echo "other code" fi

The case statement is used as follows:

name="aa" case $name in "aa") echo "name is $name" ;; "") echo "name is empty" ;; "bb") echo "name is $name" ;; *) echo "other name" ;; esac

Beginners should pay special attention to the following points:

[] must be preceded by spaces, which are logical expressions

If elif is followed by then, then the statement to be executed

If you want to print the results of the previous command, *** the way to do this is to put $? Assign to a variable because once a command is executed,$? The value may change.

Each branch of case *** ends with two semicolons, *** is the case written backwards, i.e. esac.

How to use multiple conditions, two ways, way 1:

if [ 10 -gt 5 -o 10 -gt 4 ];then echo "10>5 or 10 >4" fi

Method 2:

if [ 10 -gt 5 ] || [ 10 -gt 4 ];then echo "10>5 or 10 >4" fi

where-o or|| Indicates or. There are also some common conditional judgments.

In summary:

Or, or, or||

-a and with, same as &&

! non

integer judgment:

-eq Are the two numbers equal?

-ne, are the two numbers unequal?

-gt Is the former greater than the latter?

-lt whether the former is less than the latter

Is the former greater than or equal to the latter?

Is the former less than or equal to the latter?

String judgment str1 exp str2:

-z "$str1" Is str1 an empty string

-n "$str1" str1 is not an empty string

"$str1" == "$str2" Is str1 equal to str2

"$str1" != "$str2" Does str1 differ from str2

"$str1" =~ "str2" Does str1 contain str2

Note in particular that the string variable *** is enclosed in quotes, because once there are spaces in the string, the expression is wrong, and interested parties can try comparing str1="hello world" and str2="hello".

File directory judgment: filename

-f $filename is a file

-e $filename exists

-d $filename is a directory

-s $filename file exists and is not empty

! -s $filename file is empty

cycle

Loop form one, similar to Python's for in:

#Traverse the parameters of the output script for i in $@; do echo $i done

Loop form 2, and C language style is very similar:

for ((i = 0 ; i

< 10 ; i++)); do echo $i done 循环打印0到9。 循环形式三: for i in {1..5}; do echo "Welcome $i" done 循环打印1到5。 循环方式四: while [ "$ans" != "yes" ] do read -p "please input yes to exit loop:" ans done 只有当输入yes时,循环才会退出。即条件满足时,就进行循环。 循环方式五: ans=yes until [ $ans != "yes" ] do read -p "please input yes to exit loop:" ans done 这里表示,只有当ans不是yes时,循环就终止。 循环方式六: for i in {5..15..3}; do echo "number is $i" done 每隔5打印一次,即打印5,8,11,14。 函数 定义函数方式如下: myfunc() { echo "hello world $1" } 或者: function myfunc() { echo "hello world $1" } 函数调用: para1="shouwang" myfunc $para1 返回值 通常函数的return返回值只支持0-255,因此想要获得返回值,可以通过下面的方式。 function myfunc() { local myresult='some value' echo $myresult } val=$(myfunc) #val的值为some value 通过return的方式适用于判断函数的执行是否成功: function myfunc() { #do something return 0 } if myfunc;then echo "success" else echo "failed" fi 注释 shell通过#来注释一行内容,前面我们已经看到过了: #!/bin/bash # 这是一行注释 :' 这是 多行 注释 ' ls : log.dat 2>

&1

The meaning of 2>&1 can be found in How to understand 2>&1 in linux shell

Mode 3: Save the log file and output it to the console:

./ test.sh |tee log.dat

script execution

The most common execution methods have been seen above:

./ test.sh

Other implementation methods:

sh test.sh #Executing sh -x test.sh #in the child process will print the command to execute in the terminal, suitable for debugging source test.sh #test.sh executed in the parent process. test.sh #No need to grant execution permission, temporary execution

script exit code

Many times we need to get the execution result of the script, that is, the exit status, usually 0 means successful execution, not 0 means failure. To get the exit code, we need to use exit. For example:

#!/ bin/bash function myfun() { if [ $# -lt 2 ] then echo "para num error" exit 1 fi echo "ok" exit 2 } if [ $# -lt 1 ] then echo "para num error" exit 1 fi returnVal=`myfun aa` echo "end shell" exit 0

One thing that needs special attention here is the use of

returnVal=`myfun aa`

Such a sentence executes the function, even if there is an exit in the function, it will not exit the script execution, but will only exit the function, this is because exit exits the current process, and this way of executing the function is equivalent to forking a child process, so it will not exit the current script. The end result will be that whatever your function argument is ***end shell prints.

./ test.sh;echo $? 0 After reading this article, I believe you have a certain understanding of "What are the Linux shell knowledge points?" If you want to know more about relevant knowledge, welcome to pay attention to the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report