Catch combination Alt, Primary in bash to variable - bash

Is it possible to catch + + B (as example) in the following example:
#!/bin/bash
echo "give your key combination and press <ENTER>:"
read input
echo $input
I need B literally as a variable.
= Ctrl button

I'm going to say no. Use a different language. If you're totally desparate you might try one of these hacks, but I don't endorse them (other than the KEYBD trap, which isn't Bash).

Related

Injecting key combinations into Bash tty using TIOCSTI in Python

I am trying to inject key combinations (like ALT+.) into a tty using the TIOCSTI in Python.
For some key combinations I have found the corresponding hex code for Bash shells using the following table which works good.
From this table I can see that for example CTRL+A is '\x01' etc.
import sys,os,Queue
import termios,fcntl
# replace xx with a tty num
tty_name = "/dev/pts/xx";
parent_fd = os.open(tty_name, os.O_RDWR)
special_char = "Ctrl_a"
if special_char == "Ctrl_a":
send_char = '\x01'
if special_char == "Ctrl_e":
send_char = '\x05'
if special_char == "Ctrl_c":
send_char = '\x03'
fcntl.ioctl(self.parent_fd, termios.TIOCSTI, send_char)
But how can I get the hex codes for other combinations such as
ALT+f etc. I need a full list or a way how to get this information for any possible combo as I want to implement most bash shortcuts for moving, manipulating the history etc. to inject.
Or is there any other way to inject key-combinations using TIOCSTI ?
As I can only send single chars to a tty I wonder if there is anything else possible.
Thank you very much for your help!
The usual working of "control codes" is that the "control" modifier substracts 64 from the character code.
"A" is ASCII character 65, so "Ctrl-A" is "65-64=1".
Is it enough for you to extend this scheme to your situation?
So, if you need the control code for, for example, "Device Control 4" (ASCII code 20), you'd add 64, to obtain "84", which is "T".
Therefore, the control-code for DC4 would be "Control+T".
In the reverse direction, the value for "Control+R" (history search in BASH) is R-64, so 82-64=18 (Device Control 2)
ASCIItable.com can help with a complete listing of all character codes in ASCII
Update: Since you were asking specifically for "alt+.":
The 'Control mean minus 64" doesn't apply to Alt, unfortunately; that seems to be handled completely differently, by the keyboard driver, by generating "key codes" (also called "scancodes", variably written with or without spaces) that don't necessarily map to ASCII. (Keycodes just happen to map to ASCII for 0-9 and A-Z, which leads to much confusion)
This page lists some more keycodes, including "155" for "alt+."

Need to answer half questions automatically on script

Is it possible to answer first few or a part of questions automatically and rest manually?
I am connecting to a VPN daily which gives first prompt to say "yes" or "no", second one to choose a group and third one to input username. Upto three questions, answers will be same. Fourth prompt is a physical RSA token input and fifth one is a password.
When I script it with an input file for first three questions (eg; ./script < inputfile), the connection exits as below.
GROUP: [xxxx|abcdgroup01|sssgroup01|sssssgroup01z]:Please enter your username and password.
Username:Password:
fgets (stdin): Inappropriate ioctl for device
This is happening because the script is not getting the fourth answer from the input file.
Out of five inputs, four are static ones and the fourth one is a dynamic one.
How do I manage to enter fourth input manually ?
Please help; Thanks in advance !
There are other ways, but for your case, your approach doesn't seem to be so bad.
If you really want the user to see the original prompt from script.sh, you would have to create an expect program to drive your script; see the man-page for expect.
If you are permitted to change script.sh, you could add the possibility to supply certain parameters from the outside (a file, or an environment variable) and only ask for those parameters which are not supplied. This would IMO be the best approach.
I just found this method and it seems working !
{ echo yes; echo xxxxx; echo xxxx; read rsa; echo $rsa; echo xxxx; } | ./script.sh
Any other way ?

subshell in var assignment prevents proper document highlighting

I am yad'fying an alarm script I use from the terminal multiple times a day for quick reminders. Anyway, this var assignment:
killOrSnz=$((sleep .1 ; wmctrl -r yadAC -e 0,6,30,0,0) | yad --title yadAC --image="$imgClk" --text "Alarm:\n${am}" --form --field="Hit Enter key to stop the alarm\nor enter a number of minutes\nthe alarm should snooze." --button="gtk-cancel:1" --button="gtk-ok:0"|sed -r 's/^([0-9]{1,})\|[ ]*$/\1/')
is causing me grief. The var works fine, as intended, except that all of the code below it is no longer highlighted in my vim session, making my eyes hurt just to look at it never mind scan for problems or to make alterations.
I borrowed the idea of piping yad command thru wmctrl to gain better control over window geometry, which is great from another post on here, but there was of course no mention of the potential side-affects. I want to keep fine control over the window placement of apps, but it would just be nice to do so while maintaining document highlighting.
I did try to rearrange the pipe and subshell to see if I could get it to work another way that didn't interfere with my vim highlighting, but there was no love to be had any which way but this way.
It appears that VIM's parser is fooled by the $((, mistaking it for the start of an arithmetic expression rather than a command substitution whose first character is a parentheses. Since there is no matching )), the colorizer gets confused about what is what. Try adding an explicit space between the two open parens:
killOrSnz=$( (sleep .1; ... )

mac keyboard function keys mapping

I lost my configuration or never had it.
I need to send shift+F1 and ctrl+F1, shift+F2 and ctrl+F2 and so on to the terminal since I'm connecting to a linux machine via ssh.
well, my current config is missing these string mappings as you can see in the image.
do you know what are the string mappings for those key combinations ?
thanks.
UPDATE:
in some stackexchange i found this command:
$>for x in {1..12}; do echo -n "F$x "; tput kf$x | cat -A; echo; done
can you help me to print the strings for shift + f1 ?
First: From this page http://invisible-island.net/xterm/xterm.faq.html, you will understand that shift-F1 maps to F11, shift-F2 maps to F12 and so on.(Search the page 'a summary of the keyboard mapping' without ', you will see the list).
Second: From this page http://aperiodic.net/phil/archives/Geekery/term-function-keys.html, you will know F1 or F11 or F20 maps to what string(at the bottom).
More to say:
If you want to send Shift-F1 to a program through Terminal, these will happen:
You hit Shift-F1, the Terminal will translate the Shift-F1 to a string sequence
The string sequence will be sent to the program
For example, you send a Shift-F1 to emacs through the Terminal,the Terminal translate it into string sequence and send the string sequence to emacs, emacs will recognise it as a key strokes then do something.
I'm not quite familiar about this, so this answer may be not right.

Bash history enumerated from the end

In bash one can !-1 to execute the 1 command from the history enumerated from the end starting at 0. But what how can one view history so that mentioned enumeration is shown instead of usual one by built-in means? Of course one can write a simple script for that but I feel like there should be some kind of option for that.
Another question is if there is a way to somehow switch history expansion so that - sign would be unnessasary. Say exchanging meaning of !1 and !-1.
Showing negative indices is simple to implement. Just take the length of history (or get it from history 1), and subtract it from the index of all other history items.
neghistory() {
local i n s
read n s < <(history 1)
history "$#" | while read i s; do
printf '%5d %s\n' $((i-n-1)) "$s"
done
}
I don't see any built-in ways to affect history's output like this nor change how indices in history expansion works.

Resources