5 shell Scripts for Linux newbies to learn shell Programming–part II

Source: Internet
Author: User
Tags gpg

To learn something your need to do it, without the fear of being unsuccessful. I believe in practicality and hence'll be accompanying the practical world of Scripting Language.

Learn Basic Shell Scripting

This article is a extension of our first article understand Linux shell and Basic Shell Scripting–part I, where we gave You a taste of Scripting, continuing, and we won ' t disappoint you on this article.

Script 1:drawing a special Pattern
 #!/bin/bashmax_no=0echo-n "Enter number between (5 to 9):" Read Max_noif! [$MAX _no-ge 5-a $MAX _no-le 9]; Then echo "WTF ... I ask to enter number between 5 and 9, Try Again "Exit 1ficlearfor ((i=1; i<=max_no; i++)) does for ((S=MAX_NO; s>=i; s--)) do Echo-n "" is done for ((j=1;  j<=i;      J + +)) do echo-n "." Done echo "" Did ###### Second stage ###################### for ((I=max_no; i>=1; i--)) do for ((s=i; s<=max_no; s++)) does Echo-n "" Done for ((j=1;  j<=i;    J + +)) do echo-n "." Done echo "" doneecho-e "\n\n\t\t\t whenever need help, tecmint.com are always there" 

Most of the above 'key words' would is known to your and most of the them is self explanatory. e.g., MAX sets the maximum value of the variable, for was a loop and anything within the loop gets on executing AG Ain and again till the loop is valid for given value of input.

Sample Output
[[email protected] ~]# chmod 755 special_pattern.sh[[email protected] ~]#./special_pattern.shenter number between (5 to 9 ): 6       ...     . . .    . . . .   . . . . .  . . . . . .  . . . . . .   . . . . .    . . . .     . . .      . .       .                         Whenever need help, tecmint.com are always there

If you were a little aware of any programming language, learning the above script was not difficult, even if you were new to Computation, programming and Linux it is not going to be much difficult.

Download special_pattern.sh

Script 2:creating Colorful Script

Who says, Linux are colorless and boring, save the codes below to anything [dot] sh, make it exe Cutable and Run it, don ' t forget to tell me about it is, Think what can achieve, implementing it somewhere.

#!/bin/bashclear echo-e "33[1m Hello World" # bold effectecho-e "33[5m Blink" # Blink effectecho-e ' 33[0m Hello World ' # b  Ack to NORMALECHO-E ' 33[31m Hello World ' # Red colorecho-e ' 33[32m Hello World ' # Green colorecho-e ' 33[33m Hello World ' # See remaining in Screenecho-e "33[34m Hello World" echo-e "33[35m Hello World" echo-e "33[36m Hello World" echo-e-N "33 [0m] # Back to normalecho-e "33[41m Hello World" echo-e "33[42m Hello World" echo-e "33[43m Hello World" echo-e "33[44m He Llo world ' echo-e ' 33[45m Hello World ' echo-e 33[46m Hello World ' echo-e ' 33[0m Hello World '

Note: Don ' t bother about the "color code now", those important to you'll be at your tongue, gradually.

Warning: Your terminal might not having the facility of blinking.

Sample Output
[Email protected] ~]# chmod 755 colorfull.sh[[email protected] ~]#./colorfull.shhello Worldblinkhello Worldhello WORLDH Ello Worldhello Worldhello Worldhello worldhello worldhello worldhello worldhello worldhello WorldHello WorldHello WorldH Ello World

Download colorfull.sh

Script 3:encrypt a File/directory

This script would encrypt a file (remember? directory/driver/.... Everything is treated as file, inLinux). The current limitation of the above script was that it don ' t support auto Completion of the name using TAB. Moreover, the script and file to is encrypted in the same folder. Need to install "Pinentry-gui", using yum or apt , if required.

[[email protected] ~]# yum install pinentry-gui[[email protected] ~]# apt-get install Pinentry-gui

Crete a file called "encrypt.sh" and place the following script, make it executable and run it as shown.

