|
Linux was built to be the programmer's champion, to make the task of administering a system simple, intuitive, friendly, and automated. With the power of shell scripting, any repetitive, day-to-day, task can be accomplished automatically.
Harkening back a few years now, one of the first intensive shell scripts I wrote involved data retrieval from a tape backup system. Before I wrote the script, whenever a user needed to pull data from our data archives, I would have to perform a search to find the tape on which the data resided, enter a few commands to load that tape, perform a search on the tape for the exact location of that data, and then retrieve the data to an appropriate destination on the active server. It wasn't difficult work, but I was the only one who could perform the task, and it was starting to eat away at my work day. So, I told myself that enough was enough, and that it was time to write a script to perform this task. I made a log of each step that was involved in data retrieval. And then, I opened up my vi editor, and I typed in each step on its own separate line. Then, I saved the file, made it executable, and then ran it. It ran perfectly. While it was running, I could go about my usual business. I went back a few times and made the script more sophisticated: adding error checking code, had the script automatically create a log of the tasks it had performed, and had that log e-mailed to me each morning. This was it; there was no looking back. I was hooked on shell scripting. I decided from that day forward, if there was a task I could complete with a script, I would probably write that script. It's made me lazy, in a way. I don't like to do anything manually now. I set up cron jobs to execute most of my routine scripts for me; when it comes to administrating my system, I don't do much but read logs now. That's how I like it. My former mentor told me that "the best kind of programmer is a lazy programmer." I live by that creed. So, let's show a brief example of the power of scripting. Say you have a web server, and you often create new domains. After you create the domain folder, you like to copy over a simple index.html from a template directory; this index.html just says something like, "website under construction". So, in a GUI (graphical user interface), you often navigate to the parent folder where your domain will reside, right click and select "create new folder", name the folder something appropriate, then navigate to your template directory, copy the index.html file, navigate to your new domain folder, and then paste the index.html inside. That can get aggravating if you're setting up dozens of domains. Here's a quick script (we'll explain it after you read it): #!/bin/bash # This script creates new domain folders and copies # a simple index.html file into them
my_new_domain=$1 if [[ $my_new_domain = "" ]]; then echo "Usage: $0 domain_name" exit fi
# create the domain folder mkdir /var/www/html/$my_new_domain
# copy the template index.html file to the domain folder cp -i /location/of/template/index.html /var/www/html/$my_new_domain
So, what's that first line? "#!/bin/bash"? That's ugly, isn't it? Well, the '#!' is known as a she-bang, and it tells the shell that any file name proceeding it should be used to execute the script. That is, in this case, the bash shell should be used to interpret and execute this script. It's always good, but not entirely necessary, to include the she-bang. If you don't use the she-bang, the system will default to executing the script using the current shell, but since the current shell might not use the same syntax as your script, this could cause program failure. Any other lines starting with a '#' are considered a comment. The shell will ignore these lines; they are useful for documenting what your program is or what's about to happen in your program. The line 'my_new_domain=$1' is storing the first command-line argument into a variable called 'my_new_domain'. When you use arguments on the command-line, each is assigned a number, so that the first argument is referred to as $1, the second as $2, and so on. It's not entirely necessary to set a new variable that contains $1, but we do it here so we know that the first command line argument is supposed to be the name of your new domain. The next line that starts with 'if' is a test. This tests to see if the domain supplied on the command line contains any text. If it does not contain text, that is if it's an empty string (denoted by the expression ""), then we don't want to continue executing the program. We issue an echo statement, which prints text to the string, indicating the usage of the program (namely that we expect a command line argument), and then we exit the program. If the test fails, that is if the command line argument does contain text, then we continue then to run the commands mkdir and cp. We supply the my_new_domain variable for each line, so that the programs knows where to make the directory, and to where the index.html template file should be copied. Now, this script was overly simple. We could have done a lot more error checking. We could have verified that the directory was created, that the copy worked, etc, and if any one thing failed we could have notified the end user. It's up to you as the script writer to determine how much error checking is needed. We will suggest that the longer a script is the more debugging techniques you write into the script; this will make it easier to track exactly where a problem occurred. In our example, if the mkdir failed, we would see a warning sent to the screen, because this is mkdir's default response to an error, and since there is only one mkdir call in the entire script, I would easily be able to find the source of the problem and determine what's happening. Simply put, error checking is entirely up to you, and you should weigh the importance of error checking for each program you write. That's it for now. Remember to make your scripts executable before trying to run them (run a man on the chmod command to learn more; and in general, use man whenever you're stuck on how a command works). We'll keep adding more scripts and scripting techniques. Linux scripting is extremely powerful, whether you use the kornshell, bash shell, or a favorite for newbies, Fish (the Friendly Interactive Shell). Only registered users can write comments. Please login or register. Add as favourites (49) | Quote this article on your site | Views: 753
Powered by AkoComment Tweaked Special Edition v.1.4.6 AkoComment © Copyright 2004 by Arthur Konze - www.mamboportal.com All right reserved |