Five classic examples of Linux shell scripting "novice Learning"

Source: Internet
Author: User
Tags chmod decrypt gpg memory usage

Example one: Drawing Special graphics

#!/bin/bash

Max_no=0

Echo-n "Enter number between (5 to 9):"
Read Max_no

if! [$MAX _no-ge 5-a $MAX _no-le 9]; Then
echo "WTF ... I ask to enter number between 5 and 9, Try Again "
Exit 1
Fi

Clear

for ((I=1; i=i; s--))
Todo
Echo-n ""
Done
for ((j=1; j=1; i--))
Todo
for ((s=i; s<=max_no; s++))
Todo
Echo-n ""
Done
For ((j=1 j<=i; J + +))
Todo
Echo-n "."
Done
echo ""
Done

Echo-e "NNTTT whenever you need help, tecmint.com is always there"

You should not be bothered by the "keywords" in the example above, many of them are familiar to you, or you can guess their meaning from their names, such as "max" setting the maximum value of a variable, "for" is a loop.

Output results:

[Root@tecmint ~]# chmod 755 special_pattern.sh
[Root@tecmint ~]#./special_pattern.sh
Enter number between (5 to 9): 6
.
. .
. . .
. . . .
. . . . .
. . . . . .
. . . . . .
. . . . .
. . . .
. . .
. .
.

Whenever you need help, tecmint.com is always there

If you have a programming base in other languages, it should be easy for you to learn the script above. Even if you are a novice on the computer, the learning process will not be too difficult.

You can download the code for this example from here.

Example Two: colorful Scripts

Linux terminals also support colorful, please see the following script:

#!/bin/bash

Clear
Echo-e "& #092;& #048; 33[1m Hello World"
# Bold Effect
Echo-e "& #092;& #048; 33[5m Blink"
# Blink effect
Echo-e "& #092;& #048; 33[0m Hello World"
# Back to Noraml

Echo-e "& #092;& #048; 33[31m Hello World"
# Red Color
Echo-e "& #092;& #048; 33[32m Hello World"
# Green Color
Echo-e "& #092;& #048; 33[33m Hello World"
# remaing on screen
Echo-e "& #092;& #048; 33[34m Hello World"
Echo-e "& #092;& #048; 33[35m Hello World"
Echo-e "& #092;& #048; 33[36m Hello World"

ECHO-E-N "& #092;& #048; 33[0m"
# Back to Noraml
Echo-e "& #092;& #048; 33[41m Hello World"
Echo-e "& #092;& #048; 33[42m Hello World"
Echo-e "& #092;& #048; 33[43m Hello World"
Echo-e "& #092;& #048; 33[44m Hello World"
Echo-e "& #092;& #048; 33[45m Hello World"
Echo-e "& #092;& #048; 33[46m Hello World"

Echo-e "& #092;& #048; 33[0m Hello World"

Output results:



You can use the extrapolate on the above and put it into your own script.

You can download the code for this example from here.

example Three: Encrypted File/directory

The following example shows how to encrypt a file or folder. The current version of the script has some limitations, such as you have to put it and the file/directory you want to encrypt under the same folder. In addition, you may need to install "Pinentry-gui". The command to install "Pinentry-gui" under Fedora is:

[Root@midstage ~]# Yum Install Pinentry-gui

The command to install "Pinentry-gui" under Ubuntu/debian is:

[Root@midstage ~]# apt-get Install Pinentry-gui

Create a script "encrypt.sh" and copy the following code into it. You can also download this script from here.

#!/bin/bash
echo "Welcome, I am ready to encrypt a file/folder for you"
echo "Currently I have a limitation, place me to the same folder,
Where a file to being encrypted is present "
echo "Enter the Exact File Name with extension"
Read file;
Gpg-c $file
echo "I have encrypted the file sucessfully ..."
echo "Now I'll be removing the original file"
RM-RF $file

Output results:

[Root@tecmint ~]# chmod 755 encrypt.sh
[Root@tecmint ~]#./encrypt.sh

Welcome, I am ready to encrypt a file/folder for you
Currently I have a limitation, place me to the same folder,
Where a file to being encrypted is present
Enter the Exact File Name with extension

