The best scripts ever (trick or treat)

Fafa Hafiz Krantz fteg at london.com
Mon Oct 31 13:21:39 PST 2005


Hello.

POST YOUR COOLEST SCRIPTS! <3 (trick or treat)

I thought I'd create this thread for all you script enthusiasts out there.

I've newly started basic shell programming, and just the very thought of it
gives me this warm fuzzy feeling of having scripts assisting me in shaping
things exactly the way I want them. I know you all gurus out there might be
shaking your heads in despair to this, but I'm just trying to increase my
passion and that of others further, if such a thing is at all possible.

For whatever purpose you made your scripts, as long as you're proud of them
and they're not classified as top secret, please be a sport on this
glorious night of halloween and share them with us!

Anyway, here are my humble contributions:

crlf.sh:

#!/bin/sh
#
#   Removes CRLF line termination in ASCII files.
#   $URBAN: crlf.sh,v 1.0 2005/10/24 15:09:05 fafa Exp $
#

for file in `find . -type f ! -name ".*"`; do

	if [ "`file -b "$file" | grep "text.*CRLF"`" != "" ]; then
		perl -i -pe 's,\r\n,\n,g' "$file"
		echo "$file: Done"
	fi

done

mode.sh:

#!/bin/sh
#
#   Sets default ownership and permissions.
#   $URBAN: mode.sh,v 1.0 2005/10/24 15:09:05 fafa Exp $
#

chown -R johann:wheel *

find . -type d -exec chmod 755 '{}' \;
find . -type f -exec chmod 644 '{}' \;

mp3.sh:

#!/bin/sh
#
#   Generate SFV and M3U for MP3 releases.
#   $URBAN: mp3.sh,v 1.0 2005/10/24 15:05:09 fafa Exp $
#

for file in `find /mnt/out/mp3 -name \*.nfo`; do

	directory="`dirname ${file}`"
	prefix="`basename ${file} | sed 's/.nfo//g'`"
	current="`basename ${directory}`"
	sfv="${directory}/${prefix}.sfv"
	m3u="${directory}/${prefix}.m3u"

		cd ${directory}

		rm -f *.sfv > /dev/null 2>&1
		rm -f *.m3u > /dev/null 2>&1

		touch ${sfv}
		cfv -Cq *.mp3
		cat ${current}.sfv | awk '! /^;/' > ${sfv}
		rm -f ${current}.sfv

		for mp3 in *.mp3;
		do echo "${mp3}" >> ${m3u};
		done

	echo "$current: Done"

done

tws.sh:

#!/bin/sh
#
#   Removes trailing whitespaces in ASCII files.
#   $URBAN: tws.sh,v 1.0 2005/10/24 15:09:05 fafa Exp $
#

for file in `find . -type f ! -name ".*"`; do

	if [ "`file -b "$file" | grep text`" != "" ]; then
		perl -i -pe 's/\s+$/\n/' "$file"
		echo "$file: Done"
	fi

done

tree.sh:

#!/bin/sh
#
#   TREE.SH 1.0
#
#   Reads a directory or file list,
#   then writes a tree.
#
#   $URBAN: tree.sh,v 1.0 2005/10/24 15:05:09 fafa Exp $
#
#   -a, --all       Prints all files, not just directories.
#   -h, --help      Prints usage information.
#   -l, --list      Reads a list of files from stdin.
#   -v, --version   Print the version and exit.
#
#   Karl Vogel <vogelke at dnaco.net>
#   Sumaria Systems, Inc.
#

PATH=/bin:/usr/sbin:/usr/bin:/usr/local/bin

export PATH
umask 022

tag=`basename $0`

# *** Functions
#
#     die: prints an optional argument to stderr and exits.
#     warn: prints an optional argument to stderr.
#
#     A common use for "die" is with a test:
#
#     test -f /etc/passwd || die "no passwd file"
#
#     This works in subshells and loops,
#     but may not exit with a code other than 0.
#
die () {
    echo "$tag: Error: $*" 1>&2
    exit 1
}

# *** Usage
#
#     Prints an optional string plus part of the comment header
#     (if any) to stderr, and exits with code 1.
#
usage () {
    lines=`egrep -n '^# (NAME|AUTHOR)' $0 | sed -e 's/:.*//'`

    (
        case "$#"
        in
            0)  ;;
            *)  echo "Usage error: $*"; echo ;;
        esac

        case "$lines"
        in
            "") ;;

            *)  set `echo $lines | sed -e 's/ /,/'`
                sed -n ${1}p $0 | sed -e 's/^#//g' |
                    egrep -v AUTHOR:
                ;;
        esac
    ) 1>&2

    exit 1
}

# *** Version
#
#     Prints the current version to stdout.
#
version () {
    lsedscr='s/RCSfile: //
    s/.Date: //
    s/,v . .Revision: /  v/
    s/\$//g'

    lrevno='$RCSfile: tree.sh,v $ $Revision: 1.0 $'
    lrevdate='$Date: 2005/09/09 01:17:30 $'
    echo "$lrevno $lrevdate" | sed -e "$lsedscr"
    exit 0
}

# *** mktree
#
#     Sort the file information properly.
#
mktree () {
    scr='
s,^.$,,
/^$/d
s,[^/]*/\([^/]*\)$,+-----\1,
s,[^/]*/,|     ,g'

    tr '/' '\001' | sort -f | tr '\001' '/' | sed -e "$scr"
}

# *** Main program defaults
#
ac_help=
ac_prev=
ac_invalid="Invalid option; use --help to show usage"
argv=

# *** Initialize some variables set by options.
#
all=no
list=no
fopt="-type d"

for ac_option
do

    # *** If the previous option needs an argument, assign it.
    #
    case "$ac_prev" in
        "") ;;
        *)  eval "$ac_prev=\$ac_option"; ac_prev=; continue ;;
    esac

    case "$ac_option" in
        -*=*) ac_optarg=`echo "$ac_option" |
                sed 's/[-_a-zA-Z0-9]*=//'` ;;
        *)    ac_optarg= ;;
    esac

    # *** Main switch
    #
    case "$ac_option" in
        -a | -all | --all | --al | --a)
            all=yes; fopt="" ;;
    
        -h | -help | --help | --hel | --he)
            usage ;;
    
        -l | -list | --list | --lis | --li | --l)
            list=yes ;;
    
        -v | -version | --version | --versio |\
        --versi | --vers)
            version ;;
    
        -*) die "$ac_option: $ac_invalid" ;;
        *)  argv="$argv $ac_option" ;;
    esac
done

case "$ac_prev" in
    "") ;;
    *)  die "Missing argument to --`echo $ac_prev | sed 's/_/-/g'`" ;;
esac

# *** Real work starts here.
#     Test for specific features.
#
case "$argv"
in
    "") case "$list" in
            "yes")  top="" ;; # Sort reads stdin.
            *)      top="." ;;
        esac
        ;;

    *)  top=$argv  ;;
esac

# *** Print the directory tree.
#
case "$list"
in
    "no")   test -d $top || die "$top: not a directory"
            cd $top; pwd; find . $fopt -print | mktree ;;
    "yes")  mktree < $top ;;
esac

exit 0

--
Fafa Hafiz Krantz
  Research Designer @ http://www.bleed.com


-- 
___________________________________________________
Play 100s of games for FREE! http://games.mail.com/



More information about the freebsd-questions mailing list