1

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
2

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 "\n\nYou pressed $bb.\n\n"
fi
}

while true; do
    read -t 1 -n 1 bb && ACTION
    echo -ne "Hello $a times. Hit \"q\" to quit."'\r' 
    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 "\n\n############\n#          #\n# Bye bye  #\n#          #\n############\n" && 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'\r'

  # 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
  • Calling READ from READ adds a stackframe. If you do that too much you get a segfault: re() { t=$((t+1)); if [[ $t -gt 80000 ]]; then echo foo; return; fi; re; }; re – Ole Tange Apr 9 '19 at 8:57
  • You are right. I've updated the script with "while true". Thank you! – desgua Apr 12 '19 at 16:00
1

i would make the inf. loop its own separate script then when i run it put a ampersand after it

#!/bin/bash
cat /var/log/dmesg
~/inf-loop.sh &
ls /var/cache/apt/archive/*.deb
  • the point is there are many parameter related between those tow parts of the script and i dont want to run it in background as i want the terminal which control it in front of me and thank u for replying , in case u have more explanation for me u are welcome :) – Ahmed Zain El Dein May 3 '13 at 21:48
  • you could pass the parameters to the other script ~/inf-loop.sh val1 val2 &, unless you need them to change, if that is the case i dont know how – GM-Script-Writer-62850 May 3 '13 at 21:52
  • ok a part of this code above is a function can i put the secode while part in another file and execute it while executing can i call this function from inside the bashscript file of loop.sh while it is within the other file with its definition and all ? – Ahmed Zain El Dein May 3 '13 at 23:07
  • the code i posted will run loop.sh as its own process and not wait for it to finish before continuing, you can pass variables to it the same as you would anything else in bash – GM-Script-Writer-62850 May 4 '13 at 1:42
  • yes variable yes but i cant call within the loop.sh file another function written in the other script file to execute it right ? – Ahmed Zain El Dein May 4 '13 at 12:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.