Note that there are some explanatory texts on larger screens.

plurals
  1. PObash script: How to implement your own history mechanism?
    primarykey
    data
    text
    <p>I'm implementing an interactive bash script similar to the MySQL client, <code>/usr/bin/mysql</code>. In this script I need to issue various types of 'commands'. I also need to provide a history mechanism whereby the user can use the up/down arrow keys to scroll through the commands entered so far.</p> <p>The snippet listed here (<a href="http://tldp.org/LDP/abs/html/internal.html" rel="nofollow">Example 15-6, Detecting the arrow keys</a>) does not exactly do what I want it to. I really want the following:</p> <ol> <li><p>The up/down arrow keys should operate in silent mode. Meaning, they should not echo their character codes on the terminal. </p></li> <li><p>The other keys however (which will be used to read the command names and their arguments) <strong>must not</strong> operate in silent mode.</p></li> </ol> <p>The problem with <code>read -s -n3</code> is that it does not satisfy my simultaneously conflicting requirements of silent mode <strong>and</strong> echo mode, based solely on the character code. Also, the value <code>-n3</code> will work for arrow keys but, for other/regular keys, it won't 'return control' to the calling program until 3 characters have been consumed.</p> <p>Now, I could try <code>-n1</code> and manually assemble the input, one character at a time (yuck!). But the character-code based silent-/echo-mode switching problem would still persist!</p> <p>Has anyone attempted this thing in bash? (Note: I cannot use C, nor other scripting languages like Perl, Python, etc.)</p> <p><strong>EDIT</strong></p> <p>Continuing with Dennis' answer... You will <em>also</em> need to <em>manually</em> add your desired entries to your history via <code>history -s</code>, like so...</p> <pre><code>while read -e x; do history -s "$x" # ... done </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload