2/1/10

Advanced Bash Scripting link - Notes

Changes all filenames in working directory to lowercase.

   1 #! /bin/bash
   2 #
   3 # Changes every filename in working directory to all lowercase.
   4 #
   5 # Inspired by a script of John Dubois,
   6 # which was translated into into Bash by Chet Ramey,
   7 # and considerably simplified by Mendel Cooper, author of this document.
   8
   9
  10 for filename in *                # Traverse all files in directory.
  11 do
  12    fname=`basename $filename`
  13    n=`echo $fname | tr A-Z a-z`  # Change name to lowercase.
  14    if [ "$fname" != "$n" ]       # Rename only files not already lowercase.
  15    then
  16      mv $fname $n
  17    fi
  18 done 
  19
  20 exit 0
  21
  22
  23 # Code below this line will not execute because of "exit".
  24 #--------------------------------------------------------#
  25 # To run it, delete script above line.
  26
  27 # The above script will not work on filenames containing blanks or newlines.
  28
  29 # Stephane Chazelas therefore suggests the following alternative:
  30
  31
  32 for filename in *    # Not necessary to use basename,
  33                      # since "*" won't return any file containing "/".
  34 do n=`echo "$filename/" | tr '[:upper:]' '[:lower:]'`
  35 #                             POSIX char set notation.
  36 #                    Slash added so that trailing newlines are not
  37 #                    removed by command substitution.
  38    # Variable substitution:
  39    n=${n%/}          # Removes trailing slash, added above, from filename.
  40    [[ $filename == $n ]] || mv "$filename" "$n"
  41                      # Checks if filename already lowercase.
  42 done
  43
  44 exit 0

////////////////////////////////////////////////////

du: DOS to UNIX text file conversion.

   1 #!/bin/bash
   2 # du.sh: DOS to UNIX text file converter.
   3
   4 E_WRONGARGS=65
   5
   6 if [ -z "$1" ]
   7 then
   8   echo "Usage: `basename $0` filename-to-convert"
   9   exit $E_WRONGARGS
  10 fi
  11
  12 NEWFILENAME=$1.unx
  13
  14 CR='\015'  # Carriage return.
  15 # Lines in a DOS text file end in a CR-LF.
  16
  17 tr -d $CR < $1 > $NEWFILENAME
  18 # Delete CR and write to new file.
  19
  20 echo "Original DOS text file is \"$1\"."
  21 echo "Converted UNIX text file is \"$NEWFILENAME\"."
  22
  23 exit 0

http://www.faqs.org/docs/abs/HTML/index.html

P.S. The unalias command removes a previously set alias.

No comments:

Quick HTTP to HTTPS - Apache2

There are several methods for redirecting your Apache-based website visitors who might type your servers URL using the plain (non-secure) HT...