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 ways to use Linux Shell programming

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains the "what is the use of Linux Shell programming", the content of the explanation is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, to study and learn "what is the use of Linux Shell programming" bar!

What is Shell?

Shell itself is a command interpreter, which is between the kernel mode and the user mode of the operating system. It can execute system calls and system commands, so that users can interact with the operating system. It is also used to refer to a computer programming language (similar to C, Python, etc.). A shell program is generally called a script.

The genre of Shell language

At present, there are two main schools of shell:

Sh:

Burne shell (sh)

Burne again shell (bash)

Csh:

C shell (csh)

Tc shell (tcsh)

Korn shell (ksh)

At present, the default shell of most Linux systems is bash.

Overview of Shell programming

Under Linux, there is a scripting language called Shell script, which can help us simplify a lot of work, such as running some commands in batches, so it is necessary to learn its basic usage, a simple hello.sh script like this.

#! / bin/bash echo "Hello World"

The first line #! / bin/bash identifies which Shell interprets the Shell script: / bin/bash. Write the above two lines to the file hello.sh, and you can execute:

Execute shell script method 1:

# if there is no: #! / bin/bash in the first line, you can execute sh. / hello.sh directly in this way

Execute shell script method 2: give permission to execute:

# Grant executable permission to chmod aquix hello.sh# to execute. / hello.sh# result Hello WorldShell variable

There are three types of Shell variables:

1. User-defined variable

two。 Predefined variable

3. Environment variable

1. User-defined variable

To define variables, you need to pay attention to the following two points:

1. No spaces before and after the equal sign: NUM=10

two。 The general variable name is capitalized: Mend1

Invoke the variable using $VAR:

VAR= "Hello Shell" echo $VAR

Note: the variable definition directly names the variable, and special characters such as spaces in the string need to be enclosed in quotation marks, so that a string is stored in the VAR variable, and the value of the variable is called again with the $symbol:

Define variable prefixes: there are three common prefixes:

1. Unset: delete variables

2. Readonly: Mark read-only variables

3. Export: specify global variables

#! / bin/bash # defines ordinary variables without special characters or spaces. You can define global variables without quotation marks CITY=BEIJING# definition read-only variable readonly AGE=21# print variable value echo $CITYecho $NAMEecho $AGE# delete CITY variable unset CITY# will not output BEIJINGecho $CITY

Note: the above content can be saved and run in a file at the end of sh.

two。 Predefined variable

Predefined variables are often used to get command line input, such as the following:

$0: script file name

$1-9: 1st-9th command line argument name

$#: number of command line parameters

$@: all command line arguments

$*: all command line arguments

$?: the exit status of the previous command, which can be used to get the function return value

$: executed process ID

An example:

#! / bin/bash echo "print $" echo "\ $0 = $0" echo "\ $1 = $1" echo "\ $2 = $2" echo "\ $# = $#" echo "\ $@ = $@" echo "\ $* = $*" echo "\ $$= $" echo "\ $? = $?"

The result of executing. / hello.sh 1 2 3 4 5:

Print $# Program name $0 =. / hello.sh# first parameter $1 = "second parameter $2 =" A total of 5 parameters $# = "print out all parameters $@ = 12345 # print out all parameters $* = 12345 # process ID$$ = 945 # No other commands or functions have been executed before. Environment variable

Environment variables exist by default, and the following are commonly used:

1. HOME: user home directory

2. PATH: system environment variable PATH

3. TERM: current terminal

4. UID: current user ID

5. PWD: current working directory, absolute path

Let's look at the example:

#! / bin/bashecho "print env" echo $HOMEecho $PATHecho $TERMecho $PWDecho $UID

Running result:

Print env# current home directory / home/orange# PATH environment variable / home/orange/anaconda2/bin: there are many # current terminal xterm-256color# current directory / home/orange# user ID1000

This is the end of the introduction of the Shell variable, and let's introduce the variable operation of Shell.

Shell operation

We often need to calculate in Shell scripts, so it is necessary to master the basic operation methods. Here are four common operation methods, all of which are m + 1:

1. Masks $[m + 1]

2. M = `expr $m + 1` # be careful to enclose the character in reverse quotation marks

3. Let m=m+1

4. Masks $(m + 1)

Let's look at a practical example:

#! / bin/bash let m=m+1echo 1m mm=$ $[mx1] echo $mm= `expr $m + 1`echo $m # Note: do not add a space around the + sign: massif $expr ((mtape 1)) echo $m

Running result:

Operator summarization in 2345Bash

The operators supported by Bash are:

Mathematical operator

Relational operator

Boolean operator

Logical operator

String operator

File test operator

Mathematical operator + addition-subtraction * multiplication / division% take remainder = assignment = equal test, equal return true equal = unequal test, unequal return true

Note: the multiplication sign * must be preceded by a backslash in order to realize the multiplication.

Relational operators are generally used in IF statements to determine

Relational operators only support numbers, not strings, unless the value of a string is a number

-eq (equal) detects whether two numbers are equal, returns true-ne (not equal) to check whether two numbers are equal, returns true-gt (greater than) to check whether the number on the left is greater than that on the right, if so, returns true-lt (lower than) to detect whether the number on the left is less than that on the right, if so, returns true-ge (greater equal) to detect whether the number on the left is greater than or equal to that on the right, and if so Then return true-le (lower equal) to check whether the number on the left is less than or equal to the one on the right, and if so, return the true Boolean operator-an and operation, both expressions return true-o or operation for true, and one expression for true returns true! Non-operation, return false if the expression is true, otherwise return true logic operator & & logic and II logic or string operator = to detect whether two strings are equal, equal return true [$a = $b] return false = check whether two strings are equal, unequal return true [$a! = $b] return true-z to check whether the string length is 0 Return true [- z $a] for 0 return false-n to check whether the string length is 0, not 0 return true [- n $a] return truestr_name check string is empty, not empty return true [$a] return true file test operator

The file test operator is used to detect various properties of Unix files

-b file detects whether the file is a block device file, if so, returns true-c file to detect whether the file is a character device file, if so, returns true-d file to detect whether the file is a directory, if so, returns true-f file to detect whether the file is an ordinary file (neither a directory nor a device file), if so, returns true-g file to check whether the file has the SGID bit set, and if so Returns true-k file to detect whether the file has adhesion bit (Sticky Bit) set, if so, returns true-p fill to detect whether the file is a named pipe, if so, returns true-u file to detect whether the file has SUID bit set, if so, returns true-r file to detect whether the file is readable, if so, returns true-w file to detect whether the file is writable, if so, returns true-x file to detect whether the file is executable. If so, true-s file is returned to check whether the file is empty (the file size is greater than 0), true-e file is not returned to check whether the file (including directory) exists, and if so, true1. If statement

This is similar to if-else-if in high-level languages, except that the format is a little different. Let's take a look at an example:

#! / bin/bash read VAR# use [] to judge, note the space if [$VAR-eq 10] then echo "true" else echo "false" fi# to determine whether it is a file if [- d test.txt] then echo "true" else echo "false" fi2. For cycle

This is a basic example of using the for loop, which is quite simple, somewhat similar to Python:

#! / bin/bash # ordinary for loop for ((I = 1; I $i.shten done)

two。 Use the loop to modify the file name in batches:

For an in `ls * bam`; do echo "mv $a ${a% .*} .bam"; done# or: ls * bam | while read a mv do echo "bam ${a% .*} .bam" Done thank you for reading, these are the contents of "what is the use of Linux Shell programming?" after the study of this article, I believe you have a deeper understanding of the use of Linux Shell programming, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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