Some simple shell script examples

Source: Internet
Author: User

For those who just started to learn shell scripts, it is very important to establish programming thinking. They must be able to express what they need to do in programming, the following are some examples of my learning and collection, which may be helpful to people I have just started to contact.

Evaluate the sum or product of any number. first define the function, the sum and product of any number, then define the variable through the case structure, and call the function.

Hei (){

X = 1


While [$ #-GT 0]


Do


X = 'expr $ x \ * $1'


Shift


Done


Echo $ x


}


He (){


 

 

X = 0


While [$ #-GT 0]


Do


X = 'expr $ x + $1'


Shift


Done


Echo $ x


}


Read-P "+ or *" lx


Case "$ lx" in


"+ ")


He $ *;;


"*")


Hei $ *;;


*)


Echo "no passage ";;


Esac


~

Use Case + function to write a STARTUP script for the source code website service so that you can start, stop, and serve the website service provided by the encoding package in the following way. ()

1 #! /Bin/bash


2 # chkconfig:-85 15


3 # Description: the Apache HTTP Server is an efficient and extensible \


4 # server implementing thecurrent HTTP standards.


5 # processname: httpd


6 start (){


7/user/loacal/httpd/bin/apachectl start


8 Echo "Source httpdrunning... OK"


9 echo


10}


11


12 stop (){


13/usr/local/httpd/bin/apachectl stop


14 echo "Source httpd running... no"


15 echo


16}


17 Status (){


18 netstat-anput | grep: 80 &>/dev/null


19 if [$? -EQ 0]; then


You can view the port and process, or create a folder During Start and Stop, and check whether there are any of the two files.

20 echo "running"


21 esle


22 echo "STOPPED"


23fi


24}


25 case "S1" in


26 "start ")


27 start ;;


28 "stop ")


29 stop ;;


30 "status ")


31 status ;;


32 *)


33 echo "$0 values in START | stop | status"


Esac

 

 

 

Simple shell script, input age and name,

#! /Bin/bash


Read-P "enter your name" Name


Read-P "Enter your age" Age


[! -Z $ name] & Echo "$ name" | echo "You did not enter a name"

[! -Z $ age] & Echo "$ age" | echo "You did not enter age"


 

 

Enter the card number and password,

#! /Bin/bash


Read-P "Enter your card num" Num


Stty-echo

Disable screen output

Read-P "enter your password" passwd


Stty echo


Echo


Echo $ num $ passwd>/tmp/a.txt

Redirect the output content to a.txt

Mail-s "psswd" root </tmp/a.txt

Send a.txt to root with the subject psswd]

 

 

Server_on.sh If a service is not running, start the service. If the service is running and the "service is running" command is executed as follows:


Server_on.sh httpd


Server_on.sh crond

#! /Bin/bash


Service $1 Status &>/dev/null


If [$? -Ne 0];


Then


Service $1 start


Fi


Echo "the service is running"


~

 

Determine the advantages and disadvantages of a student based on the score.


The score is between 1 and 1 ~ Between 100


 

 

In 1 ~ 59 retake


60 ~ 79 Liang


80 ~ 90 excellent


91 ~ 100 rewards

1 #! /Bin/bash


2 read-P "Enter your score" Num


3 if [$ num-ge 1] &>/dev/null & [$ num-Le 100];


(Note: If the input is empty or contains other letters, an error is returned. You can redirect the output to solve this problem)

4 then


5 if [$ num-ge 1] & [$ num-Le 59];


6 then


7 Echo "retake required"


8 Elif [$ num-ge 60] & [$ num-Le 79];


9 then


10 echo "good"


11 Elif [$ num-ge 80] & [$ num-Le 90];


12 then


13 echo "excellent"


14 Elif [$ num-ge 91] & amp; [$ num-Le 100];


15 then


16 echo "rewarded"


17 else


18 "Enter your score range: 1-100"


19fi


20 else


21 echo "Enter your score range: 1-100"


22fi


~


~

 

Add ten system accounts

 

#! /Bin/bash


Name = Zeng


For N in 'seq-W 10'

SEQ-W: Set the width based on the maximum number, for 01 02 10

Do


Useradd $ name $ n


Done


#! /Bin/bash


# Output 21 ~ Number of odd numbers in 57


X = 0


For I in 'seq 21 57'


Do


If ['expr $ I % 2'-Ne 0]; then


Echo $ I


Let X ++


Fi


Done


Echo $ x

 

 

 

 

Output an even number between 1-

 

 

#! /Bin/bash


For N in 'seq 100'


Do


If ['expr $ n % 2'-EQ 0];


Then


Echo $ n


Fi


Done


~

 

 

 

Output 1-20

#! /Bin/bash


I = 20


While [$ I-ge 1]


Do


Echo $ I


Let I --


Done


Echo


~

 

 

Use the while loop output IP address 192.168.4.1-192.168.4.20

#! /Bin/bash


IP = 192.168.4.


I = 20


While [$ I-ge 1]


Do


Echo $ IP $ I


Let I --


Done


Echo


~

 

 

Check whether any host in the 192.168.4.0/24 network segment is online

#! /Bin/bash


IP = 192.168.4.


I = 1


Until [$ I-GT 254]


Do


Ping $ IP $ I


Let I ++


Done


Echo

The above are some shell examples I have collected. I hope this will be helpful to you. If you have any shortcomings, please advise me.

Some simple shell script examples

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.