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

Introduction to the basics of Linux scripting and the use of three types of commands in shell scripts

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

Share

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

This article mainly explains "the basics of Linux scripting and the introduction of three types of commands that can be used in shell scripts". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn the basics of Linux scripting and the introduction of three types of commands that can be used in shell scripts.

1. Basics of Linux scripting

1.1 basic introduction to grammar

1.1.1 beginning

The program must start with the following line (must be placed on the first line of the file):

#! / bin/sh

Symbol #! It is used to tell the system that the parameters behind it are the program used to execute the file. In this example, we use / bin/sh to execute the program.

When you edit a script, you must also make it executable if you want to execute it.

To make the script executable:

Compile chmod + x filename so that you can run it with. / filename

1.1.2 comments

In shell programming, sentences that begin with # indicate comments until the end of the line. We sincerely recommend that you use comments in your program.

If you use comments, you can understand the role of the script in a very short time, even if you haven't used the script for quite a long time.

And how it works.

1.1.3 variable

You must use variables in other programming languages. In shell programming, all variables are made up of strings, and you don't need to set the

To make a statement. To assign a value to a variable, you can write:

#! / bin/sh

# assign values to variables:

A = "hello world"

# now print the contents of the variable a:

Echo "An is:"

Echo $a

Sometimes variable names are easily confused with other words, such as:

Num=2

Echo "this is the $numnd"

This does not print out "this is the 2nd", but just "this is the", because shell searches for the value of the variable numnd

But this variable has no value. You can use curly braces to tell shell that we are printing the num variable:

Num=2

Echo "this is the ${num} nd"

This will print: this is the 2nd

1.1.4 Environment variable

Variables that have been processed by the export keyword are called environment variables. We don't talk about environment variables, because usually we just log in

Environment variables are used in the script.

1.1.5 Shell commands and process control

There are three types of commands that can be used in shell scripts:

1) Unix command:

Although arbitrary unix commands can be used in shell scripts, there are still some relatively more commonly used commands. These commands are usually used to

For file and text operations.

Common command syntax and functions

Echo "some text": print text on the screen

Ls: file list

Wc-l filewc-w filewc-c file: calculate the number of lines in the file, calculate the number of words in the file, calculate the number of characters in the file.

Cp sourcefile destfile: file copy

Mv oldname newname: renaming or moving files

Rm file: deleting fil

Grep 'pattern' file: search for strings such as grep' searchstring' file.txt within a file

Cut-b colnum file: specify the range of file contents to be displayed and output them to standard output devices such as output

The 5th to 9th characters of each line cut-b5-9 file.txt should never be confused with the cat command

These are two completely different orders.

Cat file.txt: output file contents to standard output device (screen)

File somefile: get the file type

Read var: prompts the user for input and assigns input to variables

Sort file.txt: sorting lines in the file.txt file

Uniq: delete the rows that appear in the text file, such as: sort file.txt | uniq

Expr: doing mathematical operations Example: add 2 and 3expr 2 "+" 3

Find: search for files such as: search find by file name. -name filename-print

Tee: output data to standard output devices (screens) and files such as: somecommand | tee outfile

Basename file: returns a file name that does not contain a path, for example: basename / bin/tux will return tux

Dirname file: returns the path where the file resides, for example: dirname / bin/tux will return / bin

Head file: print the first few lines of a text file

Tail file: the last few lines of a printed text file

Sed: Sed is a basic find and replace program. You can read text from standard input, such as command pipes, and set the

The result is output to standard output (screen). This command uses a regular expression (see reference) to search.

Don't be confused with wildcards in shell. For example, replace linuxfocus with

LinuxFocus: cat text.file | sed's Universe Linuxfocus Universe LinuxFocusUniver'> newtext.file

Awk: awk is used to extract fields from a text file. By default, the field delimiter is a space, and you can use-F to specify another separator.

