0

I have the following shell script:

#!/bin/sh

cd $1

$1 is the path that I want to pass from the main.m which will call the script.

The script is located in the same directory as main.m.

How can I call a local script within the project?

Is it possible to compile the script into a binary so that when I execute the binary, it will also call the script?

Thanks

4
  • 1
    using chmod 755 script.sh you are making it executable and can either place it in you PATH or run `./script.sh' to run it. Note that a script cannot change the environment of the parent meaning that it will change the directory but when the script exits nothing has changed for the parent shell. Commented Nov 27, 2019 at 15:44
  • What is main.m written in ? Commented Nov 27, 2019 at 16:05
  • It's written in objective-c Commented Nov 27, 2019 at 16:54
  • Are you making an iOS app, MacOS app, or command line tool? From what it sounds like, you want to create a program of some sort which is a compiled binary and run the shell command. The answer becomes "it depends" (hence the q's). You local script is pretty simplistic. You may want to actually state what you are trying to do since depending on the scenario. Helps in getting you to a solution that works for you. Commented Nov 28, 2019 at 3:43

1 Answer 1

1

I assume you are trying to run an embedded script file in macOS app or command line tool mode, you need wrap the script file with NSTask to implement it.

NSTask *task = [[NSTask alloc] init];
task.launchPath = @"/bin/bash";
task.arguments = @[@"/path/to/your_script_file"];
[task launch];

// Specify the stdout and stderr to pipe if necessary.
NSPipe *pipe = [NSPipe pipe];
[task setStandardOutput:pipe];
[task setStandardError:pipe];

Note that NSTask is available in macOS only.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.