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

How to use spaces in shell programming

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how to use spaces in shell programming". In daily operation, I believe many people have doubts about how to use spaces in shell programming. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use spaces in shell programming". Next, please follow the editor to study!

1. When defining a variable, there can be no spaces on both sides of the = sign.

Eg:

Gender=femal----right

Gender = femal----wrong

Gender= femal----wrong

two。 Leave spaces on both sides of the conditional test statement.

Eg:

If [$gender = femal]; then---right.

Echo "you are femal"

Fi

If [$gender...---wrong

If [$gender...--wrong.

3. The content of the conditional test, if it is a string comparison, leave spaces on both sides of the comparison symbol!

Eg:

If [$gender = femal]; then---right.

If [$gender= femal]; then---wrong.

If [$gender=femal]; then---wrong.

4. If if and then are written on the same line, notice that the then is preceded by the; sign. If then writes on a new line, that's fine.

Eg:

If [$gender = femal]; then---right.

If [$gender = femal]

Then-right.

If [$gender = femal] then---wrong. There is a lack of a; number in front of then.

Error message:

Syntax error near unexpected token then

By the same token, there are a lot of error messages such as

Syntax error near unexpected token fi and so on are all caused in this way.

5.if must keep up with then. The same principle

Elif must keep up with then.

Otherwise, an error message is prompted:

Syntax error near unexpected token else

1) the if statement needs to be followed by then and preceded by a semicolon

2) the space is very important. Shell will think that the space before the space is a command. If a _ blank _ 3 is considered as an assignment operation, and if it is written as a _ blank _ 3, then a will be thought of as a command this= `ls-l | grep'^ -'| wc-l `

3) operators should be separated by spaces, such as test!-d $1, of which! And-d should be separated by a space

Space is an important separator in command parsing.

6. There must be a space between the command and the parameters or objects that follow

If [- x "~ / Workspace/shell/a.sh"]; then

Only a space after-x indicates whether the following string points to an executable file name, otherwise it becomes a test of whether the string-x "~ / Workspace/shell/a.sh" is empty.

7. The symbol'$'that takes the value of a variable and the variable or parenthesis after it cannot have spaces

At this point, the study on "how to use spaces in shell programming" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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

Development

Wechat

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

12
Report