Sunday, May 5, 2024
 Popular · Latest · Hot · Upcoming
0
rated 0 times [  0] [ 0]  / answers: 1 / hits: 562  / 2 Years ago, wed, november 16, 2022, 10:50:45

How can i get this feature to work?


Pressing Esc while taking inputs from the user will exit the script


read -r -p "Enter the filenames: " -a arr

if press Esc; then
read $round
mkdir $round
fi

for filenames in "${arr[@]}"; do
if [[ -e "${filenames} ]]; then
echo "${filenames} file exists (no override)"
else
cp -n ~/Documents/library/normal.cpp "${filenames}"
fi
done

How can i detect Esc key in this script?


PS: Saw many resources

https://www.linuxquestions.org/questions/linux-newbie-8/bash-esc-key-in-a-case-statement-759927/

they use another variable like $key or read -n1 $key just one character input


but here what will i do if I've a string or an array?


More From » bash

 Answers
3

This should work in bash:


#!/bin/bash


# Bind the Escape key to run "escape_function" when pressed.
bind_escape () { bind -x '"e": escape_function' 2> /dev/null; }

# Unbind the Escape key.
unbind_escape () { bind -r "e" 2> /dev/null; }

escape_function () {

unbind_escape
echo "escape key pressed"
# command/s to be executed when the Escape key is pressed
exit

}

bind_escape

# Use read -e for this to work.
read -e -r -p "Enter the filenames: " -a arr

unbind_escape

# Commands to be executed when Enter is pressed.
for filename in "${arr[@]}"; do

echo "$filename"

done

[#376] Friday, November 18, 2022, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dileble

Total Points: 169
Total Questions: 105
Total Answers: 141

Location: Sao Tome and Principe
Member since Wed, Dec 29, 2021
2 Years ago
dileble questions
;