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 7587473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:41:01+00:00 2026-05-30T19:41:01+00:00

I am trying to run a simple shell command that runs and returns text

  • 0

I am trying to run a simple shell command that runs and returns text rather quickly inside of a loop of indeterminate size at compile time that is generated by an NSArray. In scripting languages like perl, I would be able to do something like this:

for(i=0;i<=$myinputarraysize;i++){
    $output[i]=`/my/task $inputarray[i]`;
}

This would build a new array for me from the expected output of my task. In Obj-C this seems to be much more difficult and a bit confusing to me. Right now my loop looks like this:

for(int i=0; i<[inputarray count]; i++){
    NSTask *task;
    task = [[NSTask alloc] init];
    [task setLaunchPath:nsdchat];

    NSArray *args;
    args = [NSArray arrayWithObjects:@"/my/task", [inputarray objectAtIndex:i], nil];
    [task setArguments:args];

    NSPipe *pipe;
    pipe = [NSPipe pipe];
    [task setStandardOutput:pipe];

    NSFileHandle *file;
    file = [pipe fileHandleForReading];

    [task launch];

    NSData *data;
    data = [file readDataToEndOfFile];

    NSString *desc;
    desc = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];    
    desc = [string stringByReplacingOccurrencesOfString:@"\n" withString:@""];

    [descriptions insertObject:desc atIndex:i];

    [task release];
    [args release];
    [pipe release];
    [file release];
    [data release]; 
}

My goal is to fill descriptions (an NSMutableArray) with the output from my task (which I know is always a string and always ends in a newline that I want to strip out). It seems I’m missing something about memory releasing as when I run this and NSLog the output, I get the same result for the entire count of the loop.

Is there any easier or more way to loop through simple tasks like this? Am I overcomplicating it for myself?

  • 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-05-30T19:41:02+00:00Added an answer on May 30, 2026 at 7:41 pm

    First of all, there’s no need to do most of that work inside the loop. task, pipe and file all look like they could be handled outside the loop. You should also consider using Objective-C 2.0 dot syntax and fast enumeration to cut things down a little.

    More significantly:

    NSArray *args;
    args = [NSArray arrayWithObjects:@"/my/task", [inputarray objectAtIndex:i], nil];
    [task setArguments:args];
    

    This says that the first argument passed to the executable at path ndschat is /my/task. Which doesn’t seem to match your PERL usage. Probably you want just:

    NSArray *args;
    args = [NSArray arrayWithObject:[inputarray objectAtIndex:i]];
    [task setArguments:args];
    

    Or, with the style comments taken into account:

    for(NSString *argument in inputarray)
    {
        ...
        task.arguments = [NSArray arrayWithObject:argument];
        ...
    }
    

    EDIT: you’re also releasing a lot of objects you don’t own, which as well as adding heft to your code is a memory management error possibly leading to a crash. So, to cut the whole thing down and correct that fault:

    for(NSString *argument in inputarray)
    {
        NSTask *task = [[NSTask alloc] init];  // you now own this
        task.launchPath = nsdchat;
    
        NSPipe *pipe = [NSPipe pipe];          // you don't own this
        task.standardOutput = pipe;
    
        NSFileHandle *file = [pipe fileHandleForReading];  // you also don't own this
    
        task.arguments = [NSArray arrayWithObject:argument];
        [task launch];
        [task waitUntilExit]; // you should wait until the task is done
    
        NSData *data = [file readDataToEndOfFile];  // this is another thing
                                                    // you don't own. Note also that
                                                    // readDataToEndOfFile advances
                                                    // the current read pointer, so
                                                    // it should be fine to do this
                                                    // successively 
    
        NSString *desc;
        desc = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] 
                                                                       autorelease];    
                                                    // you don't own this because
                                                    // of the autorelease; you
                                                    // don't want to own it since
                                                    // the next line will throw
                                                    // it away
    
    
        desc = [string stringByReplacingOccurrencesOfString:@"\n" withString:@""];
                                                    // you don't own this
    
        [descriptions addObject:desc];
        [task release];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to run this dreadfully simple command in Bash java -cp nasa-top-secret.jar
I am trying to write a simple shell-script that prints out the first parameter
I'm trying to write a VBScript (.vbs) script that uses the WScript.Shell Run() method,
I am trying to run simple shell commands in my script, but am unable
I am trying to run a simple multiple processes application in Python. The main
I am trying to run a simple SQLITE application on Windows Mobile developed with
I'm trying to run a simple query with $this->db in Kohana, but am running
I am using Python 2.6 and am trying to run a simple random number
I am new to Tomcat, and trying to run a simple HelloWorld servlet. I
I'm trying to run a c++ 2d array (pretty simple file) and it works

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.