Handy Linux/Unix
Script Tools

handy aliases and
handy scripts

Home > RefInfo menu > Computer topics menu > This Handy Scripts menu page

! Preliminary ! More scripts (and aliases and links) are to be collected !

< Go to Table of Contents, below. >

INTRODUCTION :

Below are samples of shell commands and shell-script code --- arranged in several groups:

Use the table-of-contents below to go directly to code samples in the groups. OR simply scroll down this page to spot samples of interest.


Shell considerations :

Most of these samples 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.

All these scripts start with the line "#!/bin/sh". In many Linux distros (like Ubuntu), /bin/sh is a symbolic-link (aka 'soft link') back to /bin/bash or /bin/dash, where dash is basically a subset of the bash interpreter. If /bin/sh does not exist in your Linux distro, you can simply create the symbolic link by issuing (as root) the command

ln -s /bin/bash /bin/sh

where /bin/bash represents the location of the bash interpreter program.


Warnings :

These scripts 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.

    This warning is probably too dire. Most of the scripts can do no harm because they simply show files, NOT change them. Examine the code, to erase doubt.

The code is presented here mostly for my own personal use and reference (from various computers ; at home or away). These pages provide an organized means of archival and backup. If you use any of the scripts, try them on test data before using in a 'serious' mode. Or use ideas/techniques from the scripts to develop your own code.

Table of Contents:   (links to content on this page or on other pages of this site)

End of Table of Contents. See warnings above. See notes below.

How the code samples (below) are presented :

These alias file code samples are presented in an HTML 'textarea' so that you can scroll the text, vertically or horizontally.

Generally, in ALL my scripts, I use double-# (##) 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 statement should be commented --- when done testing.

I do not try to make one-liner scripts, nor do I try for the ultimate in processing efficiency. (But I am aware of several things to avoid that would make scripts run as slow as molasses.) Rather, I try to make the scripts readable (to me). At the same time, they generally execute quite rapidly --- as rapidly as the target files and the applications called allow, even on a directory containing over a hundred files.


The 'spaces-in-filenames' problem :

In many of the batch code samples (especially in the Handy Nautilus Scripts), you will see that I may be using the Linux/Unix variable

$@

to 'pick up' a list of selected file names. If this proves to be a problem (for example, with embedded spaces in some filenames), I may have to resort to using a Nautilus variable:

  • NAUTILUS_SCRIPT_SELECTED_URIS,   or
  • NAUTILUS_SCRIPT_SELECTED_FILE_PATHS

However, I try to avoid using these variables so that the scripts can possibly be used outside of the Nautilus environment.

The typical loop (in the scripts that deal with multiple filenames as input) is :

      FILENAMES="$@"
      ...
      for FILENAME in $FILENAMES
      do
         ....
      done
      

A nice way that we MAY be able to deal with the embedded-spaces-in-filenames problem is to replace any occurrence of the statement

    FILENAMES="$@"
    

by the little loop

    FILENAMES=""
    while test "$1" != ""
    do
       FILENAMES="$FILENAMES \"$1\""
       shift
    done
    

which uses a '$1-and-shift' trick. [This may need an 'eval' trick --- when $FILENAME is used --- to deal with the double-quotes around the individual filenames.]


This spaces-in-filenames issue is a sign that these code samples are NOT the 'be all and end all'. You (and I) will probably have to edit them somewhat to handle particular issues.

This 'spaces-in-filenames-in-scripts' issue has received a lot of space on web pages over the past ten years or so --- mostly in contexts slightly different from the one I outlined above.

If I ever find a 'definitive' answer, for this particular filenames-loop (an answer that is worth implementing in about 50 handy Nautilus scripts), I will then, and only then, implement 'the solution'.

Until then, I avoid using spaces in filenames --- and, occasionally, I use a utility script to replace spaces in filenames by underscores. (That script uses the '$1-and-shift' trick.)


handy Command ALIASES : (for people who do some work at the 'command line')


< Go to Table of Contents, above. >

Some handy aliases to add to a
'~/.bashrc' or '~/.bash_aliases' or '~/.profile' file :


handy Shell PROMPTS : (for people who do some work at the 'command line')


< Go to Table of Contents, above. >

Some handy Shell-Prompt-setting code ... to add to
a '~/.bashrc' or '~/.bash_aliases' or '~/.profile' file :


LINKS :                   to Script CODE INTROs, TUTORIALS and SOURCES


< Go to Table of Contents, above. >

Brief background on shell scripting :

This page assumes the reader is familiar with shell scripting.

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 links on those 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 web search on the 'strings' :

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:
Since web searchers typically replace special characters in your search strings with a blank character (and seemingly replaces leading blanks by a null character),

  • a search on "bin/sh"
  • a search on "bin sh"
  • a search on "#!/bin/sh", and
  • a search on "   bin sh"
will usually return the same 'hits'.


Forums:

Another handy source of help and code samples for shell scripting is forums, such as


Books:

The most practical and useful book on shell scripting that I have ever found is the O'Reilly book, 'Unix Power Tools'.


The code samples on my script-samples pages may be, in some cases, too large for tutorials. But they have much to offer in the way of examples of working (that is, de-bugged ... to a pretty good extent) shell scripting code for shell-script developers 'of all stripes'.

Bottom of the Shell-Script Code Examples menu page.

These code samples provide shell-scripting beginners (and experienced developers) examples of shell-script code that use techniques, command-line tools, and command parameters that they may not be experienced in using. Hence these are development time-savers.

To return to a previously visited web page location, click on the
Back button of your web browser a sufficient number of times.
OR, use the History-list option of your web browser.
OR ...

< Go to Table of Contents, above. >
< Go to Top of Page, above. >

OR ...

Home > RefInfo menu > Computer topics menu > This Handy Scripts menu page

Page created 2009 Oct 28. Changed 2015 Feb 24.