Shell Script Snippets
which demonstrate uses and options of commands like
|
! Preliminary ! More snippets (and links) are to be collected !
< Go to Table of Contents, below. >
INTRODUCTION : Below are small samples of the use of Linux/Unix/Gnu shell commands --- grouped in alphabetical order by command name, and including commands such as:
Some of the examples ('snippets' of code) are composed of a combination of shell commands. For example, there are many examples which involve 'pipes' of the output of one command into the input 'file handle' of another command. So I will often make a 'judgment call' in assigning a particular example (snippet of code) to a 'command-group'. I will generally avoid putting the same snippet in multiple groups. But I may put a somewhat different snippet in other groups. For example, if I put an snippet involving 'awk' and 'sort' in the 'awk' group, I will probably put a different (but probably somewhat similar) snippet involving 'awk' and 'sort' in the 'sort' group of snippets.
Considerations in ordering the snippets : Many of these examples are similar to examples in the most practical and useful book on shell scripting that I have ever found --- the O'Reilly book, 'Unix Power Tools'. In that book, the examples are arranged in chapters by subject matter, such as 'organizing your home directory', 'setting your shell prompt', or 'finding files' --- about 50 different subject areas (chapters). Even with that many chapters, it turns out that the index in the back of the book is often the best way to find what one is looking for. The index lists both commands and subject areas. If the index failed me, I would resort to the Table of Contents at the front of the book. Simply browsing through the book was seldom practical, except when desperate, because the book is over 1,100 pages long. I am often looking for examples of how a particular command is used --- that is, examples of specifying its 'most popular' or 'most useful' options and parameters. In other words, I often know the command(s) that are suitable to do the job at hand. So I have grouped the 'snippets' by a major command used in the snippet. No matter how one assembles the 'snippets', there is need for a flexible, multi-faceted search system. In this regard, better than an index is the ability to search a web page for any text string.
Finding 'snippets' on this page : Use the table-of-contents below to go directly to code 'snippets' in the command-name groups. OR simply scroll down this page to spot samples of interest.
Alternatively, use the 'Find text' option of your web browser to look for keywords on this page. For example, when looking for examples ('snippets') having to do with disk space and you do not know which commands might be useful for that topic, use the keyword 'space' or 'disk'. Another example: When looking for 'snippets' having to do with extracting the first character of a word into an environment variable, use a keyword like 'char' or 'first'. If you know the command for which you want examples of its use, then simply search for that command. For example, if you are looking for examples of use of the 'grep' or 'sed' or 'awk' or 'cut' command, one could search for 'grep' or 'sed' or 'awk' or 'cut'. If you are looking for examples of 'for' or 'while' loops, then search for ' for ' or 'while'. If you are looking for examples of if-then-else-fi code, then search for ' fi '. I will generally try to remember to put a leading space in front of command words like 'for' or 'fi' or 'if', so that you can search for ' for ' or ' fi ' or ' if' (space in front, and perhaps behind), thus avoiding hitting a lot of words involving that sequence of non-space characters when you are doing a text search. I may not have established groups for some commands, like 'head' and 'tail'. But such commands are used in many of the snippets. So you can simply search for any command, to see if it is used in any of the snippets. Thus you may be provided with examples of its use.
Shell considerations : Most of these 'snippets' are intended to run with the Bourne or the Korn shells (command interpreters), as well as with the Bash command interpreter. Most of these samples employ commands (and their parameters) available to all three interpreters, so the scripts (and aliases) will generally run in any of these three interpreters. In fact, the Korn and Bash interpreters are offshoots of the Bourne command interpreter. Most of these 'snippets' will work on Unix systems as well as Linux systems. But the Gnu versions of many of these commands have been extended to have many more options than the same command on a Unix system such as Sun Solaris, IBM AIX, HP UX, or SGI Irix. For example, the Gnu commands have many options that start with double-hyphen (--), whereas most Unix commands have options indicated by a single hyphen only. I usually use the single-hyphen option when both a single-hyphen and double-hyphen option are available for the same function. This makes it more likely that the command will work on Unix as well as Linux. For the same reason, I usually use command options that appear in the POSIX standard. This helps assure that the 'snippet' will run in any of the major Unix distributions as well as running, in most cases, on Gnu/Linux-based distros.
Warnings : These 'snippets' may not be tested thoroughly. For example, those using filenames may not have been tested against filenames with embedded blanks. Use at your own risk. The 'snippets' are presented here mostly for my own personal use and reference (from various computers ; at home or away). This assembly of snippets provides a means for me to archive and backup the snippets. If you use any of the snippets, try them on test data before using in a 'serious' mode. Or use ideas/techniques from the 'snippets' to develop your own code and scripts. There are examples of complete (useful, practical) scripts available through my Handy Scripts page --- a page which also provides access to this 'snippets' page. |
Table of Contents: (of this page) < Return to Top of Page, above. > Shell 'snippets' involving :
End of Table of Contents. See warnings above. See the notes immediately below. |
How the code samples (below) are presented : These code 'snippets' are generally presented in a monospace (fixed-width) font. If the snippets are rather long they may be put in an HTML 'textarea' so that you can scroll the text, vertically or horizontally. Generally, in the snippets, I will not put comment statements if the snippets are rather simple in nature. However, if it seems advisable to add useful comments, I will put them at the top of, or within, the snippet, so that the comments can easily be pasted into a script along with the code. I use double-# (##) (double-asterisk) to indicate a true comment. That is, it is not an executable statement and should never be de-commented. I use single-# (#) to indicate an executable statement that has been commented. Typically, this is because I might want to decomment the statement when doing testing of the script in a terminal window. Generally, the statement should be re-commented --- or a neighboring alternative statement should be commented --- when finished testing. I do not spend time trying to make one-liner snippets, nor do I try for the ultimate in processing efficiency. (But I am aware of several things to avoid that would make script segments run as slow as molasses. In particular, avoid massive loops, especially massive loops that involve lots of number crunching or file manipulation.) I DO try to make the snippets readable (to me). At the same time, they generally execute quite rapidly --- as rapidly as the target files and the utilities called allow, even on a directory containing over a hundred files or when sweeping through a file many Megabytes in size. In some 'snippets', you will see that I may be using the Linux/Unix script variable $@or the Linux/Unix script variable $* to 'pick up' a list of parameters passed into the script. If this proves to be a problem (for example, with embedded spaces in some filenames, when there are filenames being passed into a script), one may have to resort to other techniques to handle filenames with spaces embedded in the names. This is a sign that these code snippets are NOT the 'be all and end all'. You (and I) will probably have to edit them somewhat to handle particular issues.
A programming technique to be aware of : There are times when one is having a heck of a time getting a line of code to execute without syntax errors. Often the problem involves a judicious use of double-quotes and/or single-quotes and/or the 'eval' command. The 'eval' command can be applied to a string of code to cause all variable substitutions to take place before the line of code is finally allowed to be executed. An example of 'eval' usage is when you need to make a set of variable names with a sequence of integers in the variable names (in a 'for' loop) --- and you are assigning values to the variables. Example code :
will not work, whereas
will work. There are many other cases in which a judicious use of double-quotes or single-quotes, along with the 'eval' command, will help you make a line of code that is free of syntax errors.
One thing I keep forgetting : ' 2>&1 ' is the proper way to send 'standard error' output along with 'standard out' to a listing file --- NOT some other combination of 1, 2, greater-than, and ampersand. Another handy file 2 technique : ' 2> /dev/null' can be added to a command line to shunt error messages to the 'bit bucket'. This is helpful, for example, when a command (like 'find' or 'ls') is traversing directories owned by root, to which you do not have permission. ' 2> /dev/null' helps avoid a lot of messy 'cannot access' messages in a listing file. Examples: find /data/test2 -name "*.mfh" -print > ${OUTLIST} 2>&1 ls -d $DIRNAME 2> /dev/null |
'awk' snippets :
< Return to Table of Contents, above. >
|
Brief background on shell scripting : This page assumes the reader is familiar with many of the basics of shell scripting and with using the CLI (Command Line Interface). However, for a basic introduction to Linux/Unix shell scripting, try the Wikipedia page on Shell_scripting. In particular, see the Wikipedia page on the Bourne_shell. You may find it helpful to follow some of the links on those Wikipedia pages.
Shell scripting tutorials : There are many web sites that provide tutorials for shell scripting. Someday I may provide a separate web page of such sites. Unfortunately, many such sites go dead quite rapidly. In the meantime, here are a few links to some tutorials :
A problem with tutorials is that they often contain many code 'snippets' but not many complete code samples that accomplish very much. To accomplish much, they would have to be too long for a tutorial.
Sources of Shell Script code samples :
Web Archives: Many instructive shell script code samples are available through code archive web sites such as
I found the Shelldorado archive by doing a I used the string 'shell script' because there are many other scripting languages, such as Perl, Python, and Tcl/Tk. See my Tcl-Tk pages available via my Computer Topics (Ref Info) page. If I had used 'shell' and 'script' as separate words, I probably would have 'hit' a lot more Perl, Python, and other script web pages.
Web Script-Code Searches: Since many shell scripts start off with a call to the '#!/bin/sh' or '#!/bin/ksh' or '#!/bin/bash' interpreter, one way to find code samples is to do web searches on keyword strings such as
NOTE:
Forums: Another handy source of help and code samples for shell scripting is forums, such as |
Bottom of the my SNIPPETS of Shell Code page.
To return to a previously visited web page location, click on the
< Return to Table of Contents, above. >
|