LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 10-02-2022, 01:11 PM   #1
framp
Member
 
Registered: Apr 2003
Location: Next to Stuttgart, Germany
Distribution: Debian, Mint, Raspbian
Posts: 131
Blog Entries: 5

Rep: Reputation: 27
bash: [ -t 0 ] does return false if script is piped into bash on the console


I want to pipe a script into bash from the console like

Code:
cat t.sh | bash -s -- -1
This works fine but for some reasons
Quote:
[ -t 0 ]
returns false even the command is invoked from the console.

I wrote a small script to demonstrate the effect:

Quote:
#!/bin/bash

n=${1:-1}

if [ -t 0 ]; then
echo "$n: Interactive"
else
echo "$n: Non interactive"
fi

if (( $n <= 1 )); then
./t.sh $(($n+1))
fi
If I invoke the script on the console with

Quote:
./t.sh 0
I get

Quote:
./t.sh -1
-1: Interactive
0: Interactive
1: Interactive
2: Interactive
If I use

Code:
cat ./t.sh | bash -s -- -1
-1: Non interactive
0: Non interactive
1: Non interactive
2: Non interactive
Any idea why this doesn't work?
 
Old 10-02-2022, 01:55 PM   #2
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS, Manjaro
Posts: 5,714

Rep: Reputation: 2734Reputation: 2734Reputation: 2734Reputation: 2734Reputation: 2734Reputation: 2734Reputation: 2734Reputation: 2734Reputation: 2734Reputation: 2734Reputation: 2734
Because bash is running within a pipe subshell, not directly at your terminal. Bash is acting, in other words, exactly as the man page specifies.

So it IS working, your understanding is just lacking.

I hope this helps AND gives you a clue as to where to find out more.
 
Old 10-02-2022, 01:59 PM   #3
computersavvy
Senior Member
 
Registered: Aug 2016
Posts: 3,345

Rep: Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484
A slight change to your script
Code:
#!/bin/bash

#n=${1:-1}
n=$1

if [ $n -eq 0 ]; then
echo "$n: Interactive"
else
echo "$n: Non interactive"
fi

if (( $n <= 1 )); then
./t.sh $(($n+1))
fi
yields
Code:
$ ./t.sh -1
-1: Non interactive
0: Interactive
1: Non interactive
2: Non interactive
You can research the topic at https://tldp.org/LDP/abs/html/index.html to understand why this change works.
 
Old 10-02-2022, 02:31 PM   #4
framp
Member
 
Registered: Apr 2003
Location: Next to Stuttgart, Germany
Distribution: Debian, Mint, Raspbian
Posts: 131

Original Poster
Blog Entries: 5

Rep: Reputation: 27
Quote:
Originally Posted by wpeckham View Post
I hope this helps AND gives you a clue as to where to find out more.
I see. I thought the interactive property is inherited by subshells. But frankly I have no clue where to search and which keyword to use to find a solution for my issue.

Quote:
Originally Posted by computersavvy View Post
Nice code change. But this doesn't help me at all :-(

I just found my post where I explained my reason why I use this way to call a bash script. https://www.linuxquestions.org/quest...1/#post6172880

Unfortunately I now need a way to pass the interactive property down to the called script.

Last edited by framp; 10-02-2022 at 02:35 PM.
 
Old 10-02-2022, 07:51 PM   #5
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,817

Rep: Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211
The pipe hides the original input.
Also there is useless use of cat.
The following solves both:
Code:
< t.sh bash -s -- -1
 
1 members found this post helpful.
Old 10-03-2022, 01:06 AM   #6
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,817

Rep: Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211
Now I had a look at your previous post.
If you must feed it by a program (curl, not cat) then try to run the shell in the foreground (and the program in the background).
Code:
sudo bash -s -- -o < <( curl ... )
Another attempt is the "lastpipe" shell option:
Code:
shopt -s lastpipe
curl ... | sudo bash -s -- -o
 
2 members found this post helpful.
Old 10-03-2022, 01:07 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,986

Rep: Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337
Quote:
Originally Posted by framp View Post
I see. I thought the interactive property is inherited by subshells. But frankly I have no clue where to search and which keyword to use to find a solution for my issue.
No, that is not possible. Every invocation of bash can (and will) decide if it was started in interactive mode - by itself. Interactive mode also assumes there is a real input (keyboard) attached to that bash.
Subshells and other scripts which are just executed are not interactive shells, and that is the intended way (especially when the input and output are connected to pipes)

Quote:
Originally Posted by framp View Post

Unfortunately I now need a way to pass the interactive property down to the called script.
You must not do that. You need to redesign your script. As it was suggested by MadeInGermany you can avoid starting a subshell, but in some cases it does not help (obviously it depends on your script).
 
1 members found this post helpful.
Old 10-03-2022, 04:17 AM   #8
framp
Member
 
Registered: Apr 2003
Location: Next to Stuttgart, Germany
Distribution: Debian, Mint, Raspbian
Posts: 131

Original Poster
Blog Entries: 5

Rep: Reputation: 27
Quote:
Originally Posted by MadeInGermany View Post
The pipe hides the original input.
Also there is useless use of cat.
The following solves both:
Code:
< t.sh bash -s -- -1
Thank you very much for your answers. They are very interesting. Now I have something to test and to work with :-)

As far as I can see right now I have to redesign the code (suggested by @pan64) and solve the issue with an invocation parameter to signal the script is running in interactive mode (that's what I think @comutersavvy suggested with his code change - sorry I didn't catch this yesterday) because I detected the called script also uses
Code:
BASH_SOURCE[0]
to extract the directoryname of the script but this is empty if the script is piped into bash. Or I have to find a way to remove the usage of
Code:
BASH_SOURCE[0]
.

Thank you very much to everybody for your help. I now know which alternatives I have and will choose one of them :-)
 
Old 10-03-2022, 07:13 AM   #9
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,986

Rep: Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337
you may try to use $0 instead of BASH_SOURCE[0], probably that will work. And would be nice to show us a better example about that issue.
 
Old 10-03-2022, 01:44 PM   #10
framp
Member
 
Registered: Apr 2003
Location: Next to Stuttgart, Germany
Distribution: Debian, Mint, Raspbian
Posts: 131

Original Poster
Blog Entries: 5

Rep: Reputation: 27
Quote:
Originally Posted by pan64 View Post
you may try to use $0 instead of BASH_SOURCE[0], probably that will work. And would be nice to show us a better example about that issue.
I try to find a solution and will post it here. If I don't find a solution I will write code which explains my issue.
 
Old 10-05-2022, 01:42 PM   #11
framp
Member
 
Registered: Apr 2003
Location: Next to Stuttgart, Germany
Distribution: Debian, Mint, Raspbian
Posts: 131

Original Poster
Blog Entries: 5

Rep: Reputation: 27
I detected other issues which are related to the way the called script works with stdin, stdout and stderr :-(. Therefore I decided to split everything into two commands: One which downloads the script and the other which calls the downloaded script with sudo.

Again: Thank you very much for your comments and help.
 
1 members found this post helpful.
  


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
getting return code from command with piped input noguru Linux - Newbie 6 10-01-2012 06:57 AM
mutt does not honour mutt.rc 'use_from' entry when accepting piped input davidchall Linux - Software 0 05-28-2012 02:13 AM
Using piped input in a BASH script gnashley Programming 1 08-05-2007 10:50 AM
Return true or false if I have ping reply Menestrel Programming 4 11-29-2004 12:40 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 07:14 AM.

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