[Novalug] Bash math help?

Mark Smith mark@winksmith.com
Sun Apr 1 22:03:37 EDT 2012


Steve, you're 100% right, but i'll add a slight clarification.

semi's create separate statements, but the form w/o semi's is very
useful too.  it is a very useful way to pass in exported variables w/o
actually using the export command.

	VAR=foo script.sh

within the "script.sh" the variable VAR will be set, but no where else.
e.g. if you type 'echo $VAR' right after the above command a blank line
is displayed because it is not set in your current shell.

the reason why he needs the semi's is because he's trying to instantiate
the variables on the very same command line.  using semi's with an echo
is okay because 'echo' isn't doing much anyway.

to demonstrate my point:

	$ VAR=foo /bin/bash
	$ echo $VAR
	foo
	$ exit

	VAR=foo; /bin/bash
	$ echo $VAR
	
	$ exit

once you have the new bash prompt, type in 'echo $VAR' and you'll see
that the VAR variable is passed in on the first form above, but not in
the second form.

so, if you want to debug something and you have code within your
program/script that looks for the environmental variable DEBUG all you need
to do is to call it like this:

	DEBUG=yes ./your-program

and DEBUG will be set during your program and no where else.  this will
also work alternatively like so:

	export DEBUG=yes
	./your-program

i'm obviously rambling though, so i'm done.


On Sun, Apr 01, 2012 at 09:18:37PM -0400, Steve Green wrote:
> Nick,
> 
> You need to add command separators ";" after your variable definitions, 
> that's all, like this.
> [nbrenckle@smtp-cm perl]$ oldfilelines=17005; newfilelines=17003; echo 
> "$oldfilelines/$newfilelines" | bc -l
> 
> v/r,
> Steve
> -----Original Message----- 
> From: Nick Danger
> Sent: Sunday, April 01, 2012 8:53 PM
> To: NOVALUG
> Subject: [Novalug] Bash math help?
> 
> 
> Ok, whats wrong with this?
> 
> [nbrenckle@smtp-cm perl]$ oldfilelines=17005 newfilelines=17003 echo
> "$oldfilelines/$newfilelines" | bc -l
> (standard_in) 1: parse error
> [nbrenckle@smtp-cm perl]$ echo "17005/17003" | bc -l
> 1.00011762630124095747
> [nbrenckle@smtp-cm perl]$
> 
> I need to do some quick percentage in a shell script. Int is fine, I
> don't need precision, just want a rough idea of how much a file has
> changed between runs. Script bombs out with the error above, so I tried
> to run it just on the command line. Same error. Is it how I am echoing
> the variables?
> 
> Nick
> _______________________________________________
> Novalug mailing list
> Novalug@calypso.tux.org
> http://calypso.tux.org/mailman/listinfo/novalug 
> 
> _______________________________________________
> Novalug mailing list
> Novalug@calypso.tux.org
> http://calypso.tux.org/mailman/listinfo/novalug

-- 
Mark Smith
mark@winksmith.com
mark@tux.org



More information about the Novalug mailing list