Linux Learning -10-Learning shell scripts

Source: Internet
Author: User
Tags date1 integer numbers

[Email protected] ~]# mkdir scripts; CD scripts

[Email protected] scripts]# VI sh01.sh

#!/bin/bash declaring shell names

# program: Description of Procedure Content

# This program shows the "Hello world!" in the Your screen.

# History:

# 2005/08/23 Vbird First release path=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin main environment variable declaration

Export PATH

Echo-e "Hello world! \a \ n "Main program section

Exit 0 Execution Result return value



Execution: chmod a+x sh01.sh;./sh01.sh


Interactive scripting

[Email protected] scripts]# VI sh02.sh

#!/bin/bash

# program:

# User inputs his first name and last name. Program shows he full name.

# History:

# 2005/08/23 Vbird First release Path=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin

Export PATH

Read-p "Please input your first name:" FirstName # Prompts the user to enter

Read-p "Please input your last name:" LastName # Prompts the user to enter

Echo-e "\nyour full name is: $firstname $lastname" # Results from Screen output


Change with date

[Email protected] scripts]# VI sh03.sh

#!/bin/bash

# program:

# program creates three files, which named by user ' s input

# and Date command.

# History:

# 2005/08/23 Vbird First release Path=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin

Export PATH

# 1. Let the user enter the file name, and get fileuser this variable;

Echo-e "I'll use ' touch ' command to create 3 files." # Display information purely

Read-p "Please input your filename:" Fileuser # Prompts the user to enter

# 2. In order to avoid the user arbitrarily press Enter, the use of variable function analysis file name is set?

filename=${fileuser:-"FileName"} # starts to determine if there is a profile name

# 3. Start using the date command to get the required file name;

date1=$ (Date--date= ' 2 days ago ' +%y%m%d) # Date of the previous two day

date2=$ (Date--date= ' 1 days ago ' +%y%m%d) # Date of the day before

date3=$ (Date +%y%m%d) # Today's date

FILE1=${FILENAME}${DATE1} # Bottom three lines in config file name

FILE2=${FILENAME}${DATE2}

File3=${filename}${date3}

# 4. Create a file name!

Touch "$file 1" # Bottom three lines in creating the file

Touch "$file 2"

Touch "$file 3"


Numeric operations

[Email protected] scripts]# VI sh04.sh

#!/bin/bash

# program:

# User inputs 2 integer numbers; Program would cross these and numbers.

# History:

# 2005/08/23 Vbird First release Path=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin

Export PATH

ECHO-E "You should input 2 numbers, I'll cross them! \ n "

Read-p "First Number:" Firstnu

Read-p "second number:" Secnu

total=$ (($FIRSTNU * $secnu))

Echo-e "\nthe result of $firstnu x $SECNU is ==> $total"

[[email protected] scripts]# echo $ ((13% 3))

1


Test function

TEST-E/dmtsai && echo "Exist" | | echo "not exist"

P453


# 1. Let the user enter a file name and determine if the user actually has an input string?

ECHO-E "Please input a filename, I'll check the filename ' s type and \

Permission. \ n "

Read-p "Input a filename:" filename

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


# 2. Determine if the file exists? Show message and end script if not present

Test! -E $filename && echo "The filename ' $filename ' do not exist ' && exit 0


# 3. Start judging file types and properties

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

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

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

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

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


# 4. Start outputting information!

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

echo "and the permissions are: $perm"


Using judgment symbols []

[-"$HOME"-==-"$MAIL"-]

[To add a space]

Read-p "Please input (y/n):" yn

["$yn" = = "Y"-o "$yn" = = "Y"] && echo "OK, continue" && exit 0

["$yn" = = "n"-o "$yn" = = "n"] && echo "Oh, interrupt!" && exit 0

echo "I don ' t know your choice is" && exit 0

Default variable $ P457

Shift offset Variable


Conditional judgement Type If...then

