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

Shell scripting: what are the basic elements that build the cornerstone of a program

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "Shell script programming: what are the basic elements of building program cornerstones". In daily operations, I believe many people have doubts about Shell script programming: what are the basic elements of building program cornerstones? Xiaobian 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 of "Shell script programming: what are the basic elements of building program cornerstones?" Next, please follow the editor to study!

The Shell for this series of courses applies to bash and zsh.

➜~ bash-- version | grep version GNU bash, version 5.0.17 (1)-release (x86_64-pc-linux-gnu)➜ ~ zsh-- version zsh 5.8 (x86_64-ubuntu-linux-gnu)

1. Mathematical expression

When we learn programming, the natural starting point of our instinct is how to do mathematical operations and how to deal with mathematical expressions. In bash, implementing this is a bit cumbersome, and we need to put the expression into $((...)). Medium.

Echo $((60 seconds 60 seconds 24)) # calculate the number of seconds in a day 86400

Ignore $(...) for the time being. Remember that you need to embed the mathematical expression we know, because if you type the mathematical expression directly, you will report an error on the spot.

$56 + 72 56 + 72 56: command not found

Take a look at a compound expression:

$echo $(3) 5 (7) * (2 + (4) echo $(3) 5 (7)) * (2 + (4) 390

As the first step in shell-script, we now know that the terminal is a portable calculator.

two。 Naming and variables

Our memory is limited and it is not easy to remember a long list of numbers or a program. The core solution that programming languages provide for this is naming. The way of naming is simple and straightforward.

$radius=5 # Note that there can be no spaces radius=5 on both sides of the equal sign

When you read a variable, you need the prefix $to de-index it:

$echo The radius is $radius. The radius is 5.

Next, we calculate the area of the circle:

$radius=5; pi=3; area=$ (($pi*$radius**2)); echo $area 75

So we can do mathematical operations directly with abstract names.

3. Constants and environmental variables

We can name variables, but the names of variables are not completely free and cannot conflict with the environment variables of the system. For example, check the process number of the current bash:

$echo $BASHPID 15190

Check out the complete process tree:

$pstree-asp $BASHPID systemd,1 splash └─ tmux: server,2628 new-s spirit └─ bash,2629 └─ bash,15190-v └─ pstree,26568-asp 15190

If you are curious about how many environment variables are in the system, you should ask the env or printenv command:

$env | nl | tail-n 5 87 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus 88 MAIL=/var/mail/gaowei 89 LC_NUMERIC=en_US.UTF-8 90 KONSOLE_DBUS_WINDOW=/Windows/1 91 _ = / usr/bin/env

As the results show, there are 91 environment variables in the current system.

4. Function

Compared with variables, functions are more powerful and expressive abstract tools. We can think of function as a "black box" for performing specific tasks. The syntax of the function is:

Function function_name {command... } or function_name () {command... }

In the application of the function, we still start from the familiar simple mathematical operation to find the square of the given number.

$function square {echo $(($1 * 2))}

$1 in the body of the function is something new, which refers to the positional parameter of the first parameter. Apply this function:

$square 11 121$ square 121 14641

5. Conditional statement

So far, the program expressive power expressive-power we have defined is very limited. Because we still have no way to make all kinds of tests, and according to the different results of the tests to determine the next step, that is, to give intelligence to the program.

At this point, we need to introduce conditional judgment, and the basic structure is:

If commands; then commands [elif commands; then commands...] [else commands] fi

We try to find the function of absolute value:

Function abs {if (($1 > 0)); then echo $1 elif (($1 = 0)); then echo 0 else echo $((- $1)) fi}

Test the newly defined abs function:

Abs-110110

In addition to the three simple logical judgment symbols =, you can also use the compound logic operator:

$command1 & & command2 # and logic $command1 | | command2 # or logic $! # not

6. Cyclic structure

Looping structure, the first thing that comes to mind is, of course, the for statement:

$for i in *; do echo $I; done anaconda3 backup.doom.d backup.gnus.el $for i in {A.. E}; do echo $i; done A B C D E

And while statements

Count=1 while [["$count"-le 5]]; do echo "$count" count=$ ((count + 1)) done echo "Finished."

7. Read keyboard input

The most important point of the program is the communication and interaction with the user. I like to replace xargs with read, such as this structure:

Find. -iname "* .pdf" | while read line; do something

Read has several interesting parameters, starting with-p _ prompt

Read-p "Enter one or more values. >" echo "REPLY ='$REPLY'"

Secondly,-t limits the feedback time of the user:

Read-t 10-p "Enter secret passphrase" at this point, the study on "Shell scripting: what are the basic elements of building the cornerstone of the program" 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