2
$\begingroup$

Why isn't the following linux command working from the RunProcess interface? I want to change the terminal size programmatically and then display the information:

commands[sequence__]:=StringJoin@Riffle[{sequence}," && "];
bashMode=commands[
    "echo Status for device CON:",
    "echo ----------------------",
    "echo '\tTerminal Type: $(tput longname)'",
    "echo '\tLines: $(tput lines)'",
    "echo '\tColumns: $(tput cols)'",
    "echo '\tColors: $(tput colors)'"
];
RunProcess[{"bash","-c",commands[
    "printf '\\e[8;24;90t'",
    bashMode
]}]

Since I don't know linux very well. If there is a way to join these commands into one, feel free to suggest that as well!

By the way I am on Windows and accessing bash through the Windows Subsystem for Linux.


I think the problem here might be ASCII control character ESC needed in the printf command to control the terminal: printf '\e[8;24;90t'. Since running this directly works but for some reason mathematica is unable to send this character to the bash correctly.

See: Bug report about escape characters not going through bash

$\endgroup$
  • $\begingroup$ Does that even open up a terminal? I thought it just ran the command and gave you the output $\endgroup$ – user6014 Aug 28 '19 at 12:57
  • 1
    $\begingroup$ RunProcess[{"bash", "-c", "echo \"lines\\ncols\"|tput -S"}] might give some insight as to the output you can expect here. $\endgroup$ – user6014 Aug 28 '19 at 13:01
  • $\begingroup$ @user6014 Terminal should exists since tput longname does return its name. Also you can change properties like color, etc. But it is hidden like when using WebExecute you can launch a browser in hidden mode. Under mathematica subprocesses in Task Manager you can see the Console Window Host while the RunProcess is running. $\endgroup$ – user13892 Aug 28 '19 at 13:17
  • $\begingroup$ From within a .bat file, the size of the command window can be set, for example, by the command mode con:cols=200 lines=35. $\endgroup$ – Andrew Norton Aug 31 '19 at 2:56
3
$\begingroup$

Update

It may be easier to just use the Run command. The following uses the terminfo.sh script that I described below. Because bash does not like carriage returns, \r, you must remove them when export the script. It also adds a sleep 4 command so the window stays opened for 4 seconds before closing.

SetDirectory[NotebookDirectory[]];
cmdstring = StringReplace[
   "#!/bin/bash

   echo Status for device CON:
   echo ----------------------
   echo \"    Terminal Type: $(tput longname)\"
   echo \"    Lines: $(tput lines)\"
   echo \"    Columns: $(tput cols)\"
   echo \"    Colors: $(tput colors)\"
   ", "\r\n" -> "\n"];
Export["terminfo.sh", cmdstring, "String"];
Run["cmd /c  \"MODE CON: COLS=50 LINES=8 && bash -c ./terminfo.sh; \
sleep 4 \""]

Console Animation

Original Partial Answer

Controlling the window size may be a little tricky since as you suggested in the comments that it uses Console Window Host. If you just want to see the default terminal properties, this seems to work on my Windows 10 system.

cmd = "echo Status for device CON:
  echo ----------------------
  echo \"    Terminal Type: $(tput longname)\"
  echo \"    Lines: $(tput lines)\"
  echo \"    Columns: $(tput cols)\"
  echo \"    Colors: $(tput colors)\"";
RunProcess[{"bash", "-c", cmd}, "StandardOutput"]
(* "Status for device CON:
----------------------
Terminal Type: xterm with 256 colors
Lines: 24
Columns: 80
Colors: 256
" *)

Alternatively, you could write a multiline bash script like and save it in terminfo.sh in the NotebookDirectory

#!/bin/bash echo Status for device CON: echo ---------------------- echo " Terminal Type: $(tput longname)" echo " Lines: $(tput lines)" echo " Columns: $(tput cols)" echo " Colors: $(tput colors)"

Now you can call the script from RunProcess like so

SetDirectory[NotebookDirectory[]];
RunProcess[{"cmd", "/c", "bash", "-c", "./terminfo.sh"}]
(* <|"ExitCode" -> 0, "StandardOutput" -> "Status for device CON:
  ----------------------
      Terminal Type: xterm with 256 colors
      Lines: 24
      Columns: 80
      Colors: 256
  ", "StandardError" -> ""|> *)

You may need to change the permissions on terminfo.sh to make it executable. The easiest way to do that is in the bash shell with chmod +x terminfo.sh.

$\endgroup$

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.