Cat file.txt | awk-F,'{print $1 ",'$3}'we use here as a field separator and print at the same time

The first and third fields. If the file contents are as follows: Adam Bor, 34, IndiaKerry Miller, 22, USA

The output of the command is: Adam Bor, IndiaKerry Miller, USA

2) Concepts: piping, redirection and backtick

These are not system commands, but they are really important.

The pipe (|) takes the output of one command as the input of another.

Grep "hello" file.txt | wc-l

Search for rows that contain "hello" in file.txt and count them.

Here the output of the grep command is used as the input of the wc command. Of course, you can use multiple commands.

Redirect: outputs the results of the command to a file instead of standard output (screen).

Write to the file and overwrite the old file

Add it to the end of the file and keep the contents of the old file.

Reverse short slash

Use the backslash to use the output of one command as a command-line argument to another.

Command:

Find. -mtime-1-type f-print

Used to find files that have been modified in the past 24 hours (- mtime-2 means the last 48 hours). If you

If you want to package all the files found, you can use the following script:

#! / bin/sh

# The ticks are backticks (`) not normal quotes ('):

Tar-zcvf lastmod.tar.gz `find. -mtime-1-type f-print`

3) process control

1.if

The "if" expression executes the following part of the then if the condition is true:

If....; then.

....

Elif....; then.

....

Else

....

Fi

In most cases, you can use test commands to test conditions. For example, you can compare strings and judge files

Whether it exists and whether it is readable, etc.

Conditional tests are usually referred to as "[]". Notice that the spaces here are very important. Make sure the square brackets are blank.

[- f "somefile"]: determine whether it is a file or not

[- x "/ bin/ls"]: determine whether / bin/ls exists and has executable permission

[- n "$var"]: determine whether the $var variable has a value

["$a" = "$b"]: judge whether $an and $b are equal

Execute man test to see the types that all test expressions can compare and judge.

Execute the following script directly:

#! / bin/sh

If ["$SHELL" = "/ bin/bash"]; then

Echo "your login shell is the bash (bourne again shell)"

Else

Echo "your login shell is not bash but $SHELL"

Fi

The variable $SHELL contains the name of the login shell, which we compared with / bin/bash.

Shortcut operator

Friends who are familiar with the C language may like the following expression:

[- f "/ etc/shadow"] & & echo "This computer uses shadow passwors"

Here & & is a shortcut operator that executes the statement on the right if the expression on the left is true.

You can also think of it as the and operation in logical operations. The example above indicates that if the / etc/shadow file exists

Then print "This computer uses shadow passwors". The same or operation (| |) is also true in shell programming.

Available. Here's an example:

#! / bin/sh

Mailfolder=/var/spool/mail/james

[- r "$mailfolder"]'{echo "Can not read $mailfolder"; exit 1;}

Echo "$mailfolder has mail from:"

Grep "^ From" $mailfolder

The script first determines whether the mailfolder is readable. Print the line "From" in the file if it is readable. If unreadable

Or the operation takes effect, and the script exits after the error message is printed. There is a problem here, that is, we must have two orders:

-print error message

-exit the program

We use curly braces to put the two commands together as a single command in the form of anonymous functions. General functions are mentioned below.

We can do anything with if expressions without the and / OR operator, but it's much more convenient to use the and / OR operator.

2.case

Case: expressions can be used to match a given string, not a number.

Case... In

(.) Do something here

Esac

Let's look at an example. The file command can identify the file type of a given file, such as:

File lf.gz

This returns:

Lf.gz: gzip compressed data, deflated, original filename

Last modified: Mon Aug 27 23:09:18 2001, os: Unix

We took advantage of this to write a script called smartzip that automatically unzips compressed files of bzip2, gzip, and zip types:

#! / bin/sh

Ftype= `file "$1" `

Case "$ftype" in

"$1: Zip archive" *)

Unzip "$1"

"$1: gzip compressed" *)

