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 is the use of Shell Script in linux

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article is to share with you about the use of Shell Script in linux. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

1 Shell Scipt

A program written with instructions and basic programming structures can complete complex processing flows.

1.1 Program writing

The code is as follows:

#! / bin/bash

# Program:

# This program shows "Hello Wrold" in your screen.

# History:

# 2013-2-3 on_1y First release

PATH=$PATH

Export PATH

Echo-e "Hello World!\ a\ n"

Exit 0

The first line #! / bin/bash indicates the type of shell used. Different shell syntax may be different, so indicate which shell is used.

Other # starts with comments, which generally need to be explained.

Program function

Version history

Author and contact information

Set the PATH variable so that you can call the commands in the corresponding path directly.

Program body part

Exit 0 indicates that the program executes successfully and returns 0 to the environment.

1.2 Program execution

Bash $bash sh01.sh # if you use sh sh01.sh and sh does not point to bash, then the syntax in sh01.sh will be inconsistent, because the shell script written in bash syntax is interpreted with # sh. For this program, if # $type sh # gets sh is hashed (/ bin/sh) #, then it will output-e Hello worldview instead of Hello world!

The code is as follows:

$. / xxx.sh $chmod + x sh01.sh $. / sh01.sh

Source $source sh01.sh

Note: the difference between using bash and using source is that when executing with bash, shell script actually creates a new bash subroutine under the parent program bash. When the subroutine is executed, the variables defined in shell script will disappear with the end of the subroutine, while when executed with source, it is executed in the parent program bash, and the variables defined in shell script are still there.

2 simple Shell exercises

2.1 case 1 receives user input

The code is as follows:

#! / bin/bash

# Program:

# This program is used to read user's input

# Site: www.yisu.com

# 2013-2-3 on_1y First release

PATH=$PATH

Export PATH

Read-p "Your firstname:" firstname # tell user to input

Read-p "Your lastname:" lastname # tell user to input

Echo-e "\ nYour full name: $firstname $lastname"

Exit 0

Call:

The code is as follows:

$bash sh02.sh

Your first name:Minix

Your last name:007

Your full name: Minix 007

2.2 case 2 create a file with a similar name by date

The code is as follows:

#! / bin/bash

# Program:

# This program is used to create files according to date

# History:

# 2013-2-3 on_1y First release

PATH=$PATH

Export PATH

# Get filename from user

Echo-e "I will use 'touch' to create three files."

Read-p "Please input your filename:" tmpfilename

# Prevent the user input [Enter]

# Check whether filename exists or not

Filename=$ {tmpfilename:- "filename"}

# Get the final filename according to date

Date1=$ (date-- date='2 days ago' +% Y%m%d) # date of 2 days ago

Date2=$ (date-- date='1 days ago' +% Y%m%d) # date of yesterday

Date3=$ (date +% Y%m%d) # date of today

Filename1=$ {filename} ${date1}

Filename2=$ {filename} ${date2}

Filename3=$ {filename} ${date3}

# Create file

Touch "$filename1"

Touch "$filename2"

Touch "$filename3"

Exit 0

Call:

The code is as follows:

$bash sh03.sh

I will use 'touch' to create three files.

Please input your filename:WhoKnows

$ls W*

WhoKnows20130201 WhoKnows20130202 WhoKnows20130203

3 judgment type

3.1 Test whether the file exists

Test-e filename returns 0 or 1 depending on whether the filename exists, and then the echo displays the result:

The code is as follows:

$test-e sh01.sh & & echo "Exists" | | echo "Not exists"

Exists

$test-e sh0x.sh & & echo "Exists" | | echo "Not exists"

Not exists

3.2 test common options

3.2.1 File types

The code is as follows:

-e file: whether file exists

-f file: whether file exists and is a file

-d file: whether file exists and is a directory

3.2.2 permissions

-r file: whether file has read permission

3.2.3 comparison between new and old documents

-nt file1 file2: whether file1 is newer than file2

3.2.4 Integer, string, multiple conditional judgment

-z string: whether string is empty

Example: output specified file types and attributes

The code is as follows:

#! / bin/bash

# Program:

# This program is used to output type and permission of the target file

# History:

# 2013-2-3 on_1y First release

PATH=$PATH

Export PATH

# Get filename from user

Echo-e "Input name of the file that you want to check.\ n"

Read-p "Filename:" filename

Test-z $filename & & echo "You must input a filename." & & exit 0

# Check whether the file exists or not

Test!-e $filename & & echo "The file'$filename' DO NOT exists" & & exit 0

# Check type and permission of the file

Test-f $filename & & filetype= "regular file"

Test-d $filename & & filetype= "directory"

Test-r $filename & & perm= "readable"

Test-w $filename & & perm= "$perm writable"

Test-x $filename & & perm= "$perm executable"

# Output result

Echo "The filename:$filename is a $filetype"

Echo "And Permissions are: $perm"

Exit 0

Call:

The code is as follows:

$bash sh04.sh

Input name of the file that you want to check.

Filename:sh01.sh

The filename:sh01.sh is a regular file

And Permissions are: readable writable executable

3.3 use [] judgment

