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. |
You left the 's' out of "$sizedocs":
Code:
$ docmedfree=$(echo "$freespace - $sizedoc"|bc) Code:
$ docmedfree=$(($freespace-$sizedocs)) |
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 |
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). |
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] |
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.
|
Just put the code between [ code ] and [ /code ] markups
(without the spaces in [] ) |
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" Code:
sizedocs=0 ; sizemedia=0 # Size for document and media files respectively Code:
units (){ 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 OUTPUT: Code:
/mnt -> /home/fisayo/BACKUPS/asdf_dsfadi_04-August-05_Backup/mnt OUTPUT: Code:
/mnt -> /root/BACKUPS/asdf_dsfadi_04-August-05_Backup/mnt Thanks a lot, you guys have been very helpful. |
All times are GMT -5. The time now is 12:12 AM. |