shell script-le

Meaning of eq, ne, le, ge, lt, gt in Shell script

if [1-ne1];then


-eq:equal to

-ne:not equal to

-le:less than or equal to

-ge:greater than or equal to

-lt:less than

-gt: greater than

Shell Scripting

Shell Scripting Basics

Overview: shell is actually an interface between the kernel and the user.

Shell Scripting

If there is a series of frequently used linux commands, you can store them in a file swell. shenll can read this file and execute the shenll can read this file and execute the commands in it. Such a file becomes a script file.

Executing shell scripts

To create a shell script, you have to write it in a text file using any editor like vi.

In order to use bashshell Rai to execute the script magic, the command is: bashmagic or . /magic

echo command:

echo “thisisanexampleoftheechocommand!”

The screen will show back the ” thisisanexampleoftheechocommand!”


Used in shell scripts that can contain commented entries

echo “hello”


echo “world!”

The second line is an example of a comment. It will be ignored by the shell and no message will be generated


Can be created at any time by simple assignment.



All variables in Linux are treated as strings

Referring to a variable:

The $ symbol is used to refer to the contents of a variable

variable1=${ variable2}

Reading a value into a variable

When executing shell scripts, the shell also allows the user to read a value into a variable directly from the keyboard, which can also be done using the read command.


Local and global shell variables

Local variables

When a shell is referenced, only the shell that created it is aware of the variable’s existence

Global variables

are known as subshells

Variables created in a shell are localized to the shell that created them, unless the shell created them. in the shell that created it, unless specifically noted as global with the export command.

Environmental variables:

By changing the values of these variables, the user is able to customize the environment

Some examples of environment variables are HOME, PATH, PS1, PS2, LOGNAME, SHLVL, and SHELL

HOME variable

Each user on a Linux system has an associated variable called HOME. Each user on a Linux system has an associated directory called HOME

When a user logs on, he or she enters the corresponding HOME directory


The PATH variable

Contains a list of directory pathnames delimited by colons, which makes it easy for executable programs to search.

PS1 variable

The PS1 (PromptString1) variable contains the shell prompt, the $ sign



PS2 variable< /p>

Is the environment variable that sets the value for the second prompt

LOGNAME variable

Contains the user’s registered name

$echo “${LOGNAME}”

SHLVL variable

The variable contains the the current working shelllevel

SHELL variable

The environment variable stores the user’s default shell

The env command

can be used to view a table of all moved environment variables and their respective values!

Command substitution

Another (non-Pipes) way to use multiple commands on a single command line is through command substitution

echo “thedatais `date`”

expr command

Used to find the value of an arithmetic expression. The output of this command is sent to standard output


Will display 9 on the screen

Arithmetic Expansion:

You can bracket an expression in $((…)) and compute its value with the following command;



Write a shell script to count the number of unanswered inquiries at a call center. The script should accept the total number of queries reported for the day and the number of queries answered in order to calculate the number of unanswered queries.

Total of all unanswered inquiries = total of all inquiries – number of answered inquiries

<! –[if!supportLists]–>*<! –[endif]–>※※※※※※※※※※※※※※※※※※※※※※※

Conditional execution

test and []

Tests and []

Evaluates expressions and returns true(0) or false()

Numeric tests:

– eq equals is true

– ne is true if not equal

-gt is true if greater than

-ge is true if greater than or equal to

-lt is true if less than

-le is true if less than or equal to

If Constructs

The Linux shell provides constructs for loops and determinations that can be used in shell scripts

< p>Arithmetic test

Combined with the if construct, it can be used to test the numeric value of a variable

String test

The test command can also be used for strings

=Equal is true

! = not equal is true

-z string length is zero is true

-n string length is non-zero is true

File test

The test command can also be used to check the status of a file

-e file exists is true

-r file exists and is readable is true

-w file true if it exists and is writable

-x file true if it exists and is executable

-s file true if it exists and has at least one character

-d file true if it exists and is a directory

-f file true if it exists and is a normal file

-c file true if it exists and is a character file

True if -b file exists and is a block special file

-a and -o or ! not

exit command

To terminate the execution of a shell script and return to the $ prompt

case. . esac

This construct used in shell scripts executes a specific set of commands based on the value of a variable

When the value of a variable matches one of the values. a set of commands written to that value is executed.



while construct






Between do and done can only be executed if the condition is true.

until construct

until loop constructs the opposite of a while loop

until loop will continue to execute until the condition is true

for construct





The for loop takes a list of values as input and executes a loop for each value in the loop

The break and contineu commands

Similar to other languages

The for constructor


Controlling the execution of a process

Requesting background processing

The symbol used to request background processes is (&)




Checking for background process

The ps (process status) command generates one line of entry for each process for each current activity.

Terminating background processes

To terminate background processes, use kill, as follows


Seeing how long it took to complete a command

You can use the time command to see how long it took for a command to complete from start to finish

timefine/etc -name “passwd “2>/dev/null/dev/null indicates that error messages are ignored.

Introduction to piping

The vertical bar (|) is the piping character

It’s just the shell: the output of the command before the “|” is sent as input to the command after the “|”

ls -l|more

Combining commands with pipes is powerful!

Shell script to compare number sizes

Your write down is correct, the following is fine


if[$DEV_ SIZE-ge${EXT_LIMIT[0]}-a$DEV_SIZE-le${EXT_LIMIT[1]}]]

If you are using [] or [[]] for integer testing, you need to use the symbols -eq or -le or -ge for the comparison operators inside, and you can use the >= and <= comparison operators only inside (()). and the logical operations && and || are used in [[and -a and -o are used in [[and && and || are used in (().

Under Linux, write a shell script that reads 5 integers from the keyboard and then displays the maximum, minimum, and average numbers.

#! /bin/bash

#Input any 5 numbers, determine max, min, and sum


read-p “pleaseinput: “num







read-p “pleaseinput: “num










< imgclass>

Extended information:

For loop:

General format:

forvarinitem1item2…. .itemN






Write it on one line:

forvarinitem1item2…. .itemN;docommand1;command2?done;

Write a shell script that reads in 10 parameters and outputs the maximum and minimum values. (Include execution results)

#! /usr/bin/ksh













< p>fi







echo$ max_num


Basically no more problems

The shell script -d is the catalog file, so what are -e, -f? And "! -e" what does this mean again?

-e means true if filename exists.

-f means true if filename is a regular file.