LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-17-2023, 01:19 PM   #1
pinqvin
LQ Newbie
 
Registered: Dec 2008
Posts: 23

Rep: Reputation: 2
Question BASH: How can I successfully pipe prompt strings from built-in programs to custom script functions to then print them on the terminal?


Hi all,

The following piece of code is supposed to pipe messages from built-in programs (like apt) to a couple of custom functions, which should then (i) change the colour of the received messages to make them compliant with my script's colour theme; (ii) append the same messages to a custom log file (irrelevant for now).

The trio (see: my code above) works well up until apt full-upgrade (line 29) should prompt the user with the Do you want to continue? [Y/n] message. But it's not printed on the terminal when piped through my function log_trace(), with the cursor just blinking on an empty line. But it's there, since it prints out as soon as I hit 'n' on the keyboard (to abort apt updates).

I'm sure it's somehow related to one of my custom functions, but cannot figure out the reason. Is it because the "invisible" prompt line doesn't push an EOL signal to bash?

Any help is highly appreciated.

My system: Pop!_OS 22.04 with bash 5.1.16 on top of Linux kernel 6.5.6.

Thank you!
Rustam

Last edited by pinqvin; 11-23-2023 at 03:50 AM.
 
Old 11-18-2023, 06:21 AM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,153

Rep: Reputation: 1265Reputation: 1265Reputation: 1265Reputation: 1265Reputation: 1265Reputation: 1265Reputation: 1265Reputation: 1265Reputation: 1265
Most output ends with a newline, which is what your read expects when it reads lines of input. The prompt is written without a newline and then apt calls flush to print the buffer anyway. I don't think there is a way to read a variable-length line without a newline because there is no way to know you are at the end. You could read a character at a time using the -n option.
 
1 members found this post helpful.
Old 11-18-2023, 09:49 AM   #3
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,783

Rep: Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214
In the bash built-in "read" function, you could use the "-t timeout" option to accept unterminated lines. You will have to expect repeated timeouts with zero-length reads while no input is available, but always check the return code to detect EOF and break the loop. Since the incomplete line is prompting for user input, a timeout of 1 or 2 seconds should not be noticeable to the user.
 
Old 11-18-2023, 10:38 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
Not sure if i have missed the point, based on the previous 2 replies, but in answer to this part:
Quote:
The trio (see: my code above) works well up until apt full-upgrade (line 29) should prompt the user with the Do you want to continue? [Y/n] message. But it's not printed on the terminal
Line 29
Code:
apt full-upgrade 2>/dev/null | log_trace "(APT)"
Assuming "apt" is like many other programs, the question is most likely being output to standard error, which you have sent off to /dev/null hell
So it is never being sent to your "log_trace" function

Again, I may have misread, so apologise if I am off base
 
Old 11-19-2023, 08:13 AM   #5
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,153

Rep: Reputation: 1265Reputation: 1265Reputation: 1265Reputation: 1265Reputation: 1265Reputation: 1265Reputation: 1265Reputation: 1265Reputation: 1265
I guess I thought prompts should go to stdout and not stderr, but "coding standards" is an oxymoron.

Anyway, if you want to read both streams do "2>&1" instead of "2>/dev/null". There will still be the issue of read expecting a newline.
 
Old 11-22-2023, 02:27 AM   #6
pinqvin
LQ Newbie
 
Registered: Dec 2008
Posts: 23

Original Poster
Rep: Reputation: 2
Thank you all for your suggestions! But, unfortunately, none of them works. E.g., read -t command, same as read -n, waits for a user interaction (\n) after a prompt anyway.

I also tried adding one of the IFS delimiters to the end of the string before passing it to read, to no avail.

So is there maybe a command different from read to read prompts (a line at a time) from system commands like apt or find?

Thanks!
 
Old 11-23-2023, 08:25 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
What happens if you remove the log_trace call?

I would not expect read to work as it is apt (IMHO) that is waiting for input, which you could look at expect to reply or see if this option has a way to bypass any questions?
 
Old 11-24-2023, 07:36 AM   #8
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,786

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Quote:
Originally Posted by grail View Post
Assuming "apt" is like many other programs, the question is most likely being output to standard error, which you have sent off to /dev/null hell
OP says the prompt does print after hitting 'n', so it must be going onto stanard output.


Quote:
Originally Posted by pinqvin View Post
Thank you all for your suggestions! But, unfortunately, none of them works. E.g., read -t command, same as read -n, waits for a user interaction (\n) after a prompt anyway.
It's possible that the apt program is seeing that its output is not a tty and changes to line buffering, so it is apt itself that is waiting for the \n. Try https://manned.org/unbuffer.1 in addition to read -n or read -t.

I would also point that using read -t in this situation is a bit tricky, as it will timeout and return a non-zero exit code even when it has read partial input. So just while read -t ; ... is not enough.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Trying to print list in terminal,print the results,not print from. ewpoppy Linux - Newbie 4 04-24-2020 07:33 AM
Brother MFC-J4510DW does not print although print jobs complete successfully willi53 Linux Deepin 3 01-15-2018 02:04 PM
(Samba 3.0.20) Vista prompts for username, XP prompts just for password Noffie Linux - Server 2 07-21-2008 10:26 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:17 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration