I have a small function which executes a command line tool like 7za. I don't know where this tool is installed by the user, so I use this function:
func execute(commandName: String, arguments: [String]) -> String? {
guard var bashCommand = execute(command: "/bin/bash" , arguments: ["-l", "-c", "which \(commandName)"]) else { return "\(commandName) not found" } // find path of command itself, e.g. "/usr/local/bin/7za"
bashCommand = bashCommand.trimmingCharacters(in: NSCharacterSet.whitespacesAndNewlines) // remove newline character at the end
if bashCommand == "" { return nil } // command not even found! => nil
return execute(command: bashCommand, arguments: arguments) // now we have: /usr/local/bin/7za e -pPW -o/Users/Shared/File.JPG.7z
}
On OSX 10.15 there is a new shell zsh. I can switch my command from bash to zsh, but my program should run on any system. How can I find out which command to use? I don't want to scan the entire harddisk to simply run an installed command line tool lile "7za". Any idea?