Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 31 Oct 2005 16:21:37 -0500
From:      "Fafa Hafiz Krantz" <fteg@london.com>
To:        questions@freebsd.org
Subject:   The best scripts ever (trick or treat)
Message-ID:  <20051031212138.4456283C0D@ws1-1.us4.outblaze.com>

next in thread | raw e-mail | index | archive | help

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"`" !=3D "" ]; 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=3D"`dirname ${file}`"
	prefix=3D"`basename ${file} | sed 's/.nfo//g'`"
	current=3D"`basename ${directory}`"
	sfv=3D"${directory}/${prefix}.sfv"
	m3u=3D"${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`" !=3D "" ]; 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@dnaco.net>
#   Sumaria Systems, Inc.
#

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

export PATH
umask 022

tag=3D`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=3D`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=3D's/RCSfile: //
    s/.Date: //
    s/,v . .Revision: /  v/
    s/\$//g'

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

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

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

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

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

for ac_option
do

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

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

    # *** Main switch
    #
    case "$ac_option" in
        -a | -all | --all | --al | --a)
            all=3Dyes; fopt=3D"" ;;
=20=20=20=20
        -h | -help | --help | --hel | --he)
            usage ;;
=20=20=20=20
        -l | -list | --list | --lis | --li | --l)
            list=3Dyes ;;
=20=20=20=20
        -v | -version | --version | --versio |\
        --versi | --vers)
            version ;;
=20=20=20=20
        -*) die "$ac_option: $ac_invalid" ;;
        *)  argv=3D"$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=3D"" ;; # Sort reads stdin.
            *)      top=3D"." ;;
        esac
        ;;

    *)  top=3D$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


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




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20051031212138.4456283C0D>