Link to home
Start Free TrialLog in
Avatar of mikem22
mikem22

asked on

setting a UNIX environmental variable in a one-liner


Hi

I am looking to set an environmental varibale in a one liner that I will present to the operating system from a program. I need to find the 32 bit CRC checksum of a file, the UNIX for this would be...

  cksum file.ext

Simple enough, except that this returns several columns of data, I know that I only need the first column which is the actuall checksum value so....

  echo `cksum file.ext ` | cut -d" " -f1

Will get the data I need. I then need to present this to a setenv command and this is where I am having trouble with the syntax, all of the following do not work....

  setenv MJMCKSUM `echo ``cksum file.ext ` | cut -d" " -f1``

  setenv MJMCKSUM `echo `cksum file.ext ` | cut -d" " -f1`

I thought either of these would substitute the inner command to the cksum value and then use this value to set the MJMCKSUM environmental variable. Is there something wrong with my syntax or is this a two step operation?

Thanks in advance

Mike

ASKER CERTIFIED SOLUTION
Avatar of avizit
avizit

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bira
bira

setenv MJMCKSUM `cksum file.ext  | cut -d" " -f1`
the `cmd` construct cannot be nested without \

as in:

echo `echo hello \`date\``

you can, however, nest this form:

$(cmd)

as in:

echo $(echo hello $(date))

but as pointed out above, nesting is not required for what you need
Avatar of Tintin
No need for the UUOE
setenv MJMCKSUM `cksum file.ext ` | awk '{print $1}'`

BTW, backticks can be nested, depends on the shell only
damn, ther is a typo in my last comment
setenv MJMCKSUM `cksum file.ext | awk '{print $1}'`
Wellcome to "Le Grand Typo Club" Achim:-). Membership is free;-).

The question I'm asking myself here is _why_ would you want this?
The exported environment will go away with the _child process executing the commands_ so your "controlling program" will never be able to "see" the variable anyway. So if you run a oneline system() or similar, your program (as a parent) or any sibling to that system() will not see it.
If you need the parent to be able to read the value, you'd probably most easily accomplish this by writing the result to a file in the system("cksum file.ext | awk '{print $1}'>/tmp/tempfile") ... and reading that file in the parent. Cumbersome and ugly:-).
This behaviour is a property of unix, not any specific language.

Std disclaimer: I might have missunderstood your intentions entirely... and look forward to any input in that direction:-).

-- Glenn
Obviously a case of me overinterpreting the phrasing of the question:-)... Not to mention the huge pause between starting to write a comment and actually clicking submit:-):-).

-- Glenn