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

  • Home
  • SEARCH
  • 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 7819905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:06:27+00:00 2026-06-02T07:06:27+00:00

I’m trying to use pipes to handle a command that requires multiple inputs, but

  • 0

I’m trying to use pipes to handle a command that requires multiple inputs, but not quite sure how to do it. Here’s a snippet of what I’m trying to do. I know how to handle the first input, but I’m at lost as to piping in the second input -newstdinpass

NSTask *task = [[NSTask alloc] init];
NSPipe *pipe = [NSPipe pipe];

[task setLaunchPath: @"/bin/sh"];
[task setArguments: [NSArray arrayWithObjects: @"-c", @"/usr/bin/hdiutil chpass -oldstdinpass -newstdinpass /path/to/dmg", nil]];
[task setStandardInput:pipe];
[task launch];

[[pipe fileHandleForWriting] writeData:[@"thepassword" dataUsingEncoding:NSUTF8StringEncoding]];
[[pipe fileHandleForWriting] closeFile];

[task waitUntilExit];
[task release];

So I know using hdiutil in this manner is a bit of a hack, but in terms of pipes, am I going about it the correct way?

Thanks.

UPDATE: In case others are wondering about this, a quick solution to my problem is to pass in a null-terminated string as Ken Thomases had pointed out below. Use [[NSString stringWithFormat:@"oldpass\0newpass\0"] dataUsingEncoding:NSUTF8StringEncoding] into the pipe. Now, still need to learn how to bridge multiple NSTasks with pipes…

  • 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-02T07:06:28+00:00Added an answer on June 2, 2026 at 7:06 am

    You can create multiple NSTasks and a bunch of NSPipes and hook them together, or you can use the sh -c trick to feed a shell a command, and let it parse it and set up all the IPC.

    Example :

    NSTask *task;
    task = [[NSTask alloc] init];
    [task setLaunchPath: @"/bin/sh"];
    
    NSArray *arguments;
    arguments = [NSArray arrayWithObjects: @"-c",
                         @"cat /usr/share/dict/words | grep -i ham | rev | tail -5", nil];
    [task setArguments: arguments];
    // and then do all the other jazz for running an NSTask.
    

    Reference : http://borkware.com/quickies/one?topic=nstask


    Now, as for a “proper” command execution function, here’s one I’m usually using…

    Code :

    /*******************************************************
     *
     * MAIN ROUTINE
     *
     *******************************************************/
    
    - (void)runCommand:(NSString *)cmd withArgs:(NSArray *)argsArray
    {
        //-------------------------------
        // Set up Task
        //-------------------------------
    
        if (task) { [self terminate]; }
    
        task = [[NSTask alloc] init];
        [task setLaunchPath:cmd];
        [task setArguments:argsArray];
    
        [task setStandardOutput:[NSPipe pipe]];
        [task setStandardError:[task standardOutput]];
    
        //-------------------------------
        // Set up Observers
        //-------------------------------
    
        [PP_NOTIFIER removeObserver:self];
        [PP_NOTIFIER addObserver:self 
                        selector:@selector(commandSentData:) 
                            name: NSFileHandleReadCompletionNotification 
                          object: [[task standardOutput] fileHandleForReading]];
    
        [PP_NOTIFIER addObserver:self 
                        selector:@selector(taskTerminated:) 
                            name:NSTaskDidTerminateNotification 
                          object:nil];
    
        //-------------------------------
        // Launch
        //-------------------------------
        [[[task standardOutput] fileHandleForReading] readInBackgroundAndNotify];
    
        [task launch];
    }
    
    /*******************************************************
     *
     * OBSERVERS
     *
     *******************************************************/
    
    - (void)commandSentData:(NSNotification*)n
    {
        NSData* d;
        d = [[n userInfo] valueForKey:NSFileHandleNotificationDataItem];
    
        if ([d length])
        {
            NSString* s = [[NSString alloc] initWithData:d encoding:NSUTF8StringEncoding];
    
            NSLog(@"Received : %@",s);
        }
    
        [[n object] readInBackgroundAndNotify]; 
    }
    
    - (void)taskTerminated:(NSNotification*)n
    {
        [task release];
        task = nil;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I need a function that will clean a strings' special characters. I do NOT
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.