LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-27-2005, 06:19 PM   #1
happy78
Member
 
Registered: Aug 2003
Posts: 50

Rep: Reputation: 15
Problem of echoing to a file


Hi guys,

I have this problem echoing values of 22.9% and such number with % to a file. Here is my script:

#!/bin/bash

let freeMB=`free -m | tail -2 | head -1 | awk '{print $4}'`
cpus=($(top -n 1))

[ -f /tmp/$1 ] && rm /tmp/$1

echo $freeMB >> /tmp/$1 #free Mem
echo ${cpus[48]} >> /tmp/$1
echo ${cpus[56]} >> /tmp/$1
echo "hello"
echo ${cpus[64]} >> /tmp/$1
echo ${cpus[72]} >> /tmp/$1
=======================================

freeMB's value and "hello" are echoed to /tmp/somefile

but values ${cpu[index]} aren't echoed to /tmp/somefile. However, they are echoed to screen.
Is it true that the value of cpu such as: 30.0% causes such problem because of the "%" ?
Is there any way to echo those values to a file?

thank you for your time.
 
Old 09-27-2005, 06:25 PM   #2
happy78
Member
 
Registered: Aug 2003
Posts: 50

Original Poster
Rep: Reputation: 15
Actually, I invoke that script above from a java file. That causes problem with the echoing of array cpu.
But if i was to run the script directly, it's fine.

through java, the script is called and it is supposed to echo all values that i wanted to a file, but it only does some and ignore those values of array.
 
Old 09-27-2005, 06:51 PM   #3
kjordan
Member
 
Registered: Jul 2004
Distribution: LFS, I felt the itch and scratched it
Posts: 227

Rep: Reputation: 31
How are you calling it? Are you using some sort of system execute call to execute a .sh file?
 
Old 09-27-2005, 06:56 PM   #4
happy78
Member
 
Registered: Aug 2003
Posts: 50

Original Poster
Rep: Reputation: 15
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
public class TokString
{

public static void main(String[] args)
{
try
{
String fileName="out" + System.currentTimeMillis()+".log";
Process P = Runtime.getRuntime().exec("getResource " + fileName );

int result = P.waitFor();
FileInputStream ins = new FileInputStream( fileName);
DataInputStream in = new DataInputStream(ins);

while( in.available() != 0 )
{
System.out.println(in.readLine());
}
}
catch (IOException e)
{
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}

I know my Proccess call work, because it was able to read the data out of the file. The problem here is, the getResource doesn't echo values of my array to the destined file; it echos everything else. When I open the output file, I only see regular string like "hello", but not the values of array.
If I was to run getResource directly w/o going through java, then it works fine.
 
Old 09-27-2005, 10:43 PM   #5
kjordan
Member
 
Registered: Jul 2004
Distribution: LFS, I felt the itch and scratched it
Posts: 227

Rep: Reputation: 31
Are you taking into account that your script writes it out to /tmp/$i and your java program is reading from the current directory for the file that was output? It seems to work for me if I make the script write to the current directory.
 
Old 09-27-2005, 10:49 PM   #6
happy78
Member
 
Registered: Aug 2003
Posts: 50

Original Poster
Rep: Reputation: 15
the script will send its output to a file
my java program will read that output

some how the output is missing the values of array. I suspect that, the command "top -n 1" within the script return output to a different console, that's why my array didn't get the values back from "top -n 1". And that's why no values of the array were sent to output fie
 
Old 09-27-2005, 11:06 PM   #7
kjordan
Member
 
Registered: Jul 2004
Distribution: LFS, I felt the itch and scratched it
Posts: 227

Rep: Reputation: 31
Try "top -b -n 1" to make top run in batch mode.

Quote:
-b : Batch mode operation
Starts top in 'Batch mode', which could be useful for sending out-
put from top to other programs or to a file. In this mode, top
will not accept input and runs until the iterations limit you've
set with the '-n' command-line option or until killed.
It seems to run correctly with that. It should also produce cleaner output since it doesn't output any ASCII color codes.

Last edited by kjordan; 09-27-2005 at 11:11 PM.
 
Old 09-27-2005, 11:17 PM   #8
happy78
Member
 
Registered: Aug 2003
Posts: 50

Original Poster
Rep: Reputation: 15
Thumbs up

IT'S THE ONE.....THANK YOU VERY MUCH..!!!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Linker problem: can't find a file, but the file exists atlep Programming 5 08-16-2004 06:15 AM
Echoing the first and last colums of a textfile ganninu Linux - General 4 06-26-2004 05:54 AM
Kernel echoing messages on VC (unwanted behavior) lhoff Mandriva 3 12-05-2003 03:28 PM
Script file problem (or operator problem) HELP! lostone Programming 2 05-23-2001 02:30 PM
Script file problem (or operator problem) HELP! lostone Linux - Newbie 0 05-15-2001 07:59 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:28 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration