DAY04 Script Exercises

Source: Internet
Author: User
Tags define function

1. Define function Sum_two in fun.sh script (function is able to calculate sum of any two numbers)

#!/bin/bash

#yi =$1

#er =$2

If [-Z $yi] | | [-Z $er];then

Echo number cannot be empty

If

Sum_two () {

#sum = ' expr $yi + $er '

sum= ' expr + $ '

#echo "$yi + $er = $sum"

echo "$ + $ = $sum"

}

#sum_two $

Sum_two $*


Or


#!/bin/bash

Read-p "Please enter the first number you want to calculate:" Yi

Read-p "Please enter the second number you want to calculate:" ER

If [-Z $yi] | | [-Z $er];then

echo "Number cannot be empty"

Exit

Fi

Sum_two () {

sum= ' expr $yi + $er '

echo "Their and is $sum"

}

Sum_two



2, js_four.sh (for any one number do arithmetic, and the results of the operation output, and can be based on

The user chooses the type of operation to do the operation)


#!/bin/bash


#!/bin/bash

Jia () {

I=0

While [$#-GT 1]

Do

i= ' expr $i + $

Shift

Done

Echo $i

}


Jian () {

i= ' Expr 2 \*

While [$#-GT 1]

Do

i= ' expr $i-$ '

Shift

Done

Echo $i

}



Cheng () {

I=1

While [$#-GT 1]

Do

i= ' expr $i \*

Shift

Done

Echo $i

}


Chu () {

i= ' Expr \*

While [$#-GT 1]

Do

i= ' expr $i/$

Shift

Done

Echo $i

}

Case $ in

+)

Jia $*;;

-)

Jian $*;

X

Cheng $*;

/)

Chu $*;;

*)

echo "can only be selected | + | - | x | \ |"

Esac


Test sh js_four.sh + 12 13


Or


#!/bin/bash

Read-p "Please enter the symbol for the operation you want to select | + | - | x | / | The NUM

Jia () {

I=0

While [$#-GT 0]

Do

i= ' expr $i + $ '

Shift

Done

Echo $i

}


Jian () {

i= ' expr 2 \* $ '

While [$#-GT 0]

Do

i= ' expr $i-$ '

Shift

Done

Echo $i

}



Cheng () {

I=1

While [$#-GT 0]

Do

i= ' expr $i \* $ '

Shift

Done

Echo $i

}


Chu () {

I= ' expr $ \* $ '

While [$#-GT 0]

Do

i= ' expr $i/$ '

Shift

Done

Echo $i

}

Case $num in

+)

Jia $*;;

-)

Jian $*;

X

Cheng $*;

/)

Chu $*;;

*)

echo "can only be selected | + | - | x | \ |"

Esac


Test SH js_four.sh 12 13

Please enter a symbol for the operation you want to select | + | - | x |  / | +



3, the test.sh script defines a function called JC, the function is to calculate the factorial of any number of users

#!/bin/bash

Shu () {

J=$1

I=1

If [-Z $j] | | [$j-eq 0];then

echo "0"

Exit

Fi

While [$j-GT 0]

Do

i= ' expr $i \* $j '

Let j--

Done

echo "$i"

}

Shu $*


Or


#!/bin/bash

JC () {

Read-p "Please enter the order multiplier you want to count" N

If [-Z $n] | | [$n-eq 0]; then

Echo 0;exit

Fi

J=1

For ((i=1;i<= $n; i++))

Do

j= ' expr $j \* $i '

Done

Echo $j

}

Jc




4, install the source code package website Service-rhel5.

Can use service apached start | Stop | Status stops starting and viewing the source Web server



Lock File method

#vi/etc/init.d./httpd


#!/bin/bash

# CHKCONFIG:35 55 25

# description:secure Apache Server

# PROCESSNAME:HTTPD

Start () {

/usr/local/httpd/bin/apachectl start

echo "Start httpd: [OK]"

Touch/usr/local/httpd/httpd.lock start

}



Stop () {

/usr/local/httpd/bin/apachectl stop

echo "Stop httpd: [OK]"

Rm-rf/usr/local/httpd/httpd.lock

}


Status () {

if [-e/usr/local/httpd/httpd.lock];then

echo "httpd already Start"

Else

echo "httpd has been stopped"

Fi

}



Case $ in

Start

start;;

Stop

stop;;

Status

status;;

*)

echo "Usage:/etc/init.d/httpd {start|stop|status}"

Esac


#chmod +x/etc/init.d/httpd

#service httpd Start

#chkconfig--add httpd;chkconfig--list httpd

Or




#vi/etc/init.d./httpd


#!/bin/bash

# chkconfig:2345 55 25

# Description:source Apache Server

# PROCESSNAME:HTTPD

