Linux command exercise: script to implement HDD partitioning function

Source: Internet
Author: User


Practice a

1, the ping command test 10.109.134.233 to 10.109.134.249 all hosts are online;

If it is online, the IP is up will be replaced with a real IP address and displayed in green;

If not the line, the "IP is down" where the IP to be replaced by a real IP address, and displayed in red;

Requirements: Use Whil,until and for (two form) loops respectively.


For the first form of

#!/bin/bash

# program:

# Test if the IP address is online

# History Donggen 2016-11-08-15:55

Path=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bash

Export PATH

For ((i=233;i<249;i++));d o

Ping-c 1-w 1 10.109.134. $i

P=$?

[$P-eq 0] && echo "\033[32m10.109.134. $i\033[0m is up" | | echo-e "\033[31m 10.109.134. $i \033[0m Down

Done

For the second form of  

#!/bin/bash

# program:

# Test if the IP address is online

# History Donggen 2016-11-08-15:55

Path=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bash

Export PATH

For I in {233..249}; Do

Ping-c 1-w 1 10.109.134. $I &>/dev/null

Result=$?

[$RESULT-eq 0] && echo-e "\033[32m 10.109.134. $I are up\033[0m" | | echo-e "\033[31m 10.109.134. $I is down\033[0m "

Done


until cycle

#!/bin/bash

# program:

# Practice using the Until loop to ping commands

#history Donggen 2016-11-08-17:30

Path=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bash

Export PATH

Declare-i i=232


Until [$i-ge 249]; Do

Let I+=1

Ping-c 1-w 1 10.109.134. $i &>/dev/null

Result=$?

[$RESULT-eq 0] && echo-e "\033[32m10.109.134. $i are up.\033[0m" | | echo-e "\033[31m10.109.134. $i is down.\033[ 0m "

Done

While Loop

#!/bin/bash

#program:

#练习使用while循环

#history Donggen 2016-11-09-08:45

Path=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bash

Export PATH

Declare-i i=232


While [$i-ge 232-a $i-le 249]; Do

Let i++

Ping-c 1-w 1 10.109.134. $i &>/dev/null

Result=$?

[$RESULT-eq 0] && echo-e "\033[32m10.109.134. $i are up.\033[0m" | | echo-e "\033[31m10.109.134. $i is down.\033[ 0m "

Done


Exercise two

2, write a script (premise: Please add a hard disk for the virtual machine, assuming it is/dv/sdb), to create a partition for the specified hard disk;

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, remind the user to confirm that the next operation may damage the data, and ask the user to confirm, if the user chooses Y to continue, otherwise let the user re-select;

3. Erase all partitions on the hard drive (prompt, erase all partitions and perform the Sync command, and let the script sleep for 3 seconds

After the clock is partitioned), and create three primary partitions for it, the first is 20M, the second is 512M, the third is not 128M, and the third is the Swap partition type; (Hint: The partition command is delivered to Fdisk via Echo)


#!/bin/bash

# program:

# Practice Script Partitioning

# History Donggen 2016-11-09-9:10

Path=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bash

Export PATH

Fdisk-l 2>/dev/null | grep "^disk/dev/[sh]d[a-z" | Awk-f: ' {print '} '

Read-p "Input Your choice:" Choice

if [$CHOICE = ' quit ']; Then

echo "Choice is quiting ..."

Exit 6

Fi

Until Fdisk-l 2>/dev/null | Grep-o "Disk/dev/[sh]d[a-z]" | grep "^disk $CHOICE" &>/dev/null; Do

Read-p "Input Your choice again:" Choice

Done

Read-p "Please Continue y|n:" RESULT

until [$RESULT = = "Y"-o $RESULT = = "n"]; Do

Read-p "Please Continue again y|n:" RESULT

Done

if [$RESULT = = "n"]; Then

echo "Quiting ..."

Exit 7

elif [$RESULT = = "Y"]; Then

DD If=/dev/zero of= $CHOICE bs=512 count=1

Sync

Sleep 3

Echo ' n

P

1

+20m

N

P

2

+512m

N

P

3

+128m

T

3

82

W ' | Fdisk $CHOICE

echo "$CHOICE is fdisk OK"

Fi

This article is from the "Learn Linux history" blog, please be sure to keep this source http://woyaoxuelinux.blog.51cto.com/5663865/1871115

Linux command exercise: script to implement HDD partitioning function

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.