Day02 Script Exercises

Source: Internet
Author: User

four.sh output The arithmetic result of any 2 numbers entered by the user from the health disk

If the user has a value that is not entered, the script is terminated


#!/bin/bash

Read-p "Please enter the first number:" Num1

Read-p "Please enter a second number:" Num2

[-Z $num 1] && exit

[-Z $num 2] && exit

sum= ' expr $num 1 + $num 2 '

jian= ' expr $num 1-$num 2 '

cheng= ' expr $num 1 \* $num 2 '

chu= ' expr $num 1/$num 2 '

echo "$num 1 + $num 2 = $sum"

echo "$num 1-$num 2 = $jian"

echo "$num 1 * $num 2 = $cheng"

echo "$num 1/$num 2 = $chu"


2, four_2.sh the calculation of any two by the position of the variable to pass the way to the script calculation, if

Scripts do not add positional parameters or just one positional argument, and the script does not execute


#!/bin/bash

[-Z] && exit

[-Z] && exit

sum= ' expr + $ '

Jian= ' expr $-$ '

cheng= ' Expr $ \*

chu= ' expr $/$ '

echo "$ + $ = $sum"

echo "$-$ = $jian"

echo "$ * $ = $cheng"

echo "$/$ = $chu"

#sh four_2.sh 3 5



3, very.ser.sh (check the running status of any one service)


Check the status of any service vsftpd httpd sshd crond, MySQL, if not the service in these 5,

Prompts the user to check the service name and exits the script


If the service is running, output "service name is running"


Start the service if the service is not running



Method 1: Use read to write the script

#!/bin/bash

Read-p "Please enter your service name:"

if [$service! = ' Crond ' a $service! = ' httpd ' a $service! = ' sshd '-a $service! = ' mysqld '-a $service! = ' vsftpd '];th En

echo "Can only check ' vsftpd,httpd,crond,mysqld,sshd"

Exit 5

Fi

Service $service Status &>/dev/null


If [$?-eq 0];thhen

echo "Service Online"

Else

Service $service Start

Fi


Or


Method 2: Use positional variables to write scripts

If [-Z $];then

echo "You mast specify a servername!"

echo "Usage: ' basename$0 ' servername"

Exit 2

Fi

if [= = = "Crond"] | | [$ = = "MySQL"] | | [= = = "Sshd"] | | [$ = = "HTTPd"] | | [$ = = "VSFTPD"];then

Service Status &>/dev/null

If [$?-eq 0];then

echo "is running"

Else

Service start

Fi

Else

echo "Usage: ' basename" server name "

echo "But only check for vsftpd httpd sshd crond mysqld" && exit2

Fi


4, three_server.sh This script can check the running status of MySQL Crond sshd 3 services in turn


#!/bin/bash

M=mysqld

C=crond

S=sshd


Service $m Status &>/dev/null


If [$?-eq 0];then


echo "Mysqld Service Online"

Else

echo "Mysqld service is not online"

Fi


Service $c Status &>/dev/null


If [$?-eq 0];then


echo "Crond Service Online"

Else

echo "Crond service is not online"

Fi


Service $s status &>/dev/null



If [$?-eq 0];then


echo "sshd Service Online"

Else

echo "sshd service is not online"

Fi



5. Judging students ' achievements according to students ' input

Student scores range in 1-100

Result score greater than or equal to 1 and less than 60 output failed

Result score is greater than or equal to 60 and less than 80 output is good

Score greater than or equal to 80 and less than or equal to 100 output excellent

If the input score exceeds 1-100, the output indicates that the scores range in the 1-100 interval


#!/bin/bash

Read-p "Please enter your score:" Fenshu

If [-Z $fenshu];then

echo "Score cannot be empty"

Exit

elif [$fenshu-ge 1-a $fenshu-lt];then

echo "Failed"

elif [$fenshu-ge 60-a $fenshu-lt];then

echo "Good"

elif [$fenshu-ge 80-a $fenshu-le];then

