Languages and Scripting
cancel
Showing results for 
Search instead for 
Did you mean: 

Writing Variables to a File

Highlighted
Scott Frye_1
Super Advisor

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
22 REPLIES
Ralph Grothe
Honored Contributor

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
Steven E. Protter
Exalted Contributor

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
Scott Frye_1
Super Advisor

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?
harry d brown jr
Honored Contributor

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
Georg Tresselt
Honored Contributor

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
Victor BERRIDGE
Honored Contributor

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
Ralph Grothe
Honored Contributor

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
Ralph Grothe
Honored Contributor

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
Ralph Grothe
Honored Contributor

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
Scott Frye_1
Super Advisor

Re: Writing Variables to a File

Here is my entire script.
Georg Tresselt
Honored Contributor

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
Stephen Keane
Honored Contributor

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?
Patrick Wallek
Honored Contributor

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.
Ralph Grothe
Honored Contributor

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
Ralph Grothe
Honored Contributor

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
Scott Frye_1
Super Advisor

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.
Scott Frye_1
Super Advisor

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.
Scott Frye_1
Super Advisor

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.
Patrick Wallek
Honored Contributor

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.
Patrick Wallek
Honored Contributor

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?
Scott Frye_1
Super Advisor

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!!
Scott Frye_1
Super Advisor

Re: Writing Variables to a File

I was evaluating the wrong variable in my if statement.