Part of Linux shell scripting exercises (ii)

Source: Internet
Author: User

While Condition;do
Statement
Done

Enter loop: Condition satisfies
Exit Loop: Condition not satisfied

Until Condition;do
Statment
...
Done

#!/bin/bash
#
Read-p "Input Something:" STRING

while[$STRING! = ' quit '];d o
echo $STRING | Tr ' A-Z ' A-Z
Read-p "Input Something:" STRING
Done

#!/bin/bash
#
Read-p "Input Something:" STRING

until [$STRING = = ' quit '];d o
echo $STRING | Tr ' A-Z ' A-Z
Read-p "Input Something:" STRING
Done

#!/bin/bash

So you can use the until when you use the while.

#! /bin/bash
#

W.H.O. | grep "Hadoop" &>/dev/null
Retval=$?

Until [$RETVAL-eq 0];d o
echo "Hadoop has not come"
Sleep 5
W.H.O. | grep "Hadoop" &>/dev/null
Retval=$?
Done

echo "Hadoop is LOGGD in"

The execution command State return value can be referenced directly and can be used without quotation marks.
Quotation marks are required if the conditional part is the execution of a command.

For loop

for variable in list;
Loop body
Done

The wording of the near C language
For ((EXPR1;EXPR2;EXPR3)); Do
Loop body
Done

#!/bin/bash
#
Declare-i sum=0
For I in {1..100};d o
Let sum+= $I
Done
Echo $SUM

#!/bin/bash
#
Declare-i sum2=0
for ((j=1; j<=100; J + +));d O
Let sum2+= $J
Done
Echo $SUM

Hard disk, assuming it is/dev/sdb), create a partition for the specified hard disk.
1. List all the hard drives 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 chooses, remind the user to confirm that the next action may damage the data, and ask the user to confirm; if the user chooses Y, continue; otherwise, let the user choose again.
3. Erase all partitions of the hard drive (hint: erase all partitions to perform the Sync command and have the script sleep for 3 seconds before partitioning), and create three primary partitions for it, the first 20M. The second one is 512M, the third is 128M, and the third is the Swap partition type (hint: the partition module can be delivered via Echo)

#!/bin/bash
#
echo "Initial a disk ..."
Echo-e "033[31mwarning:\033[0m"
Fdisk-l 2>/dv/null | Grep-o "^disk/dev/[sb][a-z]"

Read-p "Your choice:" Partdisk

if [$PARTDISK = = ' Quit '];then
echo "Quit"
Exit 7
Fi

Until Fdisk-l 2>/dev/null | Grep-o "^disk/dev/[sb][a-z]" | grep "Disk $PARTDISK" &>/dv/null;do
Read-p "wrong option.your Choice Aging:" Partdisk
Done

Read-p "would destroy all Data,continue:" CHOICE

until [$CHOICE = = ' y '-o $CHOICE = = ' n '];d o
Read-p "would destroy all Data,continue:" CHOICE
Done

if [$CHOICE = = ' n '];then
echo "Quit"
Exit 9
Else
DD If=/dev/zero of= $PARTDISK 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 $PARTDISK &>/dev/null
Sync
Sleep 2
Partprobe $PARTDISK &>/dev/null
Mke2fs-j ${partdisk} &>/dev/null
Mke2fs-j ${partdisk}1 &>/dev/null
Mke2fs-j ${partdisk}2 &>/dev/null
Fi



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.