How to resolve the algorithm Execute a system command step by step in the FutureBasic programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Execute a system command step by step in the FutureBasic programming language

Table of Contents

Problem Statement

Run either the   ls   system command   (dir   on Windows),   or the   pause   system command.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Execute a system command step by step in the FutureBasic programming language

Source code in the futurebasic programming language

print unix(@"ls -A")

local fn DoUnixCommand( cmd as str255 )
  str255 s

  open "Unix", 2, cmd
  while ( not eof(2) )
    line input #2, s
    print s
  wend
  close 2
end fn

fn DoUnixCommand( "ls -A" )

include "NSLog.incl"

// For remote uses like curl
// #plist NSAppTransportSecurity @{NSAllowsArbitraryLoads:YES}

local fn RunTerminalCommand( cmd as CFStringRef ) as CFStringRef
————————————————————————————————————————————————————————————————————————————————————————————————————
  ErrorRef          err = NULL
  CFStringRef outputStr = NULL
  
  TaskRef task = fn TaskInit
  TaskSetExecutableURL( task, fn URLFileURLWithPath( @"/bin/zsh" ) )
  CFStringRef cmdStr = fn StringWithFormat( @"%@", cmd )
  CFArrayRef    args = fn ArrayWithObjects( @"-c", cmdStr, NULL )
  TaskSetArguments( task, args )
  
  PipeRef p = fn PipeInit
  TaskSetStandardOutput( task, p )
  TaskSetStandardError(  task, p )
  FileHandleRef fh = fn PipeFileHandleForReading( p )
  
  fn TaskLaunch( task, NULL )
  TaskWaitUntilExit( task )
  
  CFDataRef dta = fn FileHandleReadDataToEndOfFile( fh, @err )
  if err then NSLog( @"Error reading file: %@", fn ErrorLocalizedDescription( err ) ) : exit fn
  
  fn FileHandleClose( fh, @err )
  if err then NSLog( @"Error closing file: %@", fn ErrorLocalizedDescription( err ) ) : exit fn
  
  outputStr = fn StringWithData( dta, NSUTF8StringEncoding )
end fn = outputStr

CFStringRef cmd

cmd = @"cal 2023"
NSLog( @"%@", fn RunTerminalCommand( cmd ) )

HandleEvents

  

You may also check:How to resolve the algorithm Generate lower case ASCII alphabet step by step in the Snobol programming language
You may also check:How to resolve the algorithm Truncatable primes step by step in the jq programming language
You may also check:How to resolve the algorithm Repeat step by step in the Stata programming language
You may also check:How to resolve the algorithm 21 game step by step in the uBasic/4tH programming language
You may also check:How to resolve the algorithm Reverse a string step by step in the Objective-C programming language