0

I have installed lima (https://github.com/lima-vm/lima) on my macos. I would like to create script like:

  • enter to lima environment by lima command.
  • execute there custom command eg docker ps --all -q

I tried something like this:

 bash -c "lima; docker ps --all -q"

but it works that I entered into lima, and after type exit I see docker ps --all -q output. So the second command is not executed into lima context.

How can I write proper script?

  • 1
    If I understand the documentation of `limactl shell`, it should be `lima docker ps --all -q` without the `;`. Your command executed `docker` in the local shell, not inside lima. – Barmar Aug 31 '23 at 16:39

1 Answers1

0

The command

bash -c "lima; docker ps --all -q"

starts bash with two commands. You want to start bash with one command and the command should contain arguments. What you want is

bash -c "lima docker ps --all -q"
Thomas Sablik
  • 16,127
  • 7
  • 34
  • 62
  • ...but I wonder what the point of `bash -c` is at all here. There has to be some kind of shell parsing the `bash -c` command; why not just let that shell start the `lima sh -c` command directly, without bash in the way? – Charles Duffy Aug 31 '23 at 16:44
  • @CharlesDuffy I don't know why the OP uses `bash -c`, but if there is a reason I should also use it in my answer. – Thomas Sablik Aug 31 '23 at 16:45
  • They list their requirements as bullet points; using `bash -c` isn't among those requirements, it's just a thing they "tried something like". – Charles Duffy Aug 31 '23 at 16:46
  • @CharlesDuffy It's part of a script. If it's a shell (sh) script, it could be necessary to start bash. Some environments use sh as default and a common way to choose a different shell is to start commands with `bash -c`. – Thomas Sablik Aug 31 '23 at 16:48
  • It's a pretty serious stretch; I can think of reasons to start bash from sh, but they're obscure corner cases (like if you're calling an exported shell function rather than executable), but lima _doesn't provide_ such a shell function. Other corner cases applicable to noninteractive shells get even more niche, like needing `BASH_ENV` rather than `ENV` to be sourced to run command-specific setup; but I don't think I've seen anyone deliberately use BASH_ENV that way in years, and the OP would need to know enough about the shell to not ask this question to even be _trying_ to do any of that. – Charles Duffy Aug 31 '23 at 16:53
  • It would be different if the OP were running a command that used bash-only features -- process substitutions, etc -- but we _see_ the command they're using, and it's all perfectly POSIX-compliant. And because the bash part of the script is enclosed in single quotes, the interpreter selection for this `lima` execution makes no difference to later parts of the script, which run with the original shell; nor to hypothetical shell commands run by lima, which run in their own subprocesses with independently-selected interpreters. – Charles Duffy Aug 31 '23 at 16:56
  • @CharlesDuffy Okay, thank you for your opinion. You were free to write your own, perhaps better answer, but you've chosen to close this question as a duplicate with a useless dupe target (it doesn't describe how to call lima with arguments). I try to answer questions as near as possible to the original code. I try to only change the parts that are asked for. I would not remove parts of commands in my answers just because I have a different opinion or I think they are not necessary. That's something I would post in a comment below the question. – Thomas Sablik Aug 31 '23 at 17:04
  • Thanks a lot. This works. I cannot believed that I didn't try command without `;`. – Szyn33k Aug 31 '23 at 18:02
  • @ThomasSablik, the duplicate isn't useless: it describes how to feed commands to run within lima on stdin, _which is an equally effective way to solve the OP's problem_. (Well, better and worse: Better insofar as it can be used to run more commands inside a single lima invocation, worse because you're using an invocation mode with more moving parts, starting a shell inside the VM and feeding commands to the shell, rather than giving it one thing to run). – Charles Duffy Sep 01 '23 at 00:56