LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-03-2005, 05:42 AM   #1
fisayo
Member
 
Registered: Apr 2005
Distribution: Suse 9.3 | Mandrake 10.0 - 10.1
Posts: 39

Rep: Reputation: 15
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.
 
Old 08-03-2005, 06:28 AM   #2
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 230Reputation: 230Reputation: 230
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))

Last edited by archtoad6; 08-03-2005 at 06:32 AM.
 
Old 08-03-2005, 06:28 AM   #3
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,342

Rep: Reputation: 73
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
 
Old 08-03-2005, 07:51 AM   #4
fisayo
Member
 
Registered: Apr 2005
Distribution: Suse 9.3 | Mandrake 10.0 - 10.1
Posts: 39

Original Poster
Rep: Reputation: 15
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).
 
Old 08-03-2005, 08:21 AM   #5
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 230Reputation: 230Reputation: 230
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]
 
Old 08-04-2005, 04:55 AM   #6
fisayo
Member
 
Registered: Apr 2005
Distribution: Suse 9.3 | Mandrake 10.0 - 10.1
Posts: 39

Original Poster
Rep: Reputation: 15
Angry

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.
 
Old 08-04-2005, 05:14 AM   #7
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,342

Rep: Reputation: 73
Just put the code between [ code ] and [ /code ] markups
(without the spaces in [] )
 
Old 08-04-2005, 09:23 AM   #8
fisayo
Member
 
Registered: Apr 2005
Distribution: Suse 9.3 | Mandrake 10.0 - 10.1
Posts: 39

Original Poster
Rep: Reputation: 15
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Parse Error, but why? Mistro116@yahoo.com Programming 2 11-13-2005 02:38 PM
in gcc compiler error: parse error before string constsnt cynthia_thomas Linux - Networking 1 10-20-2005 01:29 AM
Parse Error verbatim Programming 3 04-22-2005 09:16 AM
parse error? ams Programming 5 03-02-2005 04:56 AM
What is parse error ? edhan Linux - Newbie 64 09-17-2003 06:49 AM


All times are GMT -5. The time now is 11:09 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration