Re: Writing Variables to a File (293 Views)
Reply
Super Advisor
Scott Frye_1
Posts: 272
Registered: ‎05-14-2004
Message 1 of 23 (293 Views)

Writing Variables to a File

First of all I apologize for putting a basic question out here. Due to heay snow, I'm working from home so I don't have documentation to refer to...
I need to write several variables to a file. I'm trying...
echo "$CPU $TTY $PID $USERNAME $PRI $NI $SIZE $RES $STATE $TIME $WCPU $CPU $COMMAND" > /home/sysadmin/topcpu/check_cpu.txt
but it is not working. Can someone tell me what I'm doing wrong?

Thanks to all

Scott
Please use plain text.
Honored Contributor
Ralph Grothe
Posts: 2,631
Registered: ‎04-09-2000
Message 2 of 23 (293 Views)

Re: Writing Variables to a File

Does a "set" at the shell display these variables as set?
What kind of content do you get in the file (empty)?
Is this run from a script with a different environment or other variable settings?
Madness, thy name is system administration
Please use plain text.
Exalted Contributor
Steven E. Protter
Posts: 33,806
Registered: ‎08-15-2002
Message 3 of 23 (293 Views)

Re: Writing Variables to a File

env > /tmp/env.txt

You will see everything you need and then some.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Please use plain text.
Super Advisor
Scott Frye_1
Posts: 272
Registered: ‎05-14-2004
Message 4 of 23 (293 Views)

Re: Writing Variables to a File

Sorry, I wasn't clear. This is for a script I am writing. The variables contain a line from top. I want to write each of these variables to a file. With this command, the file doesn't even get created. Do I have the syntax correct? Do I need to touch the file first?
Please use plain text.
Honored Contributor
harry d brown jr
Posts: 8,418
Registered: ‎12-12-2000
Message 5 of 23 (293 Views)

Re: Writing Variables to a File

I know this sounds stupid, but does the directory "/home/sysadmin/topcpu" exist??

ls -l /home/sysadmin/topcpu

live free or die
harry d brown jr
Live Free or Die
Please use plain text.
Honored Contributor
Georg Tresselt
Posts: 2,668
Registered: ‎05-13-2002
Message 6 of 23 (293 Views)

Re: Writing Variables to a File

What exactly goes wrong?
Do you get an error message?
Or what do you find in the file if it is created at all?
http://www.tresselt.eu
Please use plain text.
Honored Contributor
Victor BERRIDGE
Posts: 2,576
Registered: ‎07-10-2000
Message 7 of 23 (293 Views)

Re: Writing Variables to a File

Hi,
I just tried your echo, and I get the script created (empty line because none of the VARs have values...) so I suspect you have something wrong with the permissions on your directory, has whatever executes the script the write permission to that directory?


All the best
Victor
Please use plain text.
Honored Contributor
Ralph Grothe
Posts: 2,631
Registered: ‎04-09-2000
Message 8 of 23 (293 Views)

Re: Writing Variables to a File

Has your shell the noclobber option set?

# set -o|grep clobber
noclobber off
Madness, thy name is system administration
Please use plain text.
Honored Contributor
Ralph Grothe
Posts: 2,631
Registered: ‎04-09-2000
Message 9 of 23 (293 Views)

Re: Writing Variables to a File

Bah, forget my silly question,
even if the file should at least be created.
Madness, thy name is system administration
Please use plain text.
Honored Contributor
Ralph Grothe
Posts: 2,631
Registered: ‎04-09-2000
Message 10 of 23 (293 Views)

Re: Writing Variables to a File

How does your script populate the variables?

I guess you parse the top dump from something similar to

top -d 2 -f /tmp/top.dump
Madness, thy name is system administration
Please use plain text.
Super Advisor
Scott Frye_1
Posts: 272
Registered: ‎05-14-2004
Message 11 of 23 (293 Views)

Re: Writing Variables to a File

Here is my entire script.
Please use plain text.
Honored Contributor
Georg Tresselt
Posts: 2,668
Registered: ‎05-13-2002
Message 12 of 23 (293 Views)

Re: Writing Variables to a File