If [conditional judgment]; Then when the conditional judgment is established, the instruction work content can be carried out;

Fi <== The IF in turn, it becomes fi! End the IF idea!

Multi-criteria

&& representative and;

|| represent or;

The last example changes to a single condition judgment

Read-p "Please input (y/n):" yn

If ["$yn" = = "Y"] | | ["$yn" = = "Y"]; Then

echo "OK, continue"

Exit 0

Fi

If ["$yn" = = "N"] | | ["$yn" = = "n"]; Then

echo "Oh, interrupt!"

Exit 0

Fi

echo "I don ' t know your choice is" && exit 0


# A conditional judgment, sub success and failure (else)

If [conditional judgment]; Then

When the conditional judgment is established, the instruction work content can be carried out;

Else

When the conditional judgment is not established, the instruction work content can be carried out;

Fi

# Multiple conditional judgments (if ... else) are performed in a number of different situations (if elif ... elif)

If [conditional judgment type one]; Then

When the conditional judgment is established, the instruction work content can be carried out;

elif [conditional judgment type II]; Then

When the conditional judgment two is established, the instruction work content can be carried out;

Else

When the conditional judgment one and two are not established, the instruction work content can be carried out;

Fi

Multi-Conditional judgment

Read-p "Please input (y/n):" yn

If ["$yn" = = "Y"] | | ["$yn" = = "Y"]; Then

echo "OK, continue"

elif ["$yn" = = "N"] | | ["$yn" = = "n"]; Then

echo "Oh, interrupt!"

Else

echo "I don t know your choice is"

Fi

=====================================

# 1. Let's do some telling.

echo "Now, I'll detect your Linux server ' s services!"

Echo-e "The WWW, ftp, ssh, and mail'll be detect! \ n "

# 2. Start doing some testing, and also output some information!

testing=$ (Netstat-tuln | grep ": 80") # Detect see Port 80 in no?

If ["$testing"! = ""]; Then echo "WWW was running in your system."

Fi

testing=$ (Netstat-tuln | grep ": 22") # detect see Port 22 in no?

If ["$testing"! = ""]; Then

echo "SSH is running in your system."

Fi

testing=$ (Netstat-tuln | grep ": 21") # Detect see Port 21 in no?

If ["$testing"! = ""]; Then

echo "FTP is running in your system."

Fi

testing=$ (Netstat-tuln | grep ": 25") # Detect see port 25 in no?

If ["$testing"! = ""]; Then

echo "Mail is running in your system."

Fi

Use case ... Esac

Case $ variable name in <== keyword for case, there are variables before the rich font size

"First variable content") <== the contents of each variable are suggested to be enclosed in double quotation marks, and the key words are parentheses.

Program Segment

;; <== use two consecutive semicolons to handle each category end!

"Second variable content")

Program Segment

;;

*) <== The last variable content will use * to represent all other values

Other program execution segments that do not contain the contents of the first variable and the contents of the second variable

Exit 1

;;

Esac <== The final case ending! "Turn to write" Think about it!


Case $ in

"Hello")

echo "Hello, how is it?"

;;

"")

echo "You must input parameters, ex> {Someword}"

;;

*) # In fact, the equivalent of a wildcard, 0~ infinite number of arbitrary characters meaning!

echo "Usage $ {Hello}"

;;

Esac


function functions

function fname () {

Program Segment

}

Loop loop

While doing done, the until do-done indeterminate loop

While [condition] <== the state inside the brackets is the judging formula

Do <==do is the beginning of the loop!

Procedure Paragraph

Done <==done is the end of the loop


until [condition]

Do

Procedure Paragraph

Done

For...do...done (fixed cycle)

For Var in con1 con2 Con3 ...

Do

Program Segment

Done

Numerical processing of For...do...done

for (initial value; limit value; execution step))

Do

Program Segment

Done

Shell script Tracking and debug



Linux Learning -10-Learning shell scripts

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.