Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7923365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T17:17:58+00:00 2026-06-03T17:17:58+00:00

The following is a short function to return a file size on a Linux

  • 0

The following is a short function to return a file size on a Linux system, run in the rhino shell:

function fsize(file){
    var filesize = runCommand("stat","-c %s",file);
    return filesize;
} 

Running the function returns a value; e.g:

fsize('/etc/hosts'); 

returns a file size in bytes

But if I run:

var filesize = fsize(testfile);

the var filesize is "0" when output to the console.

Why is this happening, and how can it be fixed?

To examine variables, I used the function:

function output(strings){
    print(strings);
}

A sample shell session, showing output:

js> var file = "/var/www/javascript/ProgressMon/progressmon.js"
js> fsize(file);
683
0
js> var filesize = fsize(file);
683
js> filesize;
0
js> output(filesize);
0
js> 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T17:18:02+00:00Added an answer on June 3, 2026 at 5:18 pm

    Examining the runCommand documentation, it can be called in the following forms:

    runCommand(command);
    runCommand(command, arg1, ..., argN);
    runCommand(command, arg1, ..., argN, options);
    

    The sample uses the second form, which prints the output to the terminal but does not capture it in any way that’s available to code. In other words, fsize(testfile) does not return the file size, it prints it.

    The result returned by all forms is the exit status of the command, which is what gets assigned to filesize.

    To capture output, you must use the third form and pass an object with an output property, which can be an java.io.OutputStream or a string. In this case, you probably want the latter, as that will cause program output to be appended to the property. The function can then call parseInt on the output to get the size as a number, rather than a string.

    The system call might generate errors. To handle them within fsize, you could print error messages and return a negative value to indicate an error. If runCommand might throw an exception, the code could be wrapped in a try-catch block.

    function fsize(file){
        var options = {
            output: '',
        };
        try {
            var result = runCommand("stat", "-c", "%s", file, options);
            if (result) {
                print(options.err);
                return -1;
            } else {
                return parseInt(options.output);
            }
        } catch (err) {
            print(err);
            return -1;      
        }
    } 
    

    Alternatively, you could let code up the call-stack handle exceptions, and raise an exception for any error generated by the runCommand call .

    function fsize(file){
        var options = {
            output: '',
            err: '',
        };
        if (runCommand("stat", "-c", "%s", file, options)) {
            // note: `SystemError` must be defined, as it's not a standard class
            throw new SystemError(options.err);
        }
        return parseInt(options.output);
    } 
    

    Note that instead of calling output(filesize); to print the value of filesize, you can evaluate it directly:

    js> var filesize = fsize(file);
    js> filesize
    683
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following short version of a tsql if else if.. IF @var
The following function writes a struct to a file. #define PAGESIZE sizeof(BTPAGE) #define HEADERSIZE
I need to sort string, and I came up with the following function. def
Consider the following short program: #include <type_traits> #include <iostream> using namespace std; template <typename
I have been following a short tutorial to build a tab menu on my
Following problem: I want to render a news stream of short messages based on
In short, currently I am using the following code to pull records from multiple
In the short version of postgres' installing it tell me to do the following
Here's the following algorithm: int encryption(int a, int b) { short int c, c2;
I have the following program #include <stdio.h> int main(void) { unsigned short int length

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.