Home  >  Article  >  Operation and Maintenance  >  what is shell script in linux

what is shell script in linux

WBOY
WBOYOriginal
2022-03-15 15:02:075339browse

In Linux, shell script is a programming method, which is a collection of commands. The shell script writes some shell syntax and instructions in it, and uses functions such as pipeline commands and traffic redirection to achieve the desired processing purpose. It is a small program written temporarily to complete the current work.

what is shell script in linux

#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.

What is a shell script in Linux

1. The concept and meaning of shell script

Shell script is a programming method under Linux. Script (Shell script) is a program written using the functions of the shell. This program uses a plain text file to write some shell syntax and instructions in it, and then uses regular notation, pipeline commands, data flow redirection and other functions. To achieve the processing purposes we want. To put it bluntly, in order to solve the actual problems of Linux, a small program was temporarily written to complete the current work. If it needs to be used multiple times, it can be saved and recycled.

2. Script specifications

The script uses the shebang mechanism, which is in this format:! #/bin/bash, !#/use/bin/python. Simple shell script interpretation is required. Here is an example:

#!/bin/bash
# ——————————————
# Filename: hello.sh
# Revision: 1.1
# Date: 2017/08/05
# Author: Mr.Dong
# Email: Dong@gmail.com
# Website: www.magedu.com
# Description: This is the first script
# ——————————————
echo “hello world”

Using this information, you can quickly know the usefulness of this script so that it can be used easily for work.

You can use the following two commands to debug the script when running

bash -n hello.sh to check the syntax errors of the script

bash -x hello.sh Yes Check the execution steps of the script

3. Variables

1. Local variables

Local variables are only valid for the current shell and not for subshells (SHLVL Check which level of shell it is).

Variable assignment: name='value'

String assignment: name=”root”

Variable reference: name=”$USER”

Command reference: name=COMMAND name=$(COMMAND)

Display all defined variables: set

Delete variables: unset name

2. Environment variables

Environment variables are valid for both the current SHELL and its sub-SHELLs.

Variable declaration and assignment commands:

export name=VALUE
declare -x name=VALUE

Variable references: $name,${name}

Display all environment variables command:

env printenv export declare -x

bash comes with environment variables:

PATH SHELL USER UID HOME PWD SHLVL LANG MAIL HOSTNAME HISTSIZE

3. Read-only variables

Declare read-only variables:

readonly name
declare -r name

View read-only variables: readonly -p

4. Position variables

$1, $2, …: Corresponding to the 1st, 2nd and other parameters, shift [n] changes the position

$0: The command itself

$: All parameters passed to the script, all parameters are combined into one string

$@: All parameters passed to the script, each parameter is an independent string

$#: The number of parameters

$@ $ will only differ when enclosed in double quotes

set —

4. Arithmetic operations and logical operations

1. Arithmetic operations

Arithmetic operators: – * / % (remainder) **(power)

Commonly used operations:

(1) let var= Arithmetic expression

(2) var=$[Arithmetic expression]

(3) var=$((Arithmetic expression))

(4) var=$(expr arg1 arg2 arg3 …)

(5) declare –i var = numerical value

(6) echo 'Arithmetic expression' | bc

Generate random number command: echo $[$RANDOM%number] Random number between 0-number

Assignment operation: = -= *= /= %=

Self-increasing and self-decreasing:

let var+=1
let var++
let var-=1
let var–

2. Logical operations

true=0 false=1

and 1&&1=1 1&&0=0 0&&1=0 0&&0=0

or 1||1=1 1||0=1 0||1=1 0||0=0

Not !1=0 !0=1

5.test command

The test command is a practical tool for testing conditional expressions in the shell environment.

For example:

test “$A” == “$B” && echo “Strings are equal”
[ “$A” == “$B” ] && echo “Strings are equal”

These two expressions express the same meaning.

[[ ]] indicates support for regular expressions

Numerical comparison: String comparison:

-gt Is greater than >

-ge Is greater than Equal to >=

-eq Is it equal to ==

-ne Is it not equal to !=

-lt Is it less than <

-le Is it Less than or equal to <=

Related recommendations: "Linux Video Tutorial"

The above is the detailed content of what is shell script in linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn