Hi ormsafetyo,
I'am just starting with shells and i'am having problems with my first one.
I need to write a shell script that receives 3 parameters (Filename, Item Description, and Amount)
when the script is invoked, it is suppossed to insert the "Item Description" and "Amount" to the the exsisting text file, I guess is working fine, but I need to format the number from this 12530 to this 12,530 or from this 1225550 to this 1,225,550
I'am trying to use your function formatCurr but I guess I don't know how to use it in the correct way or even how to call the function
I will really appreciate if you can help me with this!!!!!!!
THIS IS THE ERROR I GET WHEN RUNNING THE SCRIPT:
----------------------------------------------------------------------
B0581342 >prueba.sh 'adrian.txt' 'item description' '12530'
UX:sh (prueba.sh): ERROR: prueba.sh: Syntax error at line 1: `formatCurr' unexpected
B0581342 >more adrian.txt
item description 12530
B0581342 >
THE FOLLOWING IS THE SCRIPT:
--------------------------------------------
#!/bin/sh
################################################################
LOCALDIR=/usrclass/soa/
cd /usrclass/soa/
cd $LOCALDIR
FAXFILE=$1
DESRUBRO=$2
MONRUBRO=$3
RUBRO=$2
MONTO=$3
if [ ! -f "$FAXFILE" ]; then
touch "$FAXFILE" # cria o arquivo de LOG se ele nM-co existe
else
if [ `ls -al $FAXFILE | awk '{print $5}'` -gt 5000000 ]; then
mv $FAXFILE `ls $FAXFILE | cut -f1 -d .`".old"
touch "$FAXFILE"
fi
fi
CONT_RUBRO=40
CONT_MONTO=15
ESPACIOS_RUBRO=" "
ESPACIOS_MONTO=" "
LINEA_RUBRO=" "
lenrubro=`echo $DESRUBRO | wc -c`
lenrubro=`expr $lenrubro - 1`
lenmonto=`echo $MONRUBRO | wc -c`
lenmonto=`expr $lenmonto - 1`
if [ $lenrubro -lt $CONT_RUBRO ]; then
while [ $lenrubro -lt $CONT_RUBRO ]
do
ESPACIOS_RUBRO="$ESPACIOS_RUBRO"" "
lenrubro=`expr $lenrubro + 1`
done
RUBRO=$RUBRO$ESPACIOS_RUBRO
fi
if [ $lenmonto -lt $CONT_MONTO ]; then
while [ $lenmonto -lt $CONT_MONTO ]
do
ESPACIOS_MONTO="$ESPACIOS_MONTO"" "
lenmonto=`expr $lenmonto + 1`
done
MONTO=$ESPACIOS_MONTO$MONTO
fi
LINEA_RUBRO=$RUBRO$MONTO
echo "$LINEA_RUBRO" >> $FAXFILE
#****************************************************
#THESE ARE THE TWO LINES I ADDED TO TRY TO USE THE FUNCTION
#I ALSO TRY TO USE THESE TWO LINES AT THE END
dollar_format=`formatCurr("$MONRUBRO")`
echo "$dollar_format" >> $FAXFILE
#********************************************************
#*******************************************************
function formatCurr {
dollar_amt=$3
cents=`echo $dollar_amt | grep '\.'`
if [[ "x${cents}x" != "xx" ]]
then
cent_amt=`echo $dollar_amt | cut -d"." -f2 | cut -c 1-2`
dollar_amt=`echo $dollar_amt | cut -d"." -f1`
cent_amt=`echo ".$cent_amt"`
fi
length=`echo $dollar_amt | awk '{ print length($0) }'`
mod=`expr $length % 3`
div3=`expr $length / 3`
if [[ $mod -ne 0 ]]
then
dollar_fin=`echo $dollar_amt | cut -c 1-$mod`
fi
modp1=`expr $mod + 1`
incr=`expr $mod + 3`
for (( i = 0; i < $div3; i++ ))
do
mySub=`echo $dollar_amt | cut -c ${modp1}-${incr}`
if [[ $modp1 -ne 1 ]]
then
dollar_fin=`echo ${dollar_fin},${mySub}`
else
dollar_fin=`echo ${dollar_fin}${mySub}`
fi
incr=`expr $incr + 3`
modp1=`expr $modp1 + 3`
done
if [[ "x${cent_amt}x" != "xx" ]]
then
dollar_fin=`echo ${dollar_fin}${cent_amt}`
fi
echo "$"$dollar_fin
}
#*******************************************************
dollar_format=`formatCurr(MONRUBRO)`
echo "$dollar_format" >> $FAXFILE