openlang

Running OS commands inside Pluto

Is there a way to invoke external operating system commands from inside Pluto to automate the fossil add fernuser/tiddlers/* action, leaving just the final commit to perform manually in the console?

The conversation continues 3

Follow the reasoning
  1. Permalink

    Hi! Yes, Julia can execute shell[1] (#footnote-731542-1) commands and so can a Pluto notebook, for example a cell with the following code executes Unix-like systems' directory-listing command ls:

    run(`ls`)

    It constructs an object with type Cmd representing the command, then executes it using run. The documentation for run and Cmd contains plenty of useful additional detail explaining how to execute commands in this way.

    Be aware that opening a Pluto notebook executes every cell, and changing a cell also re-executes the cells depending on it. Whether that is desirable for external shell commands depends on your situation, though you can avoid executing that “command cell” whenever one of its dependencies changes.

    PS: run returns the process result, meaning you generally see its output or error codes when the command fails.

    Actually these commands don't execute through a shell, as explained in my later reply. ↩︎ (#footnote-ref-731542-1)

  2. Permalink

    You're most welcome. I have just noticed that I left out a detail: you may need to take particular care when constructing a command object in Julia if it includes special characters (such as the * appearing in your example). Rather than invoking a shell, Julia executes the commands itself. Consequently * likely won't behave as you expect.

    For example, suppose you try to execute

    command = `fossil add fernuser/tiddlers/*`
    run(command)

    Julia objects first to the bare * (you'd have to spell it \*), but fixing that still doesn't give the desired result because matching * against the directory's filenames is “globbing”, provided by Bash and similar shells. run doesn't perform that step, see for instance a discussion of quoting commands (https://discourse.julialang.org/t/quoting-in-shell-commands/6781) together with associated discussions such as recursive directory removal (https://discourse.julialang.org/t/removing-directory-contents-recursively/128761).

    The most straightforward way of handling this is probably to explicitly execute that command through a shell, for instance using bash -c '...'

    command = `bash -c 'fossil add fernuser/tiddlers/*'`
    run(command)

    You should then get the expected behavior of that shell (assuming Bash). This does require Bash to exist on the machine, but adapting it if necessary is straightforward.

Other sides of the same question

  1. Using MATLAB through Google Colab

    Can MATLAB commnads be used from Google Colab (cloud-hosted Jupyter)? For instance, I'd like to invoke Risk Management Toolbo…

    Replies 2Latest activity matlab
  2. Can gdb set a breakpoint for an assembly instruction using its address?

    My attempt: apparently this command does not exist, though. Can gdb do something like this?

    Replies 2Latest activity assembly
  3. Can Playwright assertions allow either of two possible text values?

    I'd like to verify that the text contains either one or two: Can this be made to work in Playwright? Could the same thing als…

    Replies 2Latest activity typescript