LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   parse error with bc (https://www.linuxquestions.org/questions/programming-9/parse-error-with-bc-349512/)

fisayo 08-03-2005 05:42 AM

parse error with bc
 
I'm currently working on a script that involves simple arithmetric calculations but I have a problem with a line:

docmedfree=$(echo "$freespace - $sizedoc"|bc)
I get:
(standard_in) 2: parse error
on the output screen.

The freespace variable is gotten from:
freespace=$(df -hk $BACKUPDIR | grep -v Available | awk '{print $4}')

and the sizedoc is from:

for sum in `cat fileindex | awk '!( /mp3/||/wma/||/DAT/||/wav/ ) { print $1 }'` ; do
sizedocs=$( echo "$sum + $sizedocs" | bc)
done

On echoing freespace and sizedoc, I get
4896688 3104284 respectively
Since both are numeric variables, I can't tell where the error is coming from.

I'm confused cause I have similar bc statements and don't get that kind of error Others are:
gb=$(echo "$1/1000000"|bc)
mb=$(echo "$1/1000"|bc)
sz=`echo "scale=2 ; $1/(1000^2)" | bc`

I've already searched the forum and have not yet found much info that may help. I don't mind any links anyway.

archtoad6 08-03-2005 06:28 AM

You left the 's' out of "$sizedocs":
Code:

$ docmedfree=$(echo "$freespace - $sizedoc"|bc)
$ #      should be
$ docmedfree=$(echo "$freespace - $sizedocs"|bc)

BTW, if you're using bash, the following also works:
Code:

$ docmedfree=$(($freespace-$sizedocs))

keefaz 08-03-2005 06:28 AM

If you use bash, you don't need always bc for integer operations,
example :
Code:

docmedfree=$(($freespace - $sizedocs))
Code:

for sum in `cat fileindex | awk '!( /mp3/||/wma/||/DAT/||/wav/ ) { print $1 }'` ; do
    let "sizedocs+=$sum"
done


fisayo 08-03-2005 07:51 AM

How can I thank you guys? Your additions have turned the life of the script. Thanks a lot. I can't even imagine that I spent days trying out to figure out a workaround for the parse error thing all because of a typo.

The arithmetric substitutions also helped (it kinda made the script run faster) and I really like the let command, it eases out a lot of tension.

Thanks, my script is working well now. I did like to post it for cleaning if you guys won't mind (you'll have to tell me how).

archtoad6 08-03-2005 08:21 AM

You're welcome. Your Q was 1 of those it is fun / instructive to figure out.

To add your script to a post, put it in a "Code" block:[list=1][*]Copy (highlight etc.) it from the source.[*]Click the "Code" button above the msg. entry area.[*]Paste it into the Code entry area -- don't worry about line breaks.[*]Click "OK".[/list=1]

fisayo 08-04-2005 04:55 AM

Thanks! I just figured that my script is so long and I can only post the 'dirtiest' part, however, I didn't even find the code button that you're talking about.

keefaz 08-04-2005 05:14 AM

Just put the code between [ code ] and [ /code ] markups
(without the spaces in [] )

fisayo 08-04-2005 09:23 AM

OK, here are my dirty parts:

I wrote this part to search for certain filetypes and output their sizes (along with names with paths) to a file

Code:

FILETYPES="*doc|*txt|*pdf|*rtf|*xls|*pst|*pps|*ppt|*mp3|*wma|*dat|*wav|*mdi|*cdr|*htm|*jpg|*gif|*msg|*DOC|*TXT|*PDF|*RTF|*XLS|*PST|*PPS|*PPT|*MP3|*WMA|*DAT|*WAV|*MDI|*CDR|*HTM|*JPG|*GIF|*MSG"
echo -e "Creating index of selected folders...\nSearching for filetypes: \n$FILETYPES\n in selected folders...\n"

rm fileindex tmpindex 2> /dev/null        # May exist from previous backups

for FOLDER in $FOLDERS ; do
        # Get search for list of files
       
        echo $FILETYPES | sed 's/|/ -o -name /g' | xargs find "$FOLDER" -name > tmpindex
       
        # Save the disk usage of the files above
        while read FILE ; do du -hk "$FILE" ; done < tmpindex >> fileindex
done

The second part was to get the total size of Document files and Media files respectively. I modified it after I got your corrections yesterday

Code:

sizedocs=0 ; sizemedia=0                        # Size for document and media files respectively

for SUM in `awk '!( /[mM][pP]3/||/[wW][mM][aA]/||/[dD][aA][tT]/||/[wW][aA][vV]/ ) { print $1 }' fileindex` ; do
        let "sizedocs+=$SUM"
done

for SUM in `awk '( /[mM][pP]3/||/[wW][mM][aA]/||/[dD][aA][tT]/||/[wW][aA][vV]/ ) { print $1 }' fileindex` ; do
        let "sizemedia+=$SUM"
done

The next on was to format figures in human readable forms (like df). I had issues with this function for some time

