I
I
Ivan Vasilich2017-07-03 11:00:38
terminal emulator
Ivan Vasilich, 2017-07-03 11:00:38

How to execute terminal commands in swift?

Good day/evening to all
I want to write a program in swift for Linux for easy use of projects. There was a problem, when I specify a new project, it was created in the project folder,
in the terminal you can create a project with the command
swift package init --type executable
and generate an xcode project for further use in xcode, you can use this command
swift package generate-xcodeproj
I have a TerminalTools class that is responsible for executing, reading from the terminal
for example I try run these two commands using the method below and it gives me an error

func shell(launchPath: String, arguments: [String]) -> String {

        let process = Process()
        process.launchPath = launchPath
        process.arguments = arguments

        let pipe = Pipe()
        process.standardOutput = pipe
        process.launch()

        let output_from_command = String(data: pipe.fileHandleForReading.readDataToEndOfFile(), encoding: String.Encoding.utf8)!

        // remove the trailing new-line char
        if output_from_command.characters.count > 0 {
            let lastIndex = output_from_command.index(before: output_from_command.endIndex)
            return output_from_command[output_from_command.startIndex ..< lastIndex]
        }
        return output_from_command
    }

creating project
fatal error: POSIX command failed with error: 2: file Foundation/Process.swift, line 473
Illegal instruction (core dumped)

searched on the Internet tried to use other methods always gives such an error,
I can’t understand why this can be something with privileges ??

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question