echo "Excellent"

Else

echo "Score in 1-100 range"

Fi



6, pri-pc5.sh the 192.168.1.0 network segment host IP address output to the screen


#!/bin/bash

For i in ' seq 254 '

Do

Echo $IP. $i

Done


7, pri-binlog.sh the current existing Binlog log output to the screen (mysqld-bin.000001.), and copied to the root clip

#!/bin/bash

For i in ' Cat/var/lib/mysql/*.index '

Do

I= ' echo $i | Awk-f "/" ' {print $} '

Cd/var/lib/mysql

CP $i/

Done



8, binlogbackup.sh

Back up the newly generated binlog file,

Back up the newly generated binlog log files of the database server into the/logbak directory of the system (prepared unprepared)

The Binlog log files that are being used are not backed up


Method 1:

#!/bin/bash

#备份数据库服务器新生成的binlog日志文件到系统的 the/logbak directory;

#正在使用的binlog文件不备份

Binlogurl= ' Cat/var/lib/mysql/mysqld-bin.index | Awk-f "/" ' {print $} ' #获取数据库名字

Newlog= ' Cat/var/lib/mysql/mysqld-bin.index | Awk-f "/" ' {print $} ' | Tail-1 ' #获取数据库最后一行正在使用的binlog文件名字

x= ' Cat/var/lib/mysql/mysqld-bin.index | Wc-l ' #获取日志文件的行数

Y= ' Ls/logbak | Wc-l ' #获取备份目录的行数

z= ' expr $x-$y '#日志文件行数-Number of file directory rows backed up

Dblog= ' Cat/var/lib/mysql/mysqld-bin.index | Awk-f "/" ' {print $} ' | tail-$z ' #获取没备份的数据库名字

if [!-e/logbak];then

Mkdir/logbak

For BL in $binlogurl

Do

cp/var/lib/mysql/$BL/logbak

Done

rm-rf/logbak/$newlog

Else

For BL in $dblog

Do

cp/var/lib/mysql/$BL/logbak

Done

rm-rf/logbak/$newlog

Fi


Or


Method 2:

#!/bin/bash

Datadir=/var/lib/mysql

Backdir=/logbak

Lastfilename= ' Tail-1 $datadir/mysqld-bin.index | Awk-f "/" ' {print $} '

if [!-e $backdir];then

mkdir $backdir

Fi

For logname in ' Cat $datadir/mysqld-bin.index '

Do

Filename= ' echo $logname | Awk-f "/" ' {print $} '

If ["$filename" = = "$lastfilename"];then

echo "$lastfilename using next Backup" >/dev/null

Else

if [!-e $backdir/$filename];then

CP $datadir/$filename $backdir

Else

echo "$filename has been backed up" >/dev/null

Fi

Fi

Done


9, pc_noline.sh

IP address of the online host in the output 192.168.1.0/24 network segment

Count the number of non-online hosts,

and save the IP address and the time that is not in the line host to the/tmp/ip.txt file.


#!/bin/bash

ip=192.168.1.

J=0

For i in ' seq 10 12 '

Do

Ping-c 3 $ip $i &>/dev/null

If [$?-eq 0];then

Echo Online hosts are: $ip $i

Else

Let J + +

echo $ip $i >>/tmp/ip.txt

Date >>/tmp/ip.txt

Fi

Done

Echo does not have a line host number of $j



10. The number of even and odd numbers between 1 and 10 output

J=0

#!/bin/bash

For i in ' seq 10 '

Do

If [' Expr $i% 2 '-eq 0];then

Echo $i

Esle

Let J + +

Fi

Done

Echo $j



11. Add 10 users and Passwords


#!/bin/bash

J=wsyht

M=abcd

For i in ' seq 3 '

Do

Useradd $j $i

echo $m $i | passwd--stdin $j $i

Done


This article is from the "Wsyht blog" blog, make sure to keep this source http://wsyht2015.blog.51cto.com/9014030/1790406

Day02 Script Exercises

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.