Code:

units (){
        unit=" "
        gb=$(( $1/1000000 )) ; mb=$(( $1/1000 ))
       
        if [ $gb -gt 0 ] ; then
                unit=GB
                sz=`echo "scale=2 ; $1/(1000^2)" | bc`
        elif [ $mb -gt 0 ] ; then
                unit=MB
                sz=`echo "scale=2 ; $1/1000" | bc` ;
        else
                unit=KB
                sz=$1
        fi
       
        echo $sz$unit
        unset sz unit gb mb        # Paid off when I started to get funny errors
}

I hope that I'm not going off post already?
I have a problem with this last part, it has to do with the cp command (or perhaps permission issues). This part is supposed to copy the files listed in the tmpindex file (from above) to a directory ($HOME/BACKUPS).

Code:

while read FILE ; do
      cp -v --parents "$FILE" $CLIENTDIR
done < ~/tmpindex

The problem is that whenever I run the script as a normal user, it copies only the first file (in the tmpindex list) and the remaining get permission denied stuff:
OUTPUT:
Code:

/mnt -> /home/fisayo/BACKUPS/asdf_dsfadi_04-August-05_Backup/mnt
/mnt/hdc1 -> /home/fisayo/BACKUPS/asdf_dsfadi_04-August-05_Backup/mnt/hdc1
/mnt/hdc1/New Folder -> /home/fisayo/BACKUPS/asdf_dsfadi_04-August-05_Backup/mnt/hdc1/New Folder
`/mnt/hdc1/New Folder/182 DAYS TEMPL.xls' -> `/home/fisayo/BACKUPS/asdf_dsfadi_04-August-05_Backup/mnt/hdc1/New Folder/182 DAYS TEMPL.xls'
`/mnt/hdc1/New Folder/A MM REPOR december  2004 TPC .xls' -> `/home/fisayo/BACKUPS/asdf_dsfadi_04-August-05_Backup/mnt/hdc1/New Folder/A MM REPOR december  2004 TPC .xls'
cp: cannot create regular file `/home/fisayo/BACKUPS/asdf_dsfadi_04-August-05_Backup/mnt/hdc1/New Folder/A MM REPOR december  2004 TPC .xls': Permission denied
`/mnt/hdc1/New Folder/ACCOUNTING ENTRIES by TPC.doc' -> `/home/fisayo/BACKUPS/asdf_dsfadi_04-August-05_Backup/mnt/hdc1/New Folder/ACCOUNTING ENTRIES by TPC.doc'
cp: cannot create regular file `/home/fisayo/BACKUPS/asdf_dsfadi_04-August-05_Backup/mnt/hdc1/New Folder/ACCOUNTING ENTRIES by TPC.doc': Permission denied
`/mnt/hdc1/New Folder/ACCT 800003300 FRM JUN TO SEPT 04.xls' -> `/home/fisayo/BACKUPS/asdf_dsfadi_04-August-05_Backup/mnt/hdc1/New Folder/ACCT 800003300 FRM JUN TO SEPT 04.xls'
cp: cannot create regular file `/home/fisayo/BACKUPS/asdf_dsfadi_04-August-05_Backup/mnt/hdc1/New Folder/ACCT 800003300 FRM JUN TO SEPT 04.xls': Permission denied

However, if I run it as root, I can copy all the files without stress
OUTPUT:
Code:

/mnt -> /root/BACKUPS/asdf_dsfadi_04-August-05_Backup/mnt
/mnt/hdc1 -> /root/BACKUPS/asdf_dsfadi_04-August-05_Backup/mnt/hdc1
/mnt/hdc1/New Folder -> /root/BACKUPS/asdf_dsfadi_04-August-05_Backup/mnt/hdc1/New Folder
`/mnt/hdc1/New Folder/182 DAYS TEMPL.xls' -> `/root/BACKUPS/asdf_dsfadi_04-August-05_Backup/mnt/hdc1/New Folder/182 DAYS TEMPL.xls'
`/mnt/hdc1/New Folder/A MM REPOR december  2004 TPC .xls' -> `/root/BACKUPS/asdf_dsfadi_04-August-05_Backup/mnt/hdc1/New Folder/A MM REPOR december  2004 TPC .xls'
`/mnt/hdc1/New Folder/ACCOUNTING ENTRIES by TPC.doc' -> `/root/BACKUPS/asdf_dsfadi_04-August-05_Backup/mnt/hdc1/New Folder/ACCOUNTING ENTRIES by TPC.doc'
`/mnt/hdc1/New Folder/ACCT 800003300 FRM JUN TO SEPT 04.xls' -> `/root/BACKUPS/asdf_dsfadi_04-August-05_Backup/mnt/hdc1/New Folder/ACCT 800003300 FRM JUN TO SEPT 04.xls'

What do you think the problem can be? Can it be with the long pathnames or folder permissions? I use Mandriva.
Thanks a lot, you guys have been very helpful.


All times are GMT -5. The time now is 06:17 AM.