Scripting for Bash programming until statements and formatted hard disk partitions in Linux (10)

Source: Internet
Author: User

Scripting for Bash programming until statements and formatted hard disk partitions in Linux (10)

1. Summary of circular statement structure

1.1.while Statement Enter the Loop statement when the condition is met

While condition; Do

Statement

Done

1.2.until Statement Enter the Loop statement when the condition is not satisfied

until conditions; Do

Statement

Done


1.3.for variable in list; Do

Loop body

Done

1.4.for ((expression 1; condition; expression 2)); Do

Loop body

Done

2. Write the following script to test whether all hosts between 1.1.1.1 and 1.1.1.30 are online through the ping command,

If it is online, it displays "$IP is up."

If you are not on a line, the "$IP is down" appears.

#!/bin/bash

Ip=1

Until [$IP-GT 30]; Do

If Ping-c 1-w 1 1.1.1. $IP &>/dev/null; Then

echo "1.1.1. $IP is up"

Else

echo "1.1.1. $IP is down"

Fi

ip=$[$IP +1]

Done

3. Write a script (the virtual machine added a hard disk to facilitate the test script)

1, list all the disks on the current system, let the user choose, if you choose Quit to quit the script, if the user chooses the error, let the user choose again;

2, when the user selects the designated hard disk, prompts the user next action will delete all hard disk data, and ask the user to confirm, if the user chooses Y or y to continue, n or N or exit exit; otherwise, let the user re-select;

3. Erase all partitions on the hard drive (prompt, erase all partitions after the Sync command, and let the script sleep for 5 seconds before partitioning), and create three primary partitions for it, the first one is 200M, the second is 100G, the third is 2G, and the third is the swap partition type; Hint: The partition command is delivered to Fdisk via Echo)

#!/bin/bash

Debug=0

Fdisk-l 2>/dev/null| grep "^disk/dev/[sh]d[a-z]" |awk-f: ' {print $} ' displays all current hard drives

echo "Quit"

Read-p "Your choice:hda|sda|sdb|quit .... "CHOICE

while [$CHOICE! = "Quit"]; Do

Case $CHOICE in

HDA|SDA|SDB)

Debug=1

disk= $CHOICE Remove the selected drive

Choice= "quit";;

*)

Read-p "Again,your choice:hda|sda|sdb|quit .... "CHOICE

;;

Esac

Done

If [$DEBUG-eq 1]; Then

Read-p "You'll earse all Data:y|n|exit" OPTION

until [$OPTION = = "Exit"]; Do

Case $OPTION in

y| Y

DD If=/dev/zero of=/dev/$DISK bs=512 count=1 Delete the specified hard disk partition

Sync

Sleep 5

echo "N begins repartitioning of the specified hard disk

P

1


+200m

N

P

2


+200g

N

P

3


+2g

T

3

82

W "| fdisk/dev/$DISK

Option= "Exit";;

n| N

Exit 6;;

*)

Read-p "Again,you'll earse all Data:y|n" OPTION;;

Esac

Done

Fi


This article is from the "Xavier Willow" blog, please be sure to keep this source http://willow.blog.51cto.com/6574604/1766620

Scripting for Bash programming until statements and formatted hard disk partitions in Linux (10)

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.