Script Programming Control Structure
Order
Select
If
Case
Loops
For
While
Until
While format:
While Condition;do
Statment
Done
Enter loop: Condition satisfies
Exit Loop: Condition not satisfied
Until format:
Until Condition;do
Statment
Done
Enter loop: Condition not satisfied
Exit Loop: Condition satisfies
#!/bin/bash#read-p "Input Something:" stringuntil [$STRING = = ' quit ']; Do echo $STRING | Tr ' A-Z ' A-Z ' read-p "Input Something:" Stringdone
#!/bin/bash#until who | grep "root" &>/dev/null;d o echo "root not login." Sleep 5done echo "root is login."
For structure:
1.
For variable in;d o
Loop body
Done
2.
For ((EXPR1;EXPR2;EXPR3));d o
Loop body
Done
#!/bin/bash#declare sum=0for I in {1..100};d o let sum+= $Idoneecho $SUMdeclare sum2=0for ((j=1; j<=100; J + +));d o let sum2+= $Jdoneecho $SUM 2
Calculates the arithmetic between 1 and 100 using the two formats for the for statement
#!/bin/bash#for2.shdeclare sum=0for ((i=2;i<=100;i+=2));d o let sum+= $Idoneecho $SUM
Calculates an even number within 100 and
awk ' pattern{action} ' file
Print $
$NF: Last Field
-F: Specifies that the delimiter is:
Fdisk-l 2>/dev/null | grep "^disk/dev/[sh]d[a-z" | Awk-f: ' {print '} '
Find all the hard drives on the server
Write a script (provided that you add a hard disk to the virtual machine, assuming it is/dev/sdb) and 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, n exits; 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 3 seconds before partitioning), and create three primary partitions for it, the first one is 20M, the second is 512M, the third is 128M, and the third is the swap partition type; Hint: The partition command is delivered to Fdisk via Echo)
#!/bin/bash#echo -e "\033[31minitial a disk ...\033[0m" echo -e "\033[ 31mwarning....\033[0m "echo " You have the following disk "fdisk -l | grep "^disk /dev/[sh]d[a-z" | awk -F: ' {print $1} ' | awk ' {print $2} ' read -p "Enter your choice:" choiceif [ $ choice == ' quit ' ];then echo ' quting ... ' exit 0fiuntil fdisk -l | grep "^disk /dev/[sh]d[a-z" | grep "^Disk $CHOICE " &> /dev/null;do read -p " wrong Option,enter your choice aging: " CHOICEdoneread -p " Will destory all format data,continue? y/n: " CHOICE2until [ $CHOICE 2 == ' Y ' -o $CHOICE 2 == ' N ' ];d o read -p "will destory all format data,continue? y/n : " CHOICE2doneif [ $CHOICE 2 == ' N ' ];then echo " quiting .... " exit 7elsedd if=/dev/zero of= $CHOICE bs=512 count=1 & > /dev/nullsyncsleep 3echo ' np1+20mnp2+150mnp3+128mt382w ' | fdisk $CHOICE &> /dev/nullsleep 1syncmke2fs -j ${choice}1 &> /dev/ Nullsleep 2mke2fs -j ${choice}2 &> /dev/nullsleep 2mkswap ${choice}3 &> /dev/nullfi
Until loop statements