[Malvern] Bash script..

Angus Clarke angus at zeppelin.co.uk
Sun Jun 5 10:57:30 BST 2005


Robin Wilson wrote:

> Hi all,
>
> I haven't yet learnt how to write bash scripts - althought it is on my 
> to do list. At the moment though, I need to write a bash script that, 
> when given a directory as a command line arguement, will:
> * Rename all files in that directory from filename.JPG to filename.jpg 
> (leaving the filename bit the same, just making the JPG in small 
> letters - I'm sure this can be done via RegExps)
> * Create a file called 'description' in that directory containing a 
> list of all the filenames in that directory then a comma and then the 
> filename again... eg:
>    filename1.jpg, filename1.jpg
>    filename2.jpg, filename2.jpg
>
> Is this possible? and would someone be able to write me a script to do 
> that?
>
> Thanks in advance,
>
> Robin
>
> _______________________________________________
> Malvern mailing list
> Malvern at mailman.lug.org.uk
> http://mailman.lug.org.uk/mailman/listinfo/malvern
>


-------------- next part --------------
#! /bin/bash

# OTT?

# VARS
DIR=$1
DESCRIPTION=${DIR}/description

# Usual error checks
if [ ! -d ${DIR} ] ; then
 echo "Directory ${DIR} does not exist"
 echo "Exiting ..."
 exit 1
fi

if [ -f ${DESCRIPTION} -a ! -w ${DESCRIPTION} ] ; then
 echo "Description file: ${DESCRIPTION} exists and is NOT writable"
 echo "Exiting ..."
 exit 2
elif [ -f ${DESCRIPTION} -a -w ${DESCRIPTION} ] ; then
 echo -e "Description file: ${DESCRIPTION} already exists, do you want to continue y/n ? [n]\b\b\c"
 read ANS
 case $ANS in
  y|Y|y*|Y*) ;;
  *) echo "Exiting ..."; exit 3;;
 esac
fi

# Any (permission) errors from moving the files should be output to screen

for FILE in `ls ${DIR}/*.JPG`
do
 NEWFILE=`echo ${FILE} | sed "s/\.JPG$/\.jpg/g"`
 mv -f ${FILE} ${NEWFILE}
done

for FILE in `ls ${DIR}`
do
 BASEFILE=`basename ${FILE}`
 echo "${BASEFILE}, ${BASEFILE}" >> ${DESCRIPTION}
done



More information about the Malvern mailing list