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 program with Shell

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

Share

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

This article introduces the knowledge of "how to program with Shell". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Basic knowledge

Shell execution method sh xx.sh (file must have r permission) |. / xx.sh (file must have x permission) Note: sh xx.sh method is more secure

File name suffix .sh

The beginning of shell content #! / bin/bash | | #! / bin/sh | | #! / bin/csh | | #! / bin/ksh Note: #! / bin/bash is frequently used

Exit exit 0

Variable assignment and use

You can use it directly without definition: there can be no spaces at both ends of the = sign when a variable is assigned.

Access the contents of a variable as $variable name or ${variable name}

Process control

Judgment note: make sure the square brackets are blank; make sure that each condition has a then

The code is as follows:

If [- d 'dirname']; then

Command set 1

Elif condition 2 then

Command set 2

Else

Command set 4

Fi

The code is as follows:

Case $variable in

Match_1)

Command set 1

Match_2)

Command set 2

……

*) (optional)

Command set n

Esac

Cycle

1.while condition

The code is as follows:

Do

/ / TODO

Done

2.until condition

The code is as follows:

Do

Command set

Done

3.for ((initial value; condition; step))

The code is as follows:

Do

Command set

Done

4.for Loop variables in variable list

The code is as follows:

Do

Command set

Done

Commonly used judgment conditional statements

Determine the file type

Examples of test flag semantics

-e whether test-e $file_name exists in "file or directory"

-f whether the "file or directory" exists and is the file test-f $file_name

-d whether the "file or directory" exists and is the directory test-d "/ boot"

-b "File or directory" exists and is a block device test-b "/ dev/sda1"

-c "file or directory" exists and is a word device test-c "/ dev/tty0"

-S "File or directory" exists and is a Socket file test-S "/ var/run/rpcbind.sock"

-p "file or directory" exists and is a FIFO file test-p $file_name

-L "file or directory" exists and is a linked file test-L $file_name

Determine file permissions

Test flag semantics

-r whether a "file or directory" exists and has readable permissions

-w whether a "file or directory" exists and has writable permissions

-x whether a "file or directory" exists and has executable permissions

-u "file or directory" exists and has the attribute of SUID

-g "file or directory" exists and has the attribute of SGID

-k "file or directory" exists and has the attribute of Sticky bit

-s "file or directory" exists and is not empty

Comparison between files

Test flag semantics

-whether nt "file name 1" is newer than "file name 2" (newer than)

-whether ot "file name 1" is older than "file name 2" (older than)

-ef determines whether "file name 1" and "file name 2" are the same file, which is mainly used to determine whether the two files point to the same inode node.

Comparison between integers

Test flag semantics

-eq two numbers are equal (equal)

-ne is not equal in number (not equal)

-gt integer 1 is greater than integer 2 (greater than)

-lt integer 1 is less than integer 2 (less than)

-ge integer 1 is greater than or equal to integer 2 (greater than or equal)

-le integer 1 is less than or equal to integer 2 (less than or equal)

With, or, not

Test flag semantics

-a (and) returns true test-e file1-a-d file2 if two conditions are true at the same time

-o (or) returns true test-r file1-o-x file2 if either of the two conditions is true

! (not) the current condition is not test!-d file1

Control structure

Calculate the value of an integer variable

Expr Note: there should be a space between operation symbols and parameters; wildcard symbols (*) should be used as multiplication operators.

Let example: let s = (2x3) * 4

This is the end of "how to program with Shell". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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