Start () {

/usr/local/httpd/bin/apachectl start

echo "Start httpd: [OK]"



Stop () {

/usr/local/httpd/bin/apachectl stop

echo "Stop httpd: [OK]"


}status () {

NETSTAT-TULNP grep:httpd%>/dev/null

If [$?-eq 0];then

echo "Service in"

Else

echo "Service is no on"

}


Case $ in

Start

start;;

Stop

stop;;

Status

status;;

*)

echo "Usage:/etc/init.d/httpd {start|stopstatus}"

Esac

#chmod +x/etc/init.d/httpd

#service httpd Start

#chkconfig--add httpd;chkconfig--list httpd


5, arr.sh

Save the user's input name from the health disk into the array stugrp

and outputs the element value of element 2nd of the STUGRP array


Outputs all elements of the STUGRP array


The way the user loops the structure outputs all elements of the STUGRP array


Method 1:

#!/bin/bash

stugrp= ($*)

For ((i=0;i< ' echo ${#stugrp [@]} '; i++))

Do

echo ${stugrp[$i]}

Done


Method 2

#!/bin/bash

Read-p "Input name1" stugrp[0]

Read-p "Input name1" stugrp[1]

Read-p "Input name1" stugrp[2]

Echo ${stugrp[1]}

I=0

While [$i-le 2]

Do


echo ${stugrp[$i]}

Let i++

Done



Method 3

#!/bin/bash

Read-p "Please enter your user name:" Name

Read-p "Please enter your username:" name1

Read-p "Please enter your username:" Name2

stugrp= ($name $name 1 $name 2)

Echo ${stugrp[1]}

Echo

I=0

While [$i-le 2]

Do


echo ${stugrp[$i]}

Let i++

Done


Method 4

For i in ' echo ${stugrp[@]} '

Do

Echo $i

Done



6, the loop structure to the STUGRP array assignment, (according to save the number of names to the array value)



#!/bin/bash

Read-p "Please enter the number you want to enter:" Sum

Read-p "Please enter the name you want to enter:" Name

While [-Z $sum]

Do

echo "Message cannot be empty"; exit

Done

szgrp= ($name)

I=0

If [$sum-eq ${#szgrp [*]}];then

While [$i-lt $sum]

Do

echo ${szgrp[$i]}

Let i++

Done

Else

echo "The number of names you enter needs to match the number you entered"

Fi


Or


#!/bin/bash

Declare-i NUM

Read-p "How many names do you want to enter?:" NUM

Declare-a STUGRP

Declare-i i=0

While [$i-lt $NUM]

Do

Declare-i ii=i+1

Read-p "Enter $II First Name:" Stugrp[$i]

Let ++i

Done


Echo---------------------------------------------


Num=${stugrp[*]}

For ((I=0;i<num;++i))

Do

echo ${stugrp[$i]}

Done


7. mysql+cluster.sh (Check the physical connectivity of each server in MySQL cluster)


How the script executes:

./mysql_cluster.sh Check server number of server IP address list


For example:

./mysql_cluster.sh 3 1.1.1.1 1.1.1.2 1.1.1.3


Ability to change user input check the number of servers and check for IP addresses

Enter the number of IP addresses to match the number of check servers

If the user executes the script, the execution method is incorrect, prompting the user for the correct execution of the script.

Save the server IP address and the offline time in the/clusterdir/notline.txt file

Enter the IP address of the online host and the number of non-online hosts



8, slave_status.sh (check MySQL slave structure from the database server state)

1. Whether the local database service is running

2. Can communicate with the primary database server properly

3. Can I use an authorized user to connect to the database server

4, the local slave_io process is in the Yes State

Whether the slave_sql process in this machine is in the Yes State


#!/bin/bash

NETSTAT-TULNP | grep:3306 >/dev/null

If [$?-eq 0];then

echo "Service is running"

Else

Service mysqld Start

Fi

Ping-c 3 192.168.1.100 &>/dev/null

If [$?-eq 0];then

echo "Network connection OK"

Else

echo "Network connection Failed"

Fi

mysql-h192.168.1.100-uroot-p123456 &>/dev/null

If [$?-eq 0];then

echo "Database connection succeeded"

Else

echo "Database connection Failed"

Fi

io= mysql-uroot-p123-e "show Slave Status\g" | grep slave_io_running | awk ' {print $ ' >/dev/null

Sql= mysql-uroot-p123-e "show Slave Status\g" | grep slave_sql_running | awk ' {print $} '/dev/null

If [Io==yes] && [Sql==yes];then

echo "IO and SQL Connection succeeded"

Else

echo "IO thread and SQL thread connection failed"

Fi




9, rename_file.sh (bulk modification of files in the current directory)

Modify the file ending in. txt to end with. doc

MV Image.txt Image-1.doc

Web.txt Web-2.doc

Test.txt Test-3.doc



#!/bin/bash

For i in ' LS??. Sh '

Do

file=${i%.*}

MV $file. Sh $file. doc

Done


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

DAY04 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.