How to change bash script output when the script is running?

Scott Pearce

I have the following loop which counts from 0 till 99:

    #!/bin/bash

for ((i=0;i<100;i++));
do
   echo $i
   sleep 1
done

Is there a way to change the result of the output from terminal while this loop script is running. Lets say if I press letter k the loop automatically add 10 more number to the current number, so if we have 10 being displayed on the screen and we press K the loop should automatically change to 20! Thanks

taliezin

As mentioned in comment you can use:

read -t 1 -n 1 key

which because of -t option we can remove sleep, so your script could be:

#!/bin/bash

for ((i=0; i<100; i++)); do
    read -t 1 -n 1 key
    if [ "$key" = "k" ]; then
        i=$((i + 10))
    fi
    echo $i
done

But I think more portable could be:

#!/bin/bash

if [ -t 0 ]; then stty -echo -icanon -icrnl time 0 min 0; fi

keystroke=''
i=0
while [ $i -lt 100 ]; do
    keystroke="$(dd bs=1 count=1 2>/dev/null)" # http://www.tldp.org/LDP/abs/html/
    if [ "$keystroke" = "k" ]; then
        i=$(( i + 10 ))
    elif [ "$keystroke" = "q" ]; then
        break
    fi
    i=$(( i + 1 ))
        echo $i
    sleep 1 
done

if [ -t 0 ]; then stty sane; fi

exit 0

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Unexpected Numerical Output when running automated script using Webdriver and Java

分類Dev

Variable notation when running python commands with arguments in a bash script

分類Dev

How to stop the bash script when a condition fails?

分類Dev

Using the output of a php script in a bash script

分類Dev

Running bash script in a kubernetes pod

分類Dev

bash script retrieve dbus output

分類Dev

How can I capture two columns of ls output in a bash script

分類Dev

How can I change the user during a bash script execution?

分類Dev

How can I suppress output of a bash script from within the script itself?

分類Dev

Running Scrapy from a script with file output

分類Dev

Running a command in a new terminal instance in a bash script

分類Dev

Running a shell script with and without "bash" command

分類Dev

How to authenticate programmatically when running an Apps Script Web App from another Apps Script project

分類Dev

How to loop for 3 times in bash script when docker push fails?

分類Dev

Bash Script store cat output in variable and then echo it

分類Dev

Bash script saving output of pipe sed to variable

分類Dev

How to run a script when there is a change in your local IP?

分類Dev

Bash script to monitor file change and execute command

分類Dev

Functions defined in .zshrc not found when running script

分類Dev

Not found error when running a script with `sh`

分類Dev

VSC throwing an error when running powershell script

分類Dev

Getting an error when running my script in a rule

分類Dev

Running script with parameter when item is clicked

分類Dev

Segmentation Fault when running a script using System ()

分類Dev

Bash-Script: How to insert Variables into Bash-Script?

分類Dev

How to filter script output in bash using a query like "(me OR you) OR (john AND ! doe)"?

分類Dev

Passing variables to a bash script when sourcing it

分類Dev

How to stop npm script running in background automatically

分類Dev

How to stop a running python script by Ansible?

Related 関連記事

  1. 1

    Unexpected Numerical Output when running automated script using Webdriver and Java

  2. 2

    Variable notation when running python commands with arguments in a bash script

  3. 3

    How to stop the bash script when a condition fails?

  4. 4

    Using the output of a php script in a bash script

  5. 5

    Running bash script in a kubernetes pod

  6. 6

    bash script retrieve dbus output

  7. 7

    How can I capture two columns of ls output in a bash script

  8. 8

    How can I change the user during a bash script execution?

  9. 9

    How can I suppress output of a bash script from within the script itself?

  10. 10

    Running Scrapy from a script with file output

  11. 11

    Running a command in a new terminal instance in a bash script

  12. 12

    Running a shell script with and without "bash" command

  13. 13

    How to authenticate programmatically when running an Apps Script Web App from another Apps Script project

  14. 14

    How to loop for 3 times in bash script when docker push fails?

  15. 15

    Bash Script store cat output in variable and then echo it

  16. 16

    Bash script saving output of pipe sed to variable

  17. 17

    How to run a script when there is a change in your local IP?

  18. 18

    Bash script to monitor file change and execute command

  19. 19

    Functions defined in .zshrc not found when running script

  20. 20

    Not found error when running a script with `sh`

  21. 21

    VSC throwing an error when running powershell script

  22. 22

    Getting an error when running my script in a rule

  23. 23

    Running script with parameter when item is clicked

  24. 24

    Segmentation Fault when running a script using System ()

  25. 25

    Bash-Script: How to insert Variables into Bash-Script?

  26. 26

    How to filter script output in bash using a query like "(me OR you) OR (john AND ! doe)"?

  27. 27

    Passing variables to a bash script when sourcing it

  28. 28

    How to stop npm script running in background automatically

  29. 29

    How to stop a running python script by Ansible?

ホットタグ

アーカイブ