0

I would like to do something if the output of a shell script contains the string "Caddy 2 serving static files on :2015". This is what I have so far but the beach ball is just spinning. It seems it is because of the last command in my bash script which starts a server. There is no exit in the bash script so it does not end. The output of the bash script is correct and I can see "Caddy 2 serving static files on :2015". But it seems this code is only working with a bash script which has a end.

If someone wants to try, here you can download the executable caddy file which I am using in my bash script: https://caddyserver.com/download

- (IBAction)localHost:(id)sender
{
    // execute bash script to start server
    NSTask *task = [[NSTask alloc] init];
    [task setLaunchPath:@"/bin/bash"];
    [task setArguments:[NSArray arrayWithObjects:[[NSBundle mainBundle] pathForResource:@"caddy-start" ofType:@""], nil]];
    
    NSPipe *pipe = [NSPipe pipe];
    [task setStandardOutput: pipe];
    NSFileHandle *file = [pipe fileHandleForReading];
    
    [task launch];
    
    NSData *data = [file readDataToEndOfFile];
    NSString *result = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
    
    NSLog(result);
    
    NSString *string = result;
    NSString *substring = @"Caddy 2 serving static files on :2015";
    if ([string rangeOfString:substring].location != NSNotFound) {
        NSLog(@"Yes it does contain that substring");
    }
    else if ([string rangeOfString:substring].location == NSNotFound) {
        NSLog(@"No it does NOT contain that substring");
    }
}

And here is my bash script:

#!/bin/bash
DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
cd "${DIR}"

pwd

#open http://localhost:2015

# kill server if already running
kill -9 $(lsof -ti:2015)
(./caddy_darwin_amd64 stop) &
(./caddy_darwin_amd64 file-server --listen :2015 --root Content/) &
aronsommer
  • 123
  • 1
  • 11
  • Have you tried stepping through and determining where the code is stalling? I tried your code with the bash script of just `echo Caddy 2 serving static files on :2015` and removing the loadPage call and it outputs "Yes it does contain that substring" – R4N Feb 22 '22 at 23:02
  • I think the problem is between `[task launch];` and `NSLog(result);`. I can see the output but it never logs the result. The bash script does start a server and it has no exit. I have tried to run the commands in the bash script in the background like this `(command) &` but it does not help. – aronsommer Feb 23 '22 at 07:58
  • I have posted my bash script above. It seems the problem is the last command of my bash script. It does never end because its starting a server. – aronsommer Feb 23 '22 at 08:13
  • What's the output of `NSLog(result);`? I'm not familiar with these bash command, but is it async? Does it appear? What does `loadPage:`? – Larme Feb 23 '22 at 11:53
  • "Caddy 2 serving static files on :2015" does appear in the output of the bash script. The last command in bash script does start a server and there is no exit. NSLog does not log the result because the script is stuck after task launch. I think its because there is no exit in the bash script so there is no end. The stuff after wait 3 seconds is not relevant. – aronsommer Feb 23 '22 at 13:29
  • You might be interested in https://stackoverflow.com/questions/9965360/async-execution-of-shell-command-not-working-properly then? – Larme Feb 24 '22 at 08:28

0 Answers0