Sunday, April 28, 2024
 Popular · Latest · Hot · Upcoming
2
rated 0 times [  2] [ 0]  / answers: 1 / hits: 1369  / 2 Years ago, thu, may 12, 2022, 5:58:38

i have this bash script code and i want to run the read command side by side with the while loop as the read command fetch data from user and save it in some file and the while loop do checking on a related something , so any suggestions like doing them in both terminals or something like that



Thanks



 #!/bin/bash 
trap "echo "" > /home/........./file.txt" SIGINT SIGTERM SIGHUP
while sleep 2
do
read -s -n 1 key
if [[ $key = "" ]]; then
echo >> somefile
else
echo "You pressed '$key'"
fi
clear
done
while sleep $sleepInterval
do
i=0
while read line
do
somecommands
done

More From » 12.04

 Answers
0

This is how I do that:



#!/bin/bash

a=0

function ACTION ()
{
if [[ "$bb" == "Q" || "$bb" == "q" ]]; then
echo "Good bye!"
exit 0
else
echo -e "

You pressed $bb.

"
fi
}

while true; do
read -t 1 -n 1 bb && ACTION
echo -ne "Hello $a times. Hit "q" to quit."'
'
a=`echo "$a + 1" | bc`
done
exit 0


And this is what was answered at Stackoverflow by Andy and works great here.



The script is his (I just added the trap part as he says that terminal would be in a weird state if the script gets killed):



#!/bin/bash

trap 'echo -e "

############
# #
# Bye bye #
# #
############
" && exit 1' INT
trap 'notify-send "Bye bye" "The terminal has been closed" && exit 1' HUP

if [ ! -t 0 ]; then
echo "This script must be run from a terminal"
exit 1
fi

stty -echo -icanon time 0 min 0

count=0
keypress=''
while true; do
let count+=1
echo -ne $count'
'

# This stuff goes in _handle_keys
read keypress
case $keypress in
# This case is for no keypress
"")
;;
$'e[C')
echo "derecha"
;;
$'e[D')
echo "izquierda"
;;
# If you want to do something for unknown keys, otherwise leave this out
*)
echo "unknown input $keypress"
;;
esac
# End _handle_keys
done

stty sane

[#31406] Friday, May 13, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
smelrop

Total Points: 263
Total Questions: 122
Total Answers: 108

Location: Saudi Arabia
Member since Thu, Jan 28, 2021
3 Years ago
smelrop questions
Mon, Mar 13, 23, 07:22, 1 Year ago
Sun, Feb 5, 23, 13:02, 1 Year ago
Tue, Aug 31, 21, 00:50, 3 Years ago
Sat, Dec 18, 21, 15:18, 2 Years ago
;