Gunzip "$1"

"$1: bzip2 compressed" *)

Bunzip2 "$1"

*) echo "File $1 can not be uncompressed with smartzip"

Esac

You may notice that we use a special variable $1 here. This variable contains the first parameter value passed to the program.

That is, when we run:

Smartzip articles.zip

$1 is the string articles.zip

3. Selsect

Select expression is an extended application of bash, especially good at interactive use. Users can choose from a different set of values.

Select var in...; do

Break

Done

.... Now $var can be used....

Here is an example:

#! / bin/sh

Echo "What is your favourite OS?"

Select var in "Linux"Gnu Hurd"Free BSD"Other"; do

Break

Done

Echo "You have selected $var"

Here is the result of the script running:

What is your favourite OS?

1) Linux

2) Gnu Hurd

3) Free BSD

4) Other

#? 1

You have selected Linux

4.loop

Loop expression:

While...; do

....

Done

While-loop will run until the expression test is true. Will run while the expression that we test for is true.

The keyword "break" is used to jump out of the loop. The keyword "continue" is used to skip to the next loop without executing the rest.

The for-loop expression looks at a list of strings separated by spaces and assigns them to a variable:

For var in....; do.

....

Done

In the following example, the ABC will be printed to the screen separately:

The code is as follows:

#! / bin/sh

For var in A B C; do

Echo "var is $var"

Done

Here is a more useful script, showrpm, which prints statistics for some RPM packages:

The code is as follows:

#! / bin/sh

# list a content summary of a number of RPM packages

# USAGE: showrpm rpmfile1 rpmfile2...

# EXAMPLE: showrpm / cdrom/RedHat/RPMS/*.rpm

For rpmpackage in $*; do

If [- r "$rpmpackage"]; then

Echo "= $rpmpackage ="

Rpm-qi-p $rpmpackage

Else

Echo "ERROR: cannot read file $rpmpackage"

Fi

Done

Here comes the second special variable, $*, which contains all the command line parameter values entered.

If you run showrpm openssh.rpm w3m.rpm webgrep.rpm

At this point, $* contains three strings, namely openssh.rpm, w3m.rpm and webgrep.rpm.

5. Quotation mark

Before passing any parameters to the program, the program extends wildcards and variables. The so-called extension here means that the program will put wildcards

Replace (for example, *) with the appropriate file name, and the variable with the value of the variable. To prevent the program from making this replacement, you can use the

Quotation marks: let's look at an example, suppose there are some files in the current directory, two jpg files, mail.jpg and tux.jpg.

1.2 compile SHELL script

# chauffeured Binder mod + x filename

Cho * .jpg ∪ to execute your script. / filename.

This will print out the result of "mail.jpg tux.jpg".

Quotation marks (single and double quotes) prevent this wildcard extension:

#! / bin/sh

Echo "* .jpg"

Echo'* .jpg'

This will print "* .jpg" twice.

Single quotation marks are stricter. It prevents any variable expansion. Double quotation marks prevent wildcard expansion but allow variable expansion.

#! / bin/sh

Echo $SHELL

Echo "$SHELL"

Echo'$SHELL'

The running result is:

/ bin/bash

/ bin/bash

$SHELL

Finally, another way to prevent this extension is to use the escape character, the backslash:

Echo * .jpg

Echo $SHELL

This will output:

* .jpg

$SHELL

6. Here documents

When passing a few lines of text to a command, here documents

It's a good way. It is useful to write a helpful text for each script, if we have that here documents

You don't have to use the echo function to output line by line. A "Here document" with shift by 2

The code is as follows:

--) shift;break;; # end of options

-*) echo "error: no such option $1.-h for help" exit 1

*) break

Esac

Done

Echo "opt_f is $opt_f"

Echo "opt_l is $opt_l"

Echo "first arg is $1"

Echo "2nd arg is $2"

You can run the script like this:

