Shell Programming while

Source: Internet
Author: User
Tags disk usage

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

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.