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 are the useful Shell scripts

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

Share

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

This article mainly explains the "what are the useful Shell scripts", the content of the explanation is simple and clear, easy to learn and understand, please follow the editor's ideas slowly in depth, together to study and learn what useful Shell scripts!

[example: 001] judge 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] find the average

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" >

)); 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] self-decreasing 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:

# list of names

# 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 results

#. / namelist.sh

Name= Jame

Name= Bob

Name= Tom

Name= Jerry

Name= Sherry

Name= Alice

Name= John

[example: 005] batch test files exist

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 files of specified size

The code is as follows:

[root@host ~] # df-ih / tmp

Filesystem Inodes IUsed IFree IUse% Mounted on

/ dev/mapper/vg00-lvol5

1000K 3.8K 997K 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=counter00))

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=1M count=1

[example: 006] read the configuration file by traversing

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

ITunes 0

While read LINE;do

Hosts_allow [$I] = $LINE

((iTunes +))

Done < hosts.allow

For ((iTun1)

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