Cmdparser-l hello-f-somefile1 somefile2

The result returned is:

Opt_f is 1

Opt_l is hello

First arg is-somefile1

2nd arg is somefile2

How does this script work? The script first loops through all input command line parameters, which will enter the parameters

Compare with the case expression, set a variable and remove the parameter if it matches. According to the convention of unix system

The first input should be the parameter that contains the minus sign.

Part 2 examples

Now let's discuss the general steps for writing a script. Any good script should have help and input parameters. And it's a great idea to write a pseudo script (framework.sh) that contains the framework required by most scripts. At this point, when writing a new script, we just need to execute the copy command:

Cp framework.sh myscript

And then insert your own function.

Let's look at two more examples:

Binary to decimal conversion

The script B2d converts a binary number, such as 1101, to the corresponding decimal number. This is also an example of doing mathematical operations with the expr command:

The code is as follows:

#! / bin/sh

# vim: set sw=4 ts=4 et:

Help ()

{

Cat <

B2h-convert binary to decimal

USAGE: B2h [- h] binarynum

OPTIONS:-h help text

EXAMPLE: b2h 111010

Will return 58

HELP

Exit 0

}

Error ()

{

# print an error and exit

Echo "$1"

Exit 1

}

Lastchar ()

{

# return the last character of a string in $rval

If [- z "$1"]; then

# empty string

Rval= ""

Return

Fi

# wc puts some space behind the output this is why we need sed:

Numofchar= `echo-n "$1" | wc-c | sed's / g``

# now cut out the last char

Rval= `echo-n "$1" | cut-b $numofchar`

}

Chop ()

{

# remove the last character in string and return it in $rval

If [- z "$1"]; then

# empty string

Rval= ""

Return

Fi

# wc puts some space behind the output this is why we need sed:

Numofchar= `echo-n "$1" | wc-c | sed's / g``

If ["$numofchar" = "1"]; then

# only one char in string

Rval= ""

Return

Fi

Numofcharminus1= `expr $numofchar "-" 1`

# now cut all but the last char:

Rval= `echo-n "$1" | cut-b 0color ${numofcharminus1} `

}

While [- n "$1"]; do

Case $1 in

-h) help;shift 1 Taiwan; # function help is called

--) shift;break;; # end of options

-*) error "error: no such option $1.-h for help"

*) break

Esac

Done

# The main program

Sum=0

Weight=1

# one arg must be given:

[- z "$1"] & & help

Binnum= "$1"

Binnumorig= "$1"

While [- n "$binnum"]; do

Lastchar "$binnum"

If ["$rval" = "1"]; then

Sum= `expr "$weight"+" $sum "`

Fi

# remove the last position in $binnum

Chop "$binnum"

Binnum= "$rval"

Weight= `expr "$weight" * "2`

Done

Echo "binary $binnumorig is decimal $sum"

The algorithm used in this script is to use the weights of decimal and binary numbers (1, 2, 4, 8, 16, etc.), for example, binary "10" can

To convert to decimal in this way:

0 * 1 + 1 * 2 = 2

In order to get a single binary number, we use the lastchar function. This function uses wc-c to calculate the number of characters

Then use the cut command to take out the last character. The Chop function removes the last character.

File loop program

Maybe you are one of the people who want to save all your emails to a file, but after a few months

In the future, the file may become so large that it slows access to the file. The following script rotatefile

Can solve this problem. This script can rename the message save file (assuming outmail) to outmail.1

For outmail.1, it becomes outmail.2 and so on.

The code is as follows:

#! / bin/sh

# vim: set sw=4 ts=4 et:

Ver= "0.1"

Help ()

{

Cat <

Rotatefile-rotate the file name

USAGE: rotatefile [- h] filename

OPTIONS:-h help text

EXAMPLE: rotatefile out

This will e.g rename out.2 to out.3, out.1 to out.2, out to out.1

And create an empty out-file

The max number is 10

Version $ver

HELP

Exit 0

}

