Example one: Drawing special graphics
Copy Code code as follows:
#!/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 "\n\n\t\t\t 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:
Copy Code code as follows:
[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.
Example two: Colorful scripts
Linux terminals also support colorful, please see the following script:
Copy Code code as follows:
#!/bin/bash
Clear
Echo-e "\033[1m Hello World"
# Bold Effect
Echo-e "\033[5m Blink"
# Blink effect
Echo-e "\033[0m Hello World"
# Back to Noraml
Echo-e "\033[31m Hello World"
# Red Color
Echo-e "\033[32m Hello World"
# Green Color
Echo-e "\033[33m Hello World"
# remaing on screen
Echo-e "\033[34m Hello World"
Echo-e "\033[35m Hello World"
Echo-e "\033[36m Hello World"
ECHO-E-N "\033[0m"
# Back to Noraml
Echo-e "\033[41m Hello World"
Echo-e "\033[42m Hello World"
Echo-e "\033[43m Hello World"
Echo-e "\033[44m Hello World"
Echo-e "\033[45m Hello World"
Echo-e "\033[46m Hello World"
Echo-e "\033[0m Hello World"
Output results:
You can use the extrapolate on the above and put it into your own script.
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:
Copy Code code as follows:
[Root@midstage ~]# Yum Install Pinentry-gui
The command to install "Pinentry-gui" under Ubuntu/debian is:
Copy Code code as follows:
[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.
Copy Code code as follows:
#!/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:
Copy Code code as follows:
[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!
Copy Code code as follows:
#!/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:
Copy Code code as follows:
[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
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.
Copy Code code as follows:
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".