KornShell (ksh): info message under read input - ksh

Using KornShell, I want to ask the user for an input.
The goal will be to make an info message, which appear under the input request.
My script so far:
info_message="test"
read input?"Select script: \n{info_message}"
Expected output:
Select script: (mouse cursor here)
test
Real output:
Select script: \ntest
Attention: The mouse cursor should be remain at the read input and not at the info message below.
KornShell is something new for me, but I am interested in it and would like to get to know it.

Related

Output of complete script to variable

I have rather complex bash script which is normally run manually and thus needs live output on the console (stdout and stderr).
However, since the outcome and output of this script is rather important I'd like to save its output at the end into a database.
I have already a trap function for this and the database query as such is also not a problem. The problem is: How do I get the output of the entire script till that point into a variable?
The live console output should be preserved. The output after the database query (if any) does not matter.
Is this possible at all? Might it be necessary to wrap the script into another script (file)?
I'm doing similar task like this
exec 5>&1
message=$(check|tee /dev/fd/5)
mutt -s "$subjct" "$mailto" <<< "$message"
Add your script instead of check function and change mailing to db query.

Capturing output of an expect session with a bash script

I have a bash script that is using xmlstarlet to manipulate some key/value pairs in an application configuration file to prepare the file to be moved to a new production host. The values that need changed are host/encryption specific.
In order to discover one of the new values I need to interact with a vendor provided script in an expect session and capture the output into a variable in the bash script so I can continue to use it.
The expect part of the bash script looks something like this:
expect <<DONE
spawn command_provided_by_vendor
expect :
send -- "newvalue\r"
DONE
This is where I get stuck
In a shell the output of this command looks like:
Encrypted value (case sensitive, please cut and paste): 2qIrRvcSoHMb55dpcef6vw==
What I need to do is capture the non-whitespace output after the ":" and nothing I've tried works due to regexp errors, the parenthesis in the prompt string, etc.
There are other questions on stackoverflow that are similar, but I failed to understand how those answers helped my problem.
Any help, pointers appreciated.
I would use the expect command to look for an appropriate regular expression and capture the value there:
value=$(
expect <<DONE
spawn command_provided_by_vendor
expect :
send -- "newvalue\r"
expect -re {Encrypted value.*: (\S+)}
puts $expect_out(1,string)
expect eof
DONE
)

opening a bash in second terminal

Im pretty new to bash and want to open a second bash script on a second terminal.
but for some reason im not able to doe this.
Im using gnome-terminal and ive already set my preference to "hold terminal open"
if i just type in the terminal:
gnome-terminal --window-with-profile=shit -e "./Test.sh"
I get an error that says:
The child process exited normally with status 0 (and sometimes 2)
The test bash script is one line that says:
echo "hoi"
If anyone has the answer please let me know
Thanks in advance
The child process exited normally with status 0 is not an error message. It indicates everything went well in your script.
When hold terminal open is selected, this message will appear as a pop-up on the top of the new window when a command exits. Your output is simply too short for you to see under the pop-up message. If you add more lines to your output you should be able to see something.

How to detect on waiting for user input from bash script?

My bash script looks like:
read -p "Do you wish to continue?" yn
# further actions ...
And I just want to interact with this script using nodejs / child_process.
How can I detect that it's waiting for the user input?
var spawn = require('child_process').spawn;
var proc = spawn('./script.sh');
proc.stdout.on("data", function(data) {
console.log("Data from bash");
}
proc.stdin.on("data", function(data) {
console.log("Data from bash"); // doesn't work :/
}
Thank you!
From the bash man page:
read -p prompt
Display prompt on standard error, without a trailing newline, before
attempting to read any input. The prompt is displayed only if input is
coming from a terminal.
And I don't think there a any way in node.js to detect that the script is waiting for input. The problems is actually that bash detects a non-terminal and disables the output to standard error. And even then you would have to read from stderr and not stdin to detect any waiting states.
In the end, as Antoine pointed out, you might have to use tools like empty or Expect to wrap your shell-scripts and trick Bash to think it is in a terminal.
Btw.: proc.stdin.write("yes\n") works fine. Thus you could work with the script, but won't get any prompts on proc.stderr and will not know when the script actually reads the input. You can also immediately proc.stdin.write the input even if the script is not yet at the read -p statement. The input is buffered until the scripts eats it up.
Have you try to use "expect" http://en.wikipedia.org/wiki/Expect ?
It's a tool that "expect" some text (it use regular expression) and the bash script can automatically answers.

Abort bash script, if a certain console output string appears

I'm using a bash script to automatically run a simulation program. This program periodically prints the current status of the simulation in the console, like "Iteration step 42 ended normally".
Is it possible to abort the script, if the console output is something like "warning: parameter xyz outside range of validity"?
And what can I do, if the console output is piped to a text file?
Sorry if this sounds stupid, I'm new to this :-)
Thanks in advance
This isn't an ideal job for Bash. However, you can certainly capture and test STDOUT inside a Bash iteration loop using an admixture of conditionals, grep-like tools, and command substitution.
On the other hand, if Bash isn't doing the looping (e.g. it's just waiting for an external command to finish) then you need to use something like expect. Expect is purpose-built to monitor output streams for regular expressions, and perform branching based on expression matches.

Resources