Error ()

{

Echo "$1"

Exit 1

}

While [- n "$1"]; do

Case $1 in

-h) help;shift 1

--) break

-*) echo "error: no such option $1.-h for help" exit 1

*) break

Esac

Done

# input check:

If [- z "$1"]; then

Error "ERROR: you must specify a file, use-h for help"

Fi

Filen= "$1"

# rename any. 1,. 2 etc file:

For n in 9 8 7 6 5 4 3 2 1; do

If [- f "$filen.$n"]; then

P = `expr $n + 1`

Echo "mv $filen.$n $filen.$p"

Mv $filen.$n $filen.$p

Fi

Done

# rename the original file:

If [- f "$filen"]; then

Echo "mv $filen $filen.1"

Mv $filen $filen.1

Fi

Echo touch $filen

Touch $filen

How does this script work? After detecting that the user has provided a file name, we do a 9-to-1 loop. File 9 is named 10, file 8 is renamed to 9, and so on. After the loop is complete, we name the original file File 1 and create an empty file with the same name as the original file.

Debug

The simplest debugging command is, of course, to use the echo command. You can use echo to print the value of any variable wherever it is suspected to be wrong. This is why most shell programmers spend 80% of their time debugging programs. The advantage of the Shell program is that it doesn't need to be recompiled, and it doesn't take much time to insert an echo command.

Shell also has a real debug mode. If there is an error in the script "strangescript", you can debug it like this:

Sh-x strangescript

This executes the script and displays the values of all variables.

Shell also has a mode where you don't need to execute a script and just check the syntax. You can use it like this:

Sh-n your_script

This will return all syntax errors

-

Automatically restart the script

Assuming that the program that needs to be restarted is called test, we judge whether the program is normal by judging the number of processes.

Ps-ef | grep "$1" | grep-v "grep" | wc-l is the number of processes that get $1 (in this case, test). The script determines the next step based on the number of processes. Through an endless loop, check the number of processes of a specified program in the system every 1 second.

The code is as follows:

Script check

The code is as follows:

#! / bin/sh

#-

# function: CheckProcess

# function: check whether a process exists

# Parameter: $1-the name of the process to be checked

# return: return 0 if it exists, otherwise return 1.

#-

CheckProcess ()

{

# check whether the input parameters are valid

If ["$1" = ""]

Then

Return 1

Fi

# $PROCESS_NUM gets the number of specified process names. If 0 is returned for 1, it is normal. If 1 is not returned, it means there is an error and needs to be restarted.

PROCESS_NUM= `ps-ef | grep "$1" | grep-v "grep" | wc-l`

If [$PROCESS_NUM-eq 1]

Then

Return 0

Else

Return 1

Fi

}

# check whether the test instance already exists

While [1] do

CheckProcess "test"

CheckQQ_RET=$?

If [$CheckQQ_RET-eq 1]

Then

# Kill all test processes and do whatever you need to do

Killall-9 test

Exec. / test &

Fi

Sleep 1

Done

Script start:

Join limit coredumpsize 102400, set the size of core file, once the program Core Dump, there are traces to be found. Executing the check script in the background in this script can save a lot of trouble

#! / bin/csh

Limit coredumpsize 102400

. / check &

-

Shell syntax

Shell syntax

?

After the command on the left (command 1) returns true (that is, 0 is returned and successfully executed), the command on the right (command 2) can be executed; in other words, "if the command is successful & then execute the command".

| | the opposite of & &. If | | the command on the left (command 1) is not executed successfully, then execute | | the command on the right (command 2); or in other words, "if the command fails | | then execute the command.

To execute a set of commands in the current shell, you can separate each command with a command delimiter and enclose all commands in parentheses ().

At this point, I believe you have a deeper understanding of "the basics of Linux scripting and the introduction of three types of commands that can be used in shell scripts". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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