Is there a ">" missing in this line?

top -d 1 -n 1 -f /home/sysadmin/topcpu/topck.txt
http://www.tresselt.eu
Please use plain text.
Honored Contributor
Stephen Keane
Posts: 1,155
Registered: ‎11-24-2004
Message 13 of 23 (293 Views)

Re: Writing Variables to a File

It's not the old shell can't deal with floating point numbers in comparisons malarky again is it?
Please use plain text.
Honored Contributor
Patrick Wallek
Posts: 13,731
Registered: ‎06-21-2000
Message 14 of 23 (293 Views)

Re: Writing Variables to a File

OK, I think I may have found part of your issue.

In your script you read in a command into the variable COMM.

However you use the variable COMMAND rather than COMM in several places, including an IF statement. Fix that and you might see a bit of progress.
Please use plain text.
Honored Contributor
Ralph Grothe
Posts: 2,631
Registered: ‎04-09-2000
Message 15 of 23 (293 Views)

Re: Writing Variables to a File

Does any of your if tests, in whose body you write to check_cpu.txt, ever evaluate to true?

Stupid question, but have you tried to run your script with the shell's poor man's debugger "sh -x"?
Madness, thy name is system administration
Please use plain text.
Honored Contributor
Ralph Grothe
Posts: 2,631
Registered: ‎04-09-2000
Message 16 of 23 (293 Views)

Re: Writing Variables to a File

Off topic, where do you get the heavy snow?
(I hope not on your screen ;-)
So at least one of us is heaving a white Xmas.
Madness, thy name is system administration
Please use plain text.
Super Advisor
Scott Frye_1
Posts: 272
Registered: ‎05-14-2004
Message 17 of 23 (293 Views)

Re: Writing Variables to a File

Snow is in Indianapolis IN.

I've got a lot of replies to look into. I'll assign points accordingly.
Please use plain text.
Super Advisor
Scott Frye_1
Posts: 272
Registered: ‎05-14-2004
Message 18 of 23 (293 Views)

Re: Writing Variables to a File

OK, to follow up with a few things...
Yes /home/sysadmin/topcpu exists

Permissions are set as root, running script as root.

Changed COMMAND to COMM, still doesn't create files.

And yes, some of my if statements do evalutate to true.

Seems like this should be so easy, I don't know why I can't get these files to create. I'm sure I've missed something.
Please use plain text.
Super Advisor
Scott Frye_1
Posts: 272
Registered: ‎05-14-2004
Message 19 of 23 (293 Views)

Re: Writing Variables to a File

Well, I knew it was something I was doing (at least part of it). I got the files to create but they are empty. I can figure the rest of it out I think. Just to make sure, is there any reason I shouldn't expect to see data in my files by doing
echo "$var1 $var2" > /newfile.txt

My problem was my last if statement wasn't evaluating the correct variable. Oversight on my part.
Please use plain text.
Honored Contributor
Patrick Wallek
Posts: 13,731
Registered: ‎06-21-2000
Message 20 of 23 (293 Views)

Re: Writing Variables to a File

OK, how about doing this for us and posting the results:

# sh -x scriptname

Or just put

#!/usr/bin/sh -x

at the top of the script, execute it and attach the results. Maybe the trace will give us some more clues.
Please use plain text.
Honored Contributor
Patrick Wallek
Posts: 13,731
Registered: ‎06-21-2000
Message 21 of 23 (293 Views)

Re: Writing Variables to a File

Yes,

echo "$var1 $var2" > filename

should work fine. Maybe try it without the quotes around the variable and see what happens?
Please use plain text.
Super Advisor
Scott Frye_1
Posts: 272
Registered: ‎05-14-2004
Message 22 of 23 (293 Views)

Re: Writing Variables to a File

I believe I have it from here. I have one more conditional if to debug and it should work fine.

Thanks to all who helped. Working from home is great, if you have the tools

Happy Holiday's to all!!
Please use plain text.
Super Advisor
Scott Frye_1
Posts: 272
Registered: ‎05-14-2004
Message 23 of 23 (293 Views)

Re: Writing Variables to a File

I was evaluating the wrong variable in my if statement.
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation