Solved

setting a UNIX environmental variable in a one-liner

Posted on 2004-04-23
8
456 Views
Last Modified: 2010-04-21

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

0
Comment
Question by:mikem22
8 Comments
 
LVL 11

Accepted Solution

by:
avizit earned 250 total points
ID: 10900685
setenv TVS `cksum file.ext | cut -d" " -f1`

seems to work on my cygwin system ..

AFAIK what is between the backquotes is executed and the result returned , so i guess you dont need the "echo"

try and check on your system

/abhijit/
0
 
LVL 6

Expert Comment

by:bira
ID: 10900800
setenv MJMCKSUM `cksum file.ext  | cut -d" " -f1`
0
 
LVL 3

Expert Comment

by:dtkerns
ID: 10901250
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
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 48

Expert Comment

by:Tintin
ID: 10914430
No need for the UUOE
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 10921328
setenv MJMCKSUM `cksum file.ext ` | awk '{print $1}'`

BTW, backticks can be nested, depends on the shell only
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 10966619
damn, ther is a typo in my last comment
setenv MJMCKSUM `cksum file.ext | awk '{print $1}'`
0
 
LVL 20

Expert Comment

by:Gns
ID: 10996076
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
0
 
LVL 20

Expert Comment

by:Gns
ID: 10996096
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
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question