|
Scripting: Put a clock in your bash terminal |
 vote
 |
|
In the original version, the cursor positioning didn't work on my Mac OS X system. If that happens to you, try this simplified variant: #!/bin/bash while true do tput sc tput cup 0 60 echo -en `date +"%H:%M:%S %F"` tput rc sleep 1 done
Also, note that you'll need to run either script in the background to use your terminal.
The script saves the current cursor position with an ANSI escape sequence instruction. Then, using the tput command, the cursor is sent to row 0 (the top of the screen) and the last column minus 19 characters (19 is the length of HH:MM:SS YYYY-MM-DD). The formatted date command is displayed in green inverted color. The cursor is then sent back to its original position with another ANSI sequence that restores the original saved position. read more... |
|
| | mail this link | permapage | score:9209 | -Ray, January 22, 2008 |
|
Scripting: A parallel Linux backup script |
 vote
 |
|
This example bash shell script demonstrates a simple method of creating backups of multiple filesystems to multiple tape devices simultaneously. While the script presented writes to four tape drives in parallel, it can easily be modified to write to other device types and to create a different number of backup streams. The script is set up for the bash shell under Linux, but modifying it for another variety of Unix should simply be a matter of changing the locations of utility files such as tar, echo, cp, and sleep.
The script can be downloaded from http://librenix.com/scripts/par.tar.sh. Download the file now and load it into an editor as this article will refer to it frequently. Also, you may want to modify bits of it to match your filesystem names and your devices.
The first line of the script looks like this:
#!/bin/bash If the bash shell isn’t in the /bin directory on your system, you’ll need to modify this line. Enter the command which bash now to verify the location of bash. My Fedora Linux system and my Mac OS X system both have bash in /bin, but my FreeBSD system does not. If you have a non-Linux flavor of Unix, you’ll probably need to use the ‘which’ command to verify the locations of each command used in the script. The commands used are:
bash cd sleep echo date tar wait ls wc Note that ‘wait’ and ‘cd’ are usually implemented as internal shell commands and may not have external commands associated with them. If that is true for your system, leave ‘cd’ and ‘wait’ with no directory prefix just as they are in the original script.
Now, the first command in the script resets the current working directory to ‘/’:
cd / Since the script precedes each directory to be backed up with a ‘.’ to represent the current working directory, starting out at ‘/’ is necessary. The reason for this precaution is that some implementations of the tar command will only load files from a tar archive into the exact directory that was specified when the file was backed up. By prefixing the names with a ‘.’ we preserve the ability to recover the files into any subdirectory we want, without overwriting the original files.
Immediately after the ‘cd /’ command is where you would put any commands to shut down all services that must be quieted prior to a backup. The example script has a (commented out) command to initiate an Oracle database shutdown followed by a ‘sleep’ command to allow time for the shutdown to complete. The example database shutdown and the following delay probably don’t apply to your system. Obviously, you’ll have to add commands yourself to stop any applications that might interfere with the backup.
Next, we use the ‘date’ command to create two sets of four tiny files to stick at the start and end of each tape. Note that the presence of a ‘date.#’ file at the beginning of each tape lets you quickly find out when a tape was created and on which drive. The ‘zzzz.#’ files, appended to the end of each tape, only serve to let you easily verify that a backup completed without overrunning the end of the tape.
Next, we start the four actual ‘tar’ backup commands, each with sample directories named ‘./dir1’, ‘./dir2’, etc. Of course, you’ll need to modify the list of directories to match the actual directories you wish to back up. Note that you’ll probably want to balance the directory sizes so that all of the largest directores aren’t on the same tape. Also, note that each ‘tar’ command is run in the background and logs to a tar.#.log file in the /tmp directory. Obviously, you might want to put the logfiles somewhere else.
After each ‘tar’ command there is an entry like this: ‘TASK=$0’, or ‘TASK=$1’. These arbitrarily-named ‘TASK’ variables are used to store the process ID of each ‘tar’ command so that the script can wait for them with the four ‘wait’ commands that follow in the next block of code. There, we have the four ‘wait’ commands waiting on the $TASK0, etc, variables. (The addition of the ‘$’ to each TASK# shell variable is not a typo -- it’s necessary to read back the contents of the variable.)
Next, after the script has waited for the completion of each of the four ‘tar’ commands, it appends some information to a history file for later reference. It stores the date of the backup, the filesize of the logfile, and the number of files backed up on each tape to each of four history files. While the script will overwrite the logfiles (tar.#.log) each time it is run, it will append these three lines to each of the four history files (tar.#.history).
The final steps in the script are commented out. Those are the commands necessary to restart any applications that were brought down for the backup. Again, in the example we assume an Oracle database needs to be restarted. You’ll need to add the commands necessary to start any applications that were stopped at the beginning of the script. |
|
| | mail this link | permapage | score:9168 | -Ray, April 10, 2005 |
|
24 one hour Linux shell scripting lessons |
 vote
 |
|
Yes, you really can learn Unix/Linux shell scripting in 24 hours. And, what works for the traditional Unix Bourne shell also works for the GNU Bourne Again SHell, bash, the virtual standard for Linux.
One of the most powerful standard programs available in UNIX is the shell. The shell is a program that provides you with a consistent and easy-to-use environment for executing programs in UNIX. If you have ever used a UNIX system, you have interacted with the shell.
The main responsibility of the shell is to read the commands you type and then ask the UNIX kernel to perform these commands. In addition to this, the shell provides sophisticated programming constructs that enable you to make decisions, repeatedly execute commands, create functions, and store values in variables.
This book concentrates on the standard UNIX shell called the Bourne shell. read more... |
|
| | mail this link | permapage | score:9053 | -Ray, December 19, 2005 (Updated: March 24, 2007) |
|
Sysadmin Shell Scripting |
 vote
 |
|
This three-part series covers basic, intermediate and advanced shell scripting techniques for system administrators.
A working knowledge of shell scripting is vital if someone wants to become good at system administration tasks. Since this tutorial tackles topics that assume a basic understanding of shell scripting, we strongly urge you to take a look at our Shell Scripting: The Basics article first…
One important aspect of shell scripting is file-oriented utility. A file-oriented utility is basically used as a filter in a pipe.
We can add a ‘-’ to get a more useful result. That is, when we have ‘file -’, the shell waits for the user input and analyses it. read more... |
|
| | mail this link | permapage | score:8914 | -Ray, December 17, 2010 |
|
Pattern matching in shell scripting |
 vote
 |
|
This article is excerpted from the book Beginning Portable Shell Scripting.
Shell programming is heavily dependent on string processing. The term string is used generically to refer to any sequence of characters; typical examples of strings might be a line of input or a single argument to a command. Users enter responses to prompts, file names are generated, and commands produce output. Recurring throughout this is the need to determine whether a given string conforms to a given pattern; this process is called pattern matching. The shell has a fair amount of built-in pattern matching functionality. read more... |
|
| | mail this link | permapage | score:8824 | -Ray, January 1, 2009 |
|
Mac Shell Scripting Tutorial |
 vote
 |
|
A tutorial on scripting for the Mac, from Apple.
This document assumes that you already have some basic understanding of at least one procedural programming language such as C. It does not assumes that you have very much knowledge of commands executed from the terminal, though, and thus should be readable even if you have never run the Terminal application before.
The techniques in this document are not specific to Mac OS X, although this document does note various quirks of certain command-line utilities in various operating systems. In particular, it includes information about some cases where the Mac OS X versions of command-line utilities behave differently than other commonly available versions such as the GNU equivalents commonly used in Linux and some BSD systems. read more... |
|
| | mail this link | permapage | score:8742 | -Ray, October 10, 2006 |
|
Monitor a Linux service with watchdog scripting |
 vote
 |
|
Monitor your web server, or just possibly, that program that mysteriously disappears occasionally...
Old Unix hands already know this, but new Unix (Linux) users may be asking, ‘What is a “watchdog script”?’ Basically it is a bash or other script that is run via cron periodically to check on a persistent service. The watchdog script takes actions based on the state of the service it monitors. read more... |
|
| | permapage | score:8700 | -Ray, May 19, 2010 |
|
Scripting: Shell one-liners |
 vote
 |
|
Several useful example shell script one-liners are explained...
In this context, a one-liner is a set of commands normally joined through a pipe (|). When joined by a pipe, the command on the left passes its output to the command on the right. Simple or complex, you can get useful results from a single line at the bash command prompt. read more... |
|
| | permapage | score:8600 | -Ray, July 24, 2008 |
|
Shell Scripting: A Low-precision Visual Timer |
 vote
 |
|
A low-precision timer script for your Linux / Unix box...
For many years, I've wanted a simple command-line timer program to use in conjunction with short human tasks — the sort of thing my wristwatch timer works for, but I'm not always wearing that watch. I want something that provides some indication of elapsed time or time remaining. Amazingly, the commercial Unixes I'm familiar with do not provide such a utility; nor does one seem to be readily available with the standard X Window tools. read more... |
|
| | mail this link | permapage | score:8548 | -Ray, May 10, 2004 |
|
Scripting ps and grep |
 vote
 |
|
A scripted approach to grepping what you want out of the ps command...
If you've worked in Unix for any length of time, you're probably familiar with psg or some such variant. Psg stands for "ps through grep" (essentially a ps query whose result is piped grep). It works and works well, but sometimes you need more than just a process string; sometimes you want the process string AND the process ID (PID) — separated. Oh yes, and since we're compiling a wish list here, how about the operation simply telling me how many processes match a given target string. I'll show how to do all of that — and do it without having to run multiple ps commands and filtering those results through several piped operations. read more... |
|
| | mail this link | permapage | score:8505 | -Ray, February 9, 2006 |
|
Scripting: Cursor and color control with Tput |
 vote
 |
|
| Many times, shell scripts evolve into menu-based scripts, and the scripter wants to display more to users than simply scrolling text. Regardless of the complexity, scripters have always needed a way to change their output to bold, underline it, reverse the highlights, and so on. That's where tput comes into play. read more... |
|
| | permapage | score:8461 | -BlueVoodoo, April 12, 2008 |
|
bash scripting: Looping through a list |
 vote
 |
|
| This is an example of a Bash shell script used to loop through a list to compare to text strings found in logs. The script is used to detect and block attacks on a web site. read more... |
|
| | permapage | score:8411 | -aweber, December 26, 2011 |
|
Learn awk scripting |
 vote
 |
|
awk is a wonderfully powerful little Unix scripting language.
The awk utility is a small program that executes awk language scripts, which are often one-liners, but just as easily may be larger programs saved in a text file. For example, to execute an awk script saved in the file prg1.awk and have it process the file data1, you could use a command such as: read more... |
|
| | permapage | score:8318 | -Ray, January 24, 2006 |
|
Command substitution in Bash scripting |
 vote
 |
|
Command substitution is a useful way to take the output of one command and use it as an argument in another command.
The first way is the more modern way, and is really the only way that you want to use. You surround the command that you want to substitute with parentheses, and then precede it with a dollar-sign. Here, we’re using echo to show the output of date.
echo $(date) read more... |
|
| | permapage | score:8234 | -aweber, February 5, 2011 |
|
Shell scripting: conditionals |
 vote
 |
|
Using conditional tests in your shell scripts...
Conditions are elements that you will use often. With conditions you will test for an element and then create a response to the results of that test. In most situations, conditions will be a very important part of your scripts. read more... |
|
| | permapage | score:8202 | -Ray, April 5, 2010 |
|
Scripting: Bash Array Tutorial |
 vote
 |
|
An excellent introduction to bash arrays including 15 examples...
$ cat arraymanip.sh #! /bin/bash Unix[0]='Debian' Unix[1]='Red hat' Unix[2]='Ubuntu' Unix[3]='Suse'
echo ${Unix[1]}
$./arraymanip.sh Red hat read more... |
|
| | permapage | score:8104 | -Ray, June 7, 2010 |
|
Tutorial: Graphical Scripting with Kommander |
 vote
 |
|
From the everything-should-have-a-scripting-interface dept.
Kommander is a simplified and modified version of Qt Designer which lets you add scripting abilities to the dialogues it makes. It saves the result as a designer UI file which can be run with Kommander Executer. It is the easiest way to make simple programmes, I like to think of it as graphical shell scripting.
Konstuct is a program to download and install KDE from sources. This tutorial takes us through using Kommander to make a graphical program to configure and run Konstruct. read more... |
|
| | mail this link | permapage | score:8083 | -Ray, January 5, 2005 |
|
Bash Shell Scripting String Functions |
 vote
 |
|
This intermediate bash shell scripting article provides some useful functions, in source code of course.
In C, defines strcat(3), strcpy(3), strlen(3), and strcmp(3) for string concatenation, copy, size, and test operations respectively. Such basic operations are needed constantly when programming in any language, and shell scripting is no exception. read more... |
|
| | permapage | score:7993 | -Ray, November 12, 2004 |
|
Scripting: Convert your gzip files to bzip2 |
 vote
 |
|
A 'rezip' script that converts your .gz files to .bz2 for better compression and maybe better error recovery -- at a substantial cost in compression time, of course. Rezip...
Uses a simple text file of paths and filenames for input -- so you can save the results of "find" to a file, run rezip, and the files will be re-compressed one at a time, with a running log and no user intervention (as long as there's free space on the destination drive.) Example:
$ find /mnt/bkps -name *.gz > ~/rezipp-files.txt && rezip read more... |
|
| | mail this link | permapage | score:7988 | -Ray, February 13, 2006 |
|
Java Scripting Languages: Jython and Groovy |
 vote
 |
|
Not to be confused with JavaScript...
Among the many interesting things that I saw, two stand out: Eclipse and the scripting language called Groovy. I already have written about Eclipse, so here I simply note that Eclipse now seems to dominate the Java IDE world. Groovy, on the other hand, is new. It prompted me to look at Python-like scripting languages that run in a Java environment.
In this article, I discuss Jython and Groovy, two scripting languages that use the Java runtime environment. By the way, JavaScript has nothing to do with the Java language, so it is not considered here. read more... |
|
| | mail this link | permapage | score:7952 | -Ray, July 15, 2005 |
|
|