Shell script app (for, while Loop statement and Case Branch statement)

Source: Internet
Author: User
Tags chmod

1, for: Read different variable values, execute the same set of commands one by one, until the value is complete exit, variable values are separated by a space

Grammar:

For variable value in value list

Do

Command sequence

Done

2, while: Repeated testing a condition, the establishment is executed, into the next loop, until the condition is not established

Grammar:

While "condition test"

Do

Command

To add a change test condition statement

Done

Example: Bulk add Users (STU1~STU20)

prefix= "Stu"

I=1

While "$i-le 20"

Do

Useradd ${prefix} $i

echo "123456" | passwd--stdin ${prefix} $i &>/dev/null

Let i++

Done

While condition is true: Represents always true, dead loop, must use Exit or Break command to end loop

Must be incremented to avoid a dead loop: Method:

Let i++ let i=i+1 i= ' expr i+1 '

3. Case: Perform different command sequences for different values of variables

Grammar:

Case Variable value in

Mode 1)

Command sequence 1

;;

Mode 2)

Command Sequence 2

;;

......

*)

default command sequence

Esac

When you take a value, the bracket "", which represents either, the horizontal bar-, the range of values, the vertical bar | , representing or

shell scripts (examples of applications for, while, and case statements)

One, for loop statements

1. Batch add users by Name list

Step: First create a name list Vim/root/users.txt and then create a script vim uaddfor.sh bulk Add Users

 

Authorize and execute to see if the user has created

2. Check host status based on IP address list

Step: First create an IP address list file Vim/root/padds.txt and then create the script vim chkhosts.sh

Authorize and Execute

Second, while Loop statement

1, batch add regular number of users

Step: Create a vim uaddwhile.sh script

Authorize and execute to see if the user has created

2. Guess the price game

Step: Create a script vim pricegame.sh

Authorize and Execute

III. Case Branch Statements

1. Check the type of characters entered by the user

Step: Create a script vim hitkey.sh

Authorize and Execute

2. Write system service Script

Step: Write a script vim Myprog

Authorize and Execute

Iv. Experimental Cases

1. Writing getarp.sh script files

(1) Send ARP request via arping command, record MAC address according to feedback result.

(2) Assign a network segment address (such as 192.168.4.) to the variable nadd as a prefix for the detection address.

(3) using the While loop statement, duplicate detection target and record MAC address, host address from 1-254.

The script is as follows:

[Email protected] ~]# VI getarp.sh

#!/bin/bash

# 1. Define the network segment address, Mac list file

Nadd= "192.168.4."

File= "/etc/ethers"

# 2. Send ARP requests and log feedback results

[-F $FILE] &&/bin/cp-f $FILE $FILE. old//Back up legacy files

Hadd=1 //define Start scan address

While [$HADD-LT 128]

Do

Arping-c 2-w 1 ${nadd}${hadd} &>/dev/null

If [$?-eq 0]; Then

Arp-n | grep ${nadd}${hadd} | awk ' {print $1,$3} ' >> $FILE

Fi

Let hadd++

Done

[Email protected] ~]# chmod +x getarp.sh

[[email protected] ~]#./getarp.sh //Execute inspection procedure

[[email protected] ~]# cat/etc/ethers //Confirm record Results

192.168.4.12 00:0c:29:c3:f8:51

192.168.4.110 00:50:56:c0:00:01

...//Omit part of the content

2. Writing scanhost.sh Scripts

(1) There are many ways to detect whether a host to open the anonymous FTP service, here to take the wget download tool to access the FTP root directory, if you can successfully list, it is considered anonymous FTP is turned on, otherwise considered off.

(2) Assign a value to the variable target by filtering out all IP addresses in the/etc/ethers file via the awk command.

(3) Use the FOR Loop statement, read the IP address in the target variable, and repeatedly probe the FTP open situation.

The script is as follows:

[Email protected] ~]# VI scanhost.sh

#!/bin/bash

target=$ (awk ' {print '} '/etc/ethers)

echo "The following hosts are open for anonymous FTP service:"

For IP in $TARGET

Do

wget ftp://$IP/&>/dev/null

If [$?-eq 0]; Then

Echo $IP

RM-RF index.html //delete temporary files generated by the test

Fi

Done

[Email protected] ~]# chmod +x scanhost.sh

[[email protected] ~]#./scanhost.sh //Execute script to confirm scan results

The following hosts are open for anonymous FTP services:

192.168.4.110

192.168.4.129

Article reference public number: L Baby talk about it

Shell script app (for, while Loop statement and Case Branch statement)

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.