如何检查 bash 脚本的输出是否包含 Objective-C 中的某个字符串?

发布于 2025-01-09 01:13:34 字数 1705 浏览 5 评论 0原文

如果 shell 脚本的输出包含字符串“Caddy 2serving static files on :2015”,我想做一些事情。这是我到目前为止所拥有的,但沙滩球只是在旋转。看来这是因为我的 bash 脚本中启动服务器的最后一个命令。 bash 脚本中没有退出,因此它不会结束。 bash 脚本的输出是正确的,我可以看到“Caddy 2 在 :2015 上提供静态文件”。但这段代码似乎只适用于有结束的 bash 脚本。

如果有人想尝试,您可以在这里下载我在 bash 脚本中使用的可执行 caddy 文件: 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");
    }
}

这是我的 bash 脚本:

#!/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/) &

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/) &

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文