From owner-freebsd-questions@FreeBSD.ORG Mon Oct 31 21:21:39 2005 Return-Path: X-Original-To: questions@freebsd.org Delivered-To: freebsd-questions@FreeBSD.ORG Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 5B88816A41F for ; Mon, 31 Oct 2005 21:21:39 +0000 (GMT) (envelope-from fteg@london.com) Received: from webmail-outgoing.us4.outblaze.com (webmail-outgoing2.us4.outblaze.com [205.158.62.67]) by mx1.FreeBSD.org (Postfix) with ESMTP id 0390543D46 for ; Mon, 31 Oct 2005 21:21:38 +0000 (GMT) (envelope-from fteg@london.com) Received: from unknown (unknown [192.168.9.180]) by webmail-outgoing.us4.outblaze.com (Postfix) with QMQP id 577E118001D5 for ; Mon, 31 Oct 2005 21:21:38 +0000 (GMT) X-OB-Received: from unknown (205.158.62.49) by wfilter.us4.outblaze.com; 31 Oct 2005 21:21:38 -0000 Received: by ws1-1.us4.outblaze.com (Postfix, from userid 1001) id 4456283C0D; Mon, 31 Oct 2005 21:21:38 +0000 (GMT) Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="iso-8859-1" MIME-Version: 1.0 From: "Fafa Hafiz Krantz" To: questions@freebsd.org Date: Mon, 31 Oct 2005 16:21:37 -0500 Received: from [213.187.181.70] by ws1-1.us4.outblaze.com with http for fteg@london.com; Mon, 31 Oct 2005 16:21:37 -0500 X-Originating-Ip: 213.187.181.70 X-Originating-Server: ws1-1.us4.outblaze.com Message-Id: <20051031212138.4456283C0D@ws1-1.us4.outblaze.com> Cc: Subject: The best scripts ever (trick or treat) X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 31 Oct 2005 21:21:39 -0000 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 # 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/