Archive

Posts Tagged ‘shell transfer lowercase uppercase’

linux bash shell auto switch lowercase-uppercase

August 29th, 2010 No comments

Functionality:

Switch lowercase-uppercase from files given in parameters:
#if.sh -u aaa.txt bbb.txt #aaa.txt.UC bbb.txt.UC=>UPPERCASE
#if.sh -l aaa.txt bbb.txt #aaa.txt.LC bbb.txt.LC=>lowercase
Here goes the script:
#!/bin/sh
#if.sh -u aaa.txt bbb.txt #aaa.txt.UC bbb.txt.UC=>turn to upper case
#if.sh -l aaa.txt bbb.txt #aaa.txt.LC bbb.txt.LC=>turn to lower case
FILES=""
TRCASE=""
EXT=""
OPT=no

error_msg()
{
_FILENAME=$1
echo "`basename $0`: Error the conversion failed on $_FILENAME"
}

if [ $# -eq 0 ];then
echo "for more info try `basename $0` --help"
exit 1
fi

while [ $# -gt 0 ]
do
case $1 in
-u)TRCASE=upper
EXT=".UC"
OPT=yes
shift
;;
-l)TRCASE=lower
EXT=".LC"
OPT=yes
shift
;;
-help)echo "conver a file(s) to uppercase from lowercase"
exit 0
;;

-*)echo "usage: `basename $0` -[l|u] file [file..]"
exit 1
;;

*)if [ -f $1 ]
then
FILES=$FILES" "$1
else
echo "`basename $0`:Error cannot file"
fi
shift
;;
esac
done

if [ "$OPT" = "no" ]
then
echo "`basename $0`:errot you need to specify an option. No acton taken"
echo "try `basename $0` --help"
exit 1
fi

for LOOP in $FILES
do
case $TRCASE in
lower) cat $LOOP|tr "[a-z]" "[A-Z]" > $LOOP$EXT
if [ $? != 0 ]
then
error_msg $LOOP
else
echo "Converted ifle called $LOOP$EXT"
fi
;;
upper) cat $LOOP|tr "[A-Z]" "[a-z]" >$LOOP$EXT
if [ $? != 0 ]
then
error_msg $LOOP
else
echo "Converterd file called $LOOP$EXT"
fi
;;
esac
done