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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:56:18+00:00 2026-05-13T05:56:18+00:00

I would like to run a shell script, from a file or from an

  • 0

I would like to run a shell script, from a file or from an objective-c string (within the code). I would also like the shell script’s result to be stored to a variable. I would not like the shell script to be split into arguments (such as setLaunchPath when I run it). For example: running this shell script “mount_webdav idisk.mac.com/mac_username /Volumes/mac_username” instead of “/bin/mount_webdav” then the arguments. Is there anyway to do this? I am using NSTask right now, but it has caused me some errors when I try to put the arguments with it. Here is the posed code:

(some of the .m file)

 NSString *doshellscript(NSString *cmd_launch_path, NSString *first_cmd_pt) {

 NSTask *task = [[NSTask alloc] init]; // Make a new task

 [task setLaunchPath: cmd_launch_path]; // Tell which command we are running

 [task setArguments: [NSArray arrayWithObjects: first_cmd_pt, nil]];

 [task setArguments: first_cmd_pt];

 NSPipe *pipe = [NSPipe pipe];

 [task setStandardOutput: pipe];

 [task launch];

  NSData *data = [[pipe fileHandleForReading] readDataToEndOfFile];

  NSString *string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];

  [task release]; //Release the task into the world, thus destroying it.

  return string;
}


NSString *mount_idisk(NSString *mac_username) {

 doshellscript(@"/bin/mkdir", [@"/Volumes/" stringByAppendingString:mac_username]);

 NSString *path_tmp = [mac_username stringByAppendingString: @"/ /Volumes/"];

 NSString *idisk_path = [path_tmp stringByAppendingString:mac_username];

 //NSLog(@"%@", [@" http://idisk.mac.com/" stringByAppendingString: idisk_path]);

 NSString *finished_path = [@"http://idisk.mac.com/" stringByAppendingString: idisk_path];

 doshellscript(@"/sbin/mount_webdav", finished_path);
}

…
Here is the line I am using to run it:

mount_idisk("username");

  • 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-13T05:56:18+00:00Added an answer on May 13, 2026 at 5:56 am

    There is no way to pass a whole command line to NSTask.

    For good reason; doing so is rife with security holes if you have any kind of string composition going on. Your string composition code would have to be fully cognizant of all of the rules of parsing a shell command line and would have to escape every possible combination of characters that might lead to arbitrary command execution.

    The system() C API lets you execute arbitrary commands, but has no mechanism for capturing output directly. It would be easy to add something to your command line that spews the output into a temporary file that you later read, but doing so just adds more security holes above and beyond passing down a whole command line as a single string.

    Wait… Looks like you have a straightforward bug:

    [task setArguments: [NSArray arrayWithObjects: first_cmd_pt, nil]];
    [task setArguments: first_cmd_pt];
    

    Why are you setting and then re-setting the task’s arguments?

    Given that your mount_idisk() function is effectively composing the individual arguments and concatenating them together into a single string, why don’t you simply stuff all the args into an NSArray and modify doshellscript() to take an array as the second parameter; the array of arguments?


    You aren’t creating the array of arguments correctly.

    Namely:

    NSArray *finished_path = [NSArray arrayWithObjects:@"http://idisk.mac.com/", mac_username, @"/ /Volumes/", mac_username, nil];
    

    That line is creating an array contain 4 objects which are then treated as 4 separate arguments in the doshellscript() function and not the two arguments that you need.

    Maybe something like:

    NSString *mobileMeUserURL = [@"http://idisk.mac.com/" stringByAppendingString: mac_username];
    NSString *localMountPath = [ @"/ /Volumes/" stringByAppendingString:  mac_username];
    NSArray *arguments = [NSArray arrayWithObjects: mobileMeUserURL, localMountPath, nil];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 288k
  • Answers 288k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Your code as you've given it above works fine (tested).… May 13, 2026 at 5:25 pm
  • Editorial Team
    Editorial Team added an answer Have your button populate a hidden formfield with a timestamp… May 13, 2026 at 5:25 pm
  • Editorial Team
    Editorial Team added an answer Lots of them. This will work: sed -i -e 's/.*/START… May 13, 2026 at 5:24 pm

Related Questions

I am looking for a solution, or a set of solutions with the following
I maintain a number of load balanced web servers running Windows Server 2003. Today
I would like to execute a MySQL command in a shell script/cron job that
I have a shell script which I'd like to trigger from a J2EE web
Is there a simple way to run a Python script on Windows/Linux/OS X? On

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.