#!/bin/bashecho "Welcome, I am ready to encrypt a file/folder for You" echo "currently I had a limitation, place me to Thh Same folder, where a file to being encrypted is present "echo" Enter the Exact file Name with extension "read File;gpg-c $fil Eecho "I have encrypted the file successfully ..." echo "now I'll be removing the original file" Rm-rf $file
Sample Output
[[email protected] ~]# chmod 755 encrypt.sh[[email protected] ~]#./encrypt.shwelcome, I am ready to Encrypt a f Ile/folder for youcurrently I has a limitation, place me to the same folder, where a file to beencrypted is Presententer The Exact File Name with extensionpackage.xml┌─────────────────────────                                    ────────────────────────────┐│enter Passphrase                                                   │││                                                   ││                                                   │passphrase *******_________________________________│                                                   ││      │<ok>                       <cancel>│└─────────────────────────── ──────────────────────────┘please Re-enter this passphrase┌──────────── ─────────────────────────────────────────┐│please Re-enter this PASSPH                                                     Rase││                                                   ││passphrase ********________________________________│                                                   ││                                                   │<ok> <cancel>│ └─────────────────────────────────────────────────────┘i has encrypted the file SUCCESSFULLY...N ow I'll be removing thE Original file</pre> 

gpg-c : This would encrypt your file, using a passkey aka password. In this process of learning you would has never thought that the actual process of learning could is that much easy. So after encrypting a file What do you need? obviously! Decrypting the file. and I want you–the learner, the reader to write the decryption script yourself, don ' t worry I am not leaving the Middle, I just want you-gain something out of the this article.

Note: gpg-d filename.gpg > filename is a need to implement in your decryption script . You may post your script in comment if successful and if not do you ask me to write it for you.

Download encrypt.sh

Script 4:checking Server Utilization

Checking The server utilization is one of the important task of a administrator, and a good administrator are one who K Nows How to automate the-day task. Below is the script that would give many such information about your server. Check it yourself.

 #!/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 '/' $9} ' free-m | Xargs | awk ' {print ' free/total memory: ' $17 '/' $8 ' MB '} ' echo '--------------------' start_log= ' head-1/var/log/messa Ges |cut-c 1-12 ' oom= ' grep-ci kill/var/log/messages ' echo-n ' oom errors since $start _log: "$oom echo" "E Cho "--------------------" echo "utilization and most expensive processes:" Top-b |head-3 echotop-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 Ech O "--------------------" echo "Vmstat:" Vmstat 1 5 
Sample Output
[[email protected] ~]# chmod 755 server-health.sh[[email protected] ~]#./server-health.shtue Jul 16 22:01:06 IST 2013uptime:22:01:06 up 174 days, 4:42, 1 user, load average:0.36, 0.25, 0.18Currently connected:22:01:06 up 174 Days, 4:42, 1 user, load average:0.36, 0.25, 0.18USER TTY from [email protected] IDLE J CPU PCPU whattecmint pts/0 116.72.134.162 21:48 0.00s 0.03s 0.03s sshd:tecmint [priv]--------------------La St Logins:tecmint pts/0 Tue Jul 21:48 still logged in 116.72.134.162tecmint pts/0 Tue Jul 16 21: 24-21:43 (00:19) 116.72.134.162--------------------Disk and memory usage:free/total disk:292g/457gfree/total MEM  ory:3510/3838 MB--------------------OOM errors since Jul 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.18tasks:149 Total, 1 running, 148 s leeping, 0 stopped, 0ZOMBIECPU (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 20 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

Note: I have given you the script that gives the output of the terminal itself, how to getting the output in a File for future reference. Implement it using redirect operator.

    1. '>': The redirection operator causes a file creation, and if it does exist, the contents is overwritten.
    2. '>>': When you use >> adding information, rather than replacing it.
    3. '>>' safe, as compared to '>'

Download server-health.sh

Script 5:check Disk Space and sends an Email Alert

How about getting an e-mail when disk use -partition part is bigger than Maximum allowed, it's a life saver SCR IPT for Web administrators with little modification.

Max=95[email protected]part=sda1use= ' df-h |grep $PART | awk ' {print $} ' | cut-d '% '-f1 ' if [$USE-gt $MAX]; Then  echo "Percent used: $USE" | mail-s "Running out of disk space" $EMAILfi

Note: Remove "user" with your user name. You can check mail using using the 'Mail' command.

Download check-disk-space.sh

Script writing and programming is beyond boundaries, anything and everything could be implemented as required. That's all for now, in my very next article I'll be giving your some different flavors of scripting. Till then stay cool and tuned, enjoy.

(from:http://www.tecmint.com/basic-shell-programming-part-ii/)

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.