Script Programming:
Sequential structure
Select structure
If
Case
Loop structure
For
While
Until
While loop: For scenarios where the number of cycles is unknown, exit conditions are needed
Grammar:
While CONDITION; Do
Statement
...
Done
Calculates the and of all positive integers within 100
#!/bin/bash
Declare-i I=1
Declare-i sum=0
While [$I-le 100]; Do
Let sum+= $I
Let i++
Done
Echo $SUM
Exercise: Convert user-entered characters to uppercase, except for quit:
#!/bin/bash
#
Read-p "Input Something:" STRING
while [$STRING! = ' quit ']; Do
echo $STRING | Tr ' A-Z ' A-Z
Read-p "Input Something:" STRING
Done
Exercise: See if a Hadoop user is logged in every 5 seconds, sign in and sign in and exit; otherwise, the current time is displayed and Hadoop is not logged in:
#!/bin/bash
#
W.H.O. | grep "Hadoop" &>/dev/null
Retval=$?
While [$RETVAL-ne 0]; Do
echo "' Date ', Hadoop is not log."
Sleep 5
W.H.O. | grep "Hadoop" &>/dev/null
Retval=$?
Done
echo "Hadoop is logged in."
Write a script:
1) display a menu to the user:
d| D) show disk usages.
m| M) show memory usages.
s| S) show swap usages.
*) quit.
2) When the user is given the option to display the corresponding content;
Extended:
When the user chooses to complete, displays the corresponding information, does not exit, but lets the user select again, displays the corresponding content again, except the user uses the Quit;
#!/bin/bash
#
Cat << EOF
d| D) show disk usages.
m| M) show memory usages.
s| S) show swap usages.
*) quit.
Eof
Read-p "Your Choice:" Choice
while [$CHOICE! = ' quit '];d o
Case $CHOICE in
d| D
echo "Disk usage:"
DF-PH;;
m| M
echo "Memory usage:"
free-m | grep "Mem";;
s| S
echo "Swap usage:"
free-m | grep "Swap";;
*)
echo "Unknown ...";;
Esac
Read-p "Again, Your choice:" Choice
Done
Exercise: Write a script
From the keyboard to allow users to enter a few files, the script will be able to archive several files into a file;
Read
-P "PROMPT": Give a hint
Shell Programming while