Bash - Prepare command in cli without executing it - bash

I'm working on a bash script that lets the user configure a number of settings (like for example the current driver or list of drivers, the current release, the current test suite, the current compiler or list of compilers, etc) and which works with a number of tools (compilers, misra analyzer, test report creator, etc). The script defines some aliases like for example: compile, test, misra, report, etc. When such an alias is run by the user, the script prepares about 1-3 sample commands that can be run with that tool and echoes the sample commands on the screen like for example:
s1 = misra current_driver current_compiler
s2 = misra current_drivers_list current_compiler
s3 = misra current_drivers_list current_compilers_list
The user looks at the samples, chooses the most appropriate, types s1, s2 or s3
and hits Enter. The script adds the selected command in the history, as top command. The user presses 'up arrow' and has the command ready to be edited if needed and executed when everything is ok, at a hit of Enter key.
Now, I want to speed up the process of selecting the command, by getting rid of that 'up arrow key' press. What I would like to accomplish is: when user types s1, s2 or s3 and hits Enter, the corresponding command is added in the command line, without being executed. The thing is that I want to obtain the same behavior like the command would have been typed by the user, meaning the user should still be able to edit it, before launching it by pressing Enter.
Is there any method I can use to implement this behavior ?
Thank you

Related

LabVIEW: how to control two events one after the other from one button

I am working on a project where I have to send arguments by a command line to a python file (using system exec) and then visualize the results saved in a folder after the python file finishes executing. I need to have this by only clicking on one button, so my question is, if there is any way to realize this scenario or maybe if I can order the events.
Now I have included the flat sequence structure to the block Diagram so I can order the events, but I had an issue with making the program (the python file) running every time I press the Test button (it only runs in the first time I click on the Test button), I tried to use the while loop but I couldn't execute it again unless I restart the program.
The way you phrased your question makes me think that you want to wait until the command you call via system exec finished and then run some code. You could simply use a sequence structure for this.
However, if you need to do this asynchronously, i.e. launch the command and get an event when the command finished so you can draw the results, you will need to resort to asynchronous techniques like "Start Asynchronous Call" and "Wait On Asynchronous Call", or for example queues and a separate code area for the background-work.
Use "wait until completion?" input of System Exec function to make sure the script finished execution, then proceed with the results visualization part.

How to properly write an interactive shell program which can exploit bash's autocompletion mechanism

(Please, help me adjust title and tags.)
When I run connmanctl I get a different prompt,
enrico:~$ connmanctl
connmanctl>
and different commands are available, like services, technologies, connect, ...
I'd like to know how this thing works.
I know that, in general, changing the prompt can be just a matter of changing the variable PS1. However this thing alone (read "the command connmanctl changes PS1 and returns) wouldn't have any effect at all on the functionalities of the commands line (I would still be in the same bash process).
Indeed, the fact that the available commands are changed, looks to me like the proof that connmanctl is running all the time the prompt is connmanctl>, and that, upon running connmanctl, a while loop is entered with a read statement in it, followed by a bunch of commands which process the the input.
In this latter scenario that I imagine, there's not even need to change PS1, as the connmanctl> line could simply be obtained by echo -n "connmanctl> ".
The reason behind this curiosity is that I'm trying to write a wrapper to connmanctl. I've already written it, and it works as intended, except that I don't know how to properly setup the autocompletion feature, and I think that in order to do so I first need to understand what is the right way to write an interactive shell script.

Ruby shell : how to stop a REPL copy-paste from executing?

I have some quite long script that I want users to slightly adapt to their scenario before they can copy paste in in some IRB or pry shell.
Most people would, unless paying attention, blindly copy-paste the script in some console. I'd like to stop parsing/executing the rest of the code and (for example) require a user to explicitely validate that he wants to continue running the script (for example via a simple gets that force the user to type enter)
I was thinking of something like (imagine this is the code of a wiki that one would copy paste)
# Start script
[...] # Some stuff
# Copy paste the autorization code you receive from the internet in the `token` variable
string = ("Do not blindly copy-paste, Did you do what was written in the command just above ? We need the token in the `token` variable. If you did it, just press enter. Otherwise abort with CTRL+C and restart the script".purple)
puts string
gets
Service.call(token)
The problem is that the gets/coloration seems to break further execution of the copy pasted script.
My question is, assuming you have a chunk of code that you copy paste using REPL, is there a way to abort/pause the automatic Read-Evaluate print loop via some code contained in the chunk that is copied ?
Basically I'm writing some test procedure that testers can just copy paste in their terminal to check a feature is working. 99% of the time, is is enough to just set some variables and copy-paste the code provided in the examples. But sometimes, there are some (optional) manual steps to be done in the middle of the execution of the code that is copy pasted (like copying a token, retrieving the automatically generated ID, etc.)

Script Command -- Extra space and return added to long input

I am creating a browser-based application for interacting with my local terminal, and I'm using the script command in the terminal (actually in a Java process) to feed input and capture output to display in the browser.
I have found that the virtual terminal inside the script process has 80 columns by default, and when a line of input exceeds that number, I see unexpected behavior. When it reaches the length limit, it adds a space, then a return, then continues with the line. For example, if I input the following:
user_name ~/my/current/directory$ ls /some/really/long/path/to/some/directory/somewhere
The following is what is actually transcribed (and forwarded to my browser app):
user_name ~/my/current/directory$ ls /some/really/long/path/to/some/directory/so ^Mmewhere
Notice the so ^Mmewhere instead of somewhere.
When I hit the up arrow after that to retrieve the last input, I get something different:
user_name ~/my/current/directory$ ls /some/really/long/path/to/some/directory/som^Mmewhere
This time, instead of the extra space character, it's duplicating the m: som^Mmewhere.
What is going on here? Is it that the script command is really intended to produce text output for a human reader, and therefore favors visual formatting over executability? Is there an alternative to script that would work better for my purpose?
Edit
I should have mentioned that I'm doing this in macOS Sierra. Not sure if that makes a difference.

Sending information to a prompt in a shell

I use a CLI that starts a prompt in my shell. However, I usually end up typing the same commands at the prompt. Is there a way to manually automate the starting of the CLI and then send input in one go?
Here is an example: I type spotify to start my music client.
I see:
Type help to see commands.
> list
These are your playlists
1. ...
2. ...
(some lines omitted) where ">" is a prompt I am typing at. I want my script to type list for me then play and then quit and then start a script which plays my iTunes library. How could I go about doing this?
Expect is what I was looking for. It's a UNIX testing and automation tool for exactly this purpose.
You could implement "macro recording". For example, if you type "startrec", the CLI could save all subsequent commands, e.g. "list", "play" and "quit". The you type "stoprec spotify", which stores the command list under the title "spotify". next time when you type "spotify" your CLI could find it in the macro list, and replay the command sequence.

Resources