Package.xml

Enter Passphrase

Passphrase _________________________________


Please re-enter this passphrase

Passphrase _________________________________


I have encrypted the file successfully ...
Now I'll be removing the original file

Code Description:

GPG-C: This command uses AKA to encrypt the file. When you need it, you need to decrypt the encrypted file. Here we do not give the specific code, you can try to write it. Tip: Use the command gpg-d filename.gpg > filename to decrypt a file.

Example four: View server Utilization

Viewing server utilization is an important daily job for an administrator. A smart administrator knows how to automate this task. The following script will grab a lot of information on the server and try it quickly!

#!/bin/bash
Date
echo "Uptime:"
Uptime
echo "Currently connected:"
W
echo "--------------------"
echo "Last Logins:"
Last-a |head-3
echo "--------------------"
echo "Disk and Memory Usage:"
df-h | Xargs | awk ' {print ' free/total disk: ' $11 '/' $} '
free-m | Xargs | awk ' {print ' Free/total memory: "$17"/"$" MB "}"
echo "--------------------"
Start_log= ' head-1/var/log/messages |cut-c 1-12 '
oom= ' Grep-ci kill/var/log/messages '
Echo-n "OOM errors since $start _log:" $oom
echo ""
echo "--------------------"
echo "utilization and most expensive processes:"
Top-b |head-3
Echo
Top-b |head-10 |tail-4
echo "--------------------"
echo "Open TCP Ports:"
nmap-p--t4 127.0.0.1
echo "--------------------"
echo "Current Connections:"
Ss-s
echo "--------------------"
echo "Processes:"
PS AUXF--width=200
echo "--------------------"
echo "Vmstat:"
Vmstat 1 5

Output results:

[Root@tecmint ~]# chmod 755 server-health.sh
[Root@tecmint ~]#./server-health.sh

Tue June 22:01:06 IST 2013
Uptime
22:01:06 up 174 days, 4:42, 1 user, load average:0.36, 0.25, 0.18
Currently connected:
22:01:06 up 174 days, 4:42, 1 user, load average:0.36, 0.25, 0.18
USER TTY from login@ IDLE jcpu pcpu WHAT
Tecmint pts/0 116.72.134.162 21:48 0.00s 0.03s 0.03s sshd:tecmint [Priv]
--------------------
Last logins:
Tecmint pts/0 Tue 21:48 still logged in 116.72.134.162
Tecmint pts/0 Tue June 16 21:24-21:43 (00:19) 116.72.134.162
--------------------
Disk and Memory usage:
Free/total disk:292g/457g
Free/total memory:3510/3838 MB
--------------------
OOM errors since June 14 03:37:0
--------------------
Utilization and most expensive processes:
top-22:01:07 up 174 days, 4:42, 1 user, load average:0.36, 0.25, 0.18
tasks:149 Total, 1 running, 148 sleeping, 0 stopped, 0 zombie
Cpu (s): 0.1%us, 0.0%sy, 0.0%ni, 99.3%id, 0.6%wa, 0.0%hi, 0.0%si, 0.0%st

PID USER PR NI virt RES SHR S%cpu%mem time+ COMMAND
1 root 0 3788 1128 932 S 0.0 0.0 0:32.94 Init
2 Root 0 0 0 0 S 0.0 0.0 0:00.00 Kthreadd
3 root RT 0 0 0 0 S 0.0 0.0 0:14.07 migration/0

You can download the code for this example from here.

Example five: View the hard drive usage and send a prompt message

The following example shows how to send a prompt message through a script when the disk's use space exceeds the expected value.

Max=95
email=server@127.0.0.1
Part=sda1

Use= ' Df-h |grep $PART | awk ' {print $} ' | cut-d '% '-f1 '
If [$USE-gt $MAX]; Then
echo "Percent used: $USE" | Mail-s "Running out of disk spaces" $EMAIL
Fi

Description: Replace "user" in the above script with your username. You can view your messages by using the command "mail".

With these five examples, we can learn a lot about the basics of shell programming.

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.