* Home * Articles * Tutorials * Profile * Login / Sign Up [linux-org-logo.png] Search * (*) Articles * ( ) Tutorials * ( ) Entire Site ____________________ Go [peek.png] * Home * Articles * Tutorials * Profile * Login / Sign Up [comment-arrow.png] # __________________________________________________________ ____________________________________________________________ ____________________________________________________________ ____________________________________________________________ [BUTTON Input] (not implemented)______________ [BUTTON Input] (not implemented)______ Comment goes here You should log in and post some comments! The link is up there in the toolbar. Go nuts! Programming in Linux Linux and CVS Checking for damage with rootkit hunter Tools for Programming At the risk of starting a flame war with Vi lovers, I'm an unconditional Emacs fan. For me, Emacs is the ideal editor for doing any kind of programming work. There are several add-ons for Emacs that will make your life much easier. One of them is Espen Skoglund's auto-header.el. This will create a header for your code files which Emacs will automatically populate with necessary information. It will also update the header each time you make changes. It's something that comes in really handy for me. I have a subdirectory in my home directory for emacs odds and ends. I just placed auto-header.el in this directory and then modified my .emacs file like so: (add-to-list 'load-path "~/elisp/") (require 'auto-header) There is a section of the auto-header.el file that defines the characters that are used for commented areas for different programming languages. The headers for the file need to be inside a commented region at the beginning of your source code file. The file includes 20 different programming languages, but PHP, which I use a lot, is missing. So I just added an entry for it between Perl and Postscript: (perl-mode . ("#" "" "##" "#")) (php-mode . ("/*" "*/" " *" "*")) (postscript-mode . ("%" "" "%%" "%")) Now, my files begin with a nice, auto-updating header that looks like this: /********************************************************************* * Filename: listing.php * Description: Script to list stuff * Author: Michael Jordan * Created at: Tue Nov 29 20:13:14 2005 * Modified by: Michael Jordan * Modified at: Tue Nov 29 22:58:56 2005 ********************************************************************/ Actually, you can add more info to this header. Just consult the comments in the auto-header.el file itself for more information on that. Programming Languages and Linux The is a virtual smorgasbord of programming language compilers available for Linux. Just the famous gcc compiler itself will create binaries for code written in, C++, Fortran and ADA as well as interpreting Java code. There are freely available compilers for other popular programming languages like Pascal, COBOL and Lisp. As you can see, there are really too many to go into all of them in any depth. Besides, this writer can't program in the vast majority of them anyway. But in this lesson, we will be looking at some of the most common scripting languages that run under Linux. Programming with BASH Thought it might sound like it, BASH isn't one of those captions that pop up (along with Ooff! and Biff!) when Batman and Robin are fighting the bad guys on the old 60's TV show. BASH actually stands for Bourne Again Shell. The reason for the name goes back to Steve Bourne who wrote the original Bourne Shell for Unix. When the GNU created a Free Software equivalent, they named it after Steve's shell and added a little pun on his last name. If you're a system administrator, making BASH scripts is going to be one of those mandatory things. But far from being a chore, you'll learn that it's going to make your work and your life a whole lot easier. Our First BASH Script The first thing that a BASH script needs is the proverbial 'shebang'. These are two characters: #! Following this, you should include the path to the BASH interpreter. So the first line of your script should look like this: #!/bin/bash If your default shell is BASH, the line: #!/bin/sh does the same thing. It's just a symbolic link to /bin/bash. But if your default shell isn't BASH, you won't be invoking /bin/bash if your write a shell script with that first line. Since on Linux systems, BASH is normally the default shell, you'll see most BASH scripts start with #!/bin/sh From there on, you're free to do what the shell allows. Shell scripts created for administration purposes (which are the majority of scripts) are made up of lines that invoke other commands. Let's look at a simple example. Let's say you have email users on your system but you don't have a quota in place. Still, you want to monitor the sizes of the mailboxes to make sure that people aren't taking up more space than they should. This script, run from crontab, would do the trick nicely: Except for the shebang, comment lines start with # #!/bin/sh # show us the size of email spools email spools # date in YYYY-MM-DD format today=`date +%Y-%m-%0e`; # subject and recipient variables subject="Mailcheck"; sendto="admin@linux.ork"; cd /var/spool/mail ls -lSh | awk '{print $5, $9}' | grep "(G|M)" | mail -s $subject-$today $sendto # end script First off, you'll see that we've declared some variables. These variables are not prefixed by any characters when declared but they are prefixed by the dollar sign ($) when used. You've also noticed that variables can be other commands, as in this example with the date command. When you use a command as a variable, it must be put inside backticks (` `). First, the script changes to the directory where the mail spools are located. The the scripts performs an 'ls' with options and presents a list where the biggest spools are displayed first with their size in human readable format. This is piped to awk, which sorts out the size and the user name. The awk output is grepped for those spools which are in the Megabytes and Gigabytes. This is then piped to the 'mail' command and sent to the admin account with the subject plus the date we declared in those variables. The admin will then have a nice sorted list of who is using the most space in /var/spool/mail. Built in Variables Though we created our own variables in the previous example, BASH also comes with what are known as built invariables. Here is an example of a script with frequently used built in variables. #!/bin/sh echo "You are user $UID on $HOSTNAME" echo "Your home directory is: $HOME" echo "$HOSTNAME is running $OSTYPE" The output of this script should yield something similar to this: You are user 500 on penguin.linux.ork Your home directory is: /home/mike penguin.linux.ork is running linux-gnu As you can see, we didn't have to previously declare any of these. That's why they're known as built-in variables. Their use will save you a lot of time in writing your scripts. You can find a complete list of built-in variables in theGNU BASH Reference Manual Interactive Scripts Though we mentioned that the main use of BASH scripts is for automating administrative tasks, there may be times when you need users to interact with scripts. If you want a user to input information, you need to use the variableread. Let's take a look at the following example: #!/bin/sh echo -n "Enter the name of a city: " read CITY echo -n "$CITY is " case $CITY in London | Paris | Berlin | Rome) echo -n "in Europe";; 'New York' | Chicago | Washington) echo -n "in The USA";; Tokyo | Bejing | Bangalore) echo -n "in Asia";; *) echo -n "some place - but I don't know where";; esac As you can see, we've declared a variable that's going to depend on what the user types in when he/she is prompted for the name of a city. After, we have several options for each case. If the user types in the name of a city we've contemplated here, he/she will be given a message as to where the city is. If not, the script will display a message that it doesn't know where the city is. Any answer is represented by the asterisk (*) Making Sure You Have What You Need If you have to manipulate the contents of a file, it's a good idea to check if this file exists first. Here is a simple BASH routine to do this using the if command: #!/bin/sh if test -f /var/log/mail.log; then printf "The file existsn"; fi This is a good idea, as it would render your script useless if you had it set to manipulate a file that didn't exist. If Loops: A Practical Example I was a full-time English as a foreign language teacher for 12 years, so I can't resist giving you this example of a multiple-choice test using a BASH script. #!/bin/sh PS3="Choose the number of the correct word to fill in the blank: " echo "The emergency brake let go and car rolled ______ the hill" select SENT1 in up down along beside do if [ "$SENT1" == "" ]; then echo -e "You need to enter somethingn" continue elif [ "$SENT1" != down ]; then echo -e "Sorry. Incorrectn" echo "1. Incorrect" >> eoiexam.dat elif [ "$SENT1" == down ]; then echo -e "Great!n" echo "No. 1 - Correct" >> eoiexam.dat break fi done The script makes use of the 'elif' routine to sort out answers that aren't correct. You will also notice that it writes the results, whether correct or not, to a file. If you're in the teaching profession, you could expand on this to give your students a quick quiz. Linux and CVS Checking for damage with rootkit hunter Article Details Author: Linux.org Admin Publish Date: 05/03/2012 Last Updated: 05/04/2012 Categories: Basics, Linux Other Advanced Level Course * System Administration 1. tcpdump 2. Providing Services Under Linux 3. System Administration * Programming 1. Programming with bash 2. Linux and Subversion 3. Linux and CVS 4. Programming in Linux * Security 1. Checking for damage with rootkit hunter 2. OpenSSH 3. Setting up snort 4. Firewalls 5. Basic Security 6. Keeping Your Linux System Secure An Insecure World * Home * Articles * Tutorials * Linux News * Linux Jobs * Linux News * Linux Forum * Webmaster Forum * Contact * Sitemap * Advertising * Corporate Sponsors * Linux.org Facebook Page * Linux.org Twitter Page * Linux.org Google Plus Page * Linux.org RSS Feed Copyright © 2013 Linux.org | All Rights Reserved Content by IQnection.com | Hosting by LinuxPowerHosting.com