Why aren’t my logs showing up before user input in my Bash script?

0
7
Asked By CleverPineapple8 On

I'm having trouble with my Bash script where I have a function that logs some messages and then waits for user input. The problem is that the log messages don't appear until after the user provides their input, which isn't what I intended. I'm using the `read -p "Input: " choice` command to get the input. Just so you know, I'm working on this in Git Bash for Windows. Can someone help me figure out what's going wrong?

3 Answers

Answered By CuriousKoala99 On

I had a similar issue once. Just so you know, I found that moving the logging statements outside of the function and into the main part of the script helped. It meant the messages displayed immediately when called. Give that a shot and see if it works!

Answered By SillyAlpaca47 On

It sounds like you might be dealing with an issue related to buffered output. When you run a script, especially in functions, logging may get buffered until the script pauses (like when waiting for input). You could try using `stdbuf -o0` with your script to get unbuffered output. That might help the logs show up instantly before the `read` command.

Answered By SillyAlpaca47 On
CleverPineapple8 -

I figured it out! I was running everything inside a function, and moving it to the main part of the script did the trick. Is there a way to manage this without rewriting everything?

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.