I am using Wolfram script with a Mac OS. I have a long while loop dependent on the length of a list. i.e.:

myList = {}
While[ Length[myList] < 100, myList = AppendTo[myList, 0]]

How do I get a progress bar to appear in my terminal during this while loop?

  • $\begingroup$ That code does not make sense because myList is not defined, While does not have a test and a body separated by coma and if you put the coma that loop is infinite cause you are not increasing the length of myList. $\endgroup$ – Fortsaint Jan 26 '20 at 14:36
  • $\begingroup$ It's an example, the point of the question is about the progress bar. "Corrected code": myList = {}; While[ Length[myList] < 100, myList += {0,0}] $\endgroup$ – PHPHacky Jan 26 '20 at 18:01
  • $\begingroup$ Still an infinite loop. $\endgroup$ – Fortsaint Jan 26 '20 at 18:39

This solution is specific to Linux


While[ k<100, 

       k++; Pause@.1; 

      (* progress indicator *) 
       Run[ "echo -n '" <> 
            StringRepeat["|",k] <>  
            StringRepeat["-", 100-k] <> 

but you might find a command line that overwrites previous outputs like echo -n ' \r' for other systems too.

  • $\begingroup$ hmm wolfram script doesn't seem to understand the return carriage. It prints on a new line every iteration. BTW I'm using a UNIX system so it should work $\endgroup$ – PHPHacky Jan 26 '20 at 21:27
  • $\begingroup$ It is not wolfram that do not understand the return carriage, it is the shell of your operating system. As I wrote in the answer, you need to find command line that works for your OS. $\endgroup$ – Fortsaint Jan 28 '20 at 14:48
  • $\begingroup$ Hmm using the above code and substituting the Run command for a WriteString command I get the expected output. This solution works for bash shells on Linux OS $\endgroup$ – PHPHacky Jan 29 '20 at 15:19
  • $\begingroup$ @PHPHacky Great. I am on Debian 9 and only Run gives me the expected behaviour, not WriteString, What OS is yours? $\endgroup$ – Fortsaint Jan 29 '20 at 15:25
  • $\begingroup$ macOS (which is Unix based) but I tested this on Linux as well. What shell are you using? $\endgroup$ – PHPHacky Jan 29 '20 at 17:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.