8 practical Shell scripts

Source: Internet
Author: User

8 practical Shell scripts

This article mainly introduces eight practical Shell scripts, this article provides practical scripts to determine whether the input is a number, character, or other, calculate the average, auto-subtraction output, add a prefix to the file, and check whether the batch test file exists. For more information, see

Several Shell script examples, I think it is not bad.

[Example: 001] determines whether the input is a number, character, or other

The Code is as follows:

#! /Bin/bash

Read-p "Enter a number or string here:" input

Case $ input in

[0-9]) echo-e "Good job, Your input is a numberic! \ N ";;

[A-zA-Z]) echo-e "Good job, Your input is a character! \ N ";;

*) Echo-e "Your input is wrong, input again! \ N ";;

Esac

[Example: 002] calculates the mean.

The Code is as follows:

#! /Bin/bash

# Calculate the average of a series of numbers.

SCORE = "0"

AVERAGE = "0"

SUM = "0"

NUM = "0"

While true; do

Echo-n "Enter your score [0-100%] ('q' for quit):"; read SCORE;

If ("$ SCORE" <"0") | ("$ SCORE"> "100"); then

Echo "Be serious. Common, try again :"

Elif ["$ SCORE" = "q"]; then

Echo "Average rating: $ AVERAGE % ."

Break

Else

SUM = $ [$ SUM + $ SCORE]

NUM = $ [$ NUM + 1]

AVERAGE = $ [$ SUM/$ NUM]

Fi

Done

Echo "Exiting ."

[Example: 003] auto-subtraction output

The Code is as follows:

[Scriptname: doit. sh]

While ($ #> 0 ))

Do

Echo $ *

Shift

Done

/>./Doit. sh a B c d e

A B c d e

B c d e

C d e

D e

E

[Example: 004] Add a prefix to the file

The Code is as follows:

# Name List

# Cat namelist

Jame

Bob

Tom

Jerry

Sherry

Alice

John

# Script program

# Cat namelist. sh

#! /Bin/bash

For name in $ (cat namelist)

Do

Echo "name =" $ name

Done

Echo "The name is out of namelist file"

# Output result

#./Namelist. sh

Name = Jame

Name = Bob

Name = Tom

Name = Jerry

Name = Sherry

Name = Alice

Name = John

[Example: 005] whether a batch test file exists

The Code is as follows:

[Root @ host ~] # Cat testfile. sh

#! /Bin/bash

For file in test *. sh

Do

If [-f $ file]; then

Echo "$ file existed ."

Fi

Done

[Root @ host ~] #./Testfile. sh

Test. sh existed.

Test1.sh existed.

Test2.sh existed.

Test3.sh existed.

Test4.sh existed.

Test5.sh existed.

Test78.sh existed.

Test_dev_null.sh existed.

Testfile. sh existed.

[Example: 005] fill the hard disk with a specified size file

The Code is as follows:

[Root @ host ~] # Df-ih/tmp

Filesystem Inodes IUsed IFree IUse % Mounted on

/Dev/mapper/vg00-lvol5

1000 kb 3.8 K 997 K 1%/tmp

[Root @ host ~] # Cat cover_disk.sh

#! /Bin/env bash

Counter = 0

Max = 3800

Remainder = 0

While true

Do

(Counter = counter + 1 ))

If [$ {# counter}-gt $ max]; then

Break

Fi

(Remainder = counter % 1000 ))

If [$ remainder-eq 0]; then

Echo-e "counter = $ counter \ tdate =" $ (date)

Fi

Mkdir-p/tmp/temp

Cat <testfile> "/tmp/temp/myfile. $ counter"

If [$? -Ne 0]; then

Echo "Failed to write file to Disk ."

Exit 1

Fi

Done

Echo "Done! "

[Root @ host ~] #./Cover_disk.sh

Counter = 1000 date = Wed Sep 10 09:20:39 HKT 2014

Counter = 2000 date = Wed Sep 10 09:20:48 HKT 2014

Counter = 3000 date = Wed Sep 10 09:20:56 HKT 2014

Cat: write error: No space left on device

Failed to write file to Disk.

Dd if =/dev/zero of = testfile bs = 1 M count = 1

[Example: 006] reads the configuration file through traversal.

The Code is as follows:

[Root @ host ~] # Cat hosts. allow

127.0.0.1

127.0.0.2

127.0.0.3

127.0.0.4

127.0.0.5

127.0.0.6

127.0.0.7

127.0.0.8

127.0.0.9

[Root @ host ~] # Cat readlines. sh

#! /Bin/env bash

I = 0

While read LINE; do

Hosts_allow [$ I] = $ LINE

(I ++ ))

Done

For (I = 1; I <=$ {# hosts_allow [@]}; I ++); do

Echo $ {hosts_allow [$ I]}

Done

Echo "Done"

[Root @ host ~] #./Readlines. sh

127.0.0.2

127.0.0.3

127.0.0.4

127.0.0.5

127.0.0.6

127.0.0.7

127.0.0.8

127.0.0.9

Done

[Example: 007] simple regular expression Application

The Code is as follows:

[Root @ host ~] # Cat regex. sh

#! /Bin/env sh

# Filename: regex. sh

Regex = "[A-Za-z0-9] {6 }"

If [[$1 = ~ $ Regex]

Then

Num = $1

Echo $ num

Else

Echo "Invalid entry"

Exit 1

Fi

[Root @ host ~] #./Regex. sh 123abc

123abc

#! /Bin/env bash

# Filename: validint. sh

Validint (){

Ret = 'echo $1 | awk' {start = match ($1,/^ -? [0-9] + $/); if (start = 0) print "1"; else print "0 "}''

Return $ ret

}

Validint $1

If [$? -Ne 0]; then

Echo "Wrong Entry"

Exit 1

Else

Echo "OK! Input number is: "$1

Fi

[Example: 008] simple backup file by date

The Code is as follows:

#! /Bin/bash

NOW = $ (date + "% m-% d-% Y") # current date

FILE = "backup.w.now.tar.gz" # backup FILE

Echo "Backing up data to/tmp/backup.w.now.tar.gz file, please wait..." # print information

Tar xcvf/tmp/backup.w.now.tar.gz/home/etc/var # Back up multiple files to the specified tar compressed file at the same time

Echo "Done ..."

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.