Test whether the file exists

The code is as follows:

$[- e "sh01.sh"]; echo $?

0

$[- e "sh0x.sh"]; echo $?

one

Note that there must be spaces in []

This method is similar to test's test-e "sho1.sh"; echo $? Is consistent.

4 Shell Script parameters

The code is as follows:

#! / bin/bash

# Program:

# This program is used to ouput parameter of the shell script

# History:

# 2013-2-3 on_1y First release

PATH=$PATH

Export PATH

Echo "The script's name is = = > $0"

Echo "Total parameter number is = = > $#"

# Check whether number of the parameter is less than 2

["$#"-lt 2] & & echo "The number of parameter is less than 2.Stop here." & & exit 0

Echo "The whole parameter is = = >'$@'"

Echo "The first parameter is = = > $1"

Echo "The first parameter is = = > $2"

Exit 0

Call:

The code is as follows:

$bash sh05.sh 1a 2b 3c 4d

The script's name is = = > sh05.sh

Total parameter number is = = > 4

The whole parameter is = = >'1a 2b 3c 4d'

The first parameter is = = > 1a

The first parameter is = = > 2b

Note: from the above procedure, you can see how the preset variables related to parameters are represented.

5 conditional expression

5.1 if structure

The code is as follows:

#! / bin/bash

# Program:

# This program is used to show if else expression

# History:

# 2013-2-3 on_1y First release

PATH=$PATH

Export PATH

Read-p "Please input [Yram N]" choice

If ["$choice" = = "Y"] | | ["$choice" = = "y"]; then

Echo "OK, continue"

Exit 0

Fi

If ["$choice" = = "N"] | | ["$choice" = = "n"]; then

Echo "Oh, interupt"

Exit 0

Fi

Exit 0

Call:

The code is as follows:

$bash sh06.sh

Please input [Y/N] y

OK, continue

$bash sh06.sh

Please input [Y/N] n

Oh, interupt

5.2 if else structure

The code is as follows:

#! / bin/bash

# Program:

# This program is used to show if else expression

# History:

# 2013-2-3 on_1y First release

PATH=$PATH

Export PATH

Read-p "Please input [Yram N]" choice

If ["$choice" = = "Y"] | | ["$choice" = = "y"]; then

Echo "OK, continue"

Exit 0

Elif ["$choice" = = "N"] | | ["$choice" = = "n"]; then

Echo "Oh, interupt"

Exit 0

Else

Echo "Input [Yplink N]"

Fi

Exit 0

5.3 case

The code is as follows:

#! / bin/bash

# Program:

# This program is used to show case expression

# History:

# 2013-2-3 on_1y First release

PATH=$PATH

Export PATH

Read-p "Tell me your choice: [1-3] = >" choice "

Case $choice in

"1")

Echo "Your choice is ONE"

"2")

Echo "Your choice is TWO"

"3")

Echo "Your choice is THREE"

Esac

Exit 0

Call:

The code is as follows:

$bash sh08.sh

Tell me your choice: [1-3] = > 2

Your choice is TWO

$bash sh08.sh

Tell me your choice: [1-3] = > 1

Your choice is ONE

$bash sh08.sh

Tell me your choice: [1-3] = > 3

Your choice is THREE

6 function

The code is as follows:

#! / bin/bash

# Program:

# This program is used to test function

# History:

# 2013-2-3 on_1y First release

PATH=$PATH

Export PATH

Function myprint () {

Echo-n "Your choice is"

}

Read-p "Tell me your choice: [1-3] = >" choice "

Case $choice in

"1")

Myprint;echo "ONE"

"2")

Myprint;echo "TWO"

"3")

Myprint;echo "THREE"

Esac

Exit 0

Call:

The code is as follows:

$bash sh09.sh

Tell me your choice: [1-3] = > 1

Your choice is ONE

$bash sh09.sh

Tell me your choice: [1-3] = > 2

Your choice is TWO

$bash sh09.sh

Tell me your choice: [1-3] = > 3

Your choice is THREE

7 cycle

7.1 while

The code is as follows:

#! / bin/bash

# Program:

# This program shows while expression

# History:

# 2013-2-3 on_1y First release

PATH=$PATH

Export PATH

While ["$choice"! = "yes"]

Do

Read-p "Give your choice [yes/no]:" choice

Done

Exit 0

Call:

The code is as follows:

$bash sh20.sh

Give your choice [yes/no]: no

Give your choice [yes/no]: no

Give your choice [yes/no]: nx

Give your choice [yes/no]: yes

7.2 for

The code is as follows:

#! / bin/bash

# Program:

# This program is used to demo for expression

# History:

# 2013-2-3 on_1y First release

PATH=$PATH

Export PATH

For choice in 1 2 3

Do

Echo "your choice is $choice"

Done

Exit 0

Call example:

The code is as follows:

$bash sh21.sh

Your choice is 1

Your choice is 2

Your choice is 3

Tracking and Debug of 8 shell script

Sh-n xx.sh # syntax check

Sh-x xx.sh # lists the execution process of xx.sh

Thank you for reading! This is the end of this article on "what is the use of Shell Script in linux?". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

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