I have a Bash script I was trying to make to help me run a rather complex command with small changes that it would ask me about through echo and read.
I have found solutions to force it to run a terminal to execute the command, but I'm not interested in that. What I would like it to do is, if I space out and just hit Enter on it in Nautilus (making it run with Run Software), it'll just gently pop up a notification saying "Please run this from a terminal."
I can get the popup to happen -- as in I know the command -- but I can't get the Bash script to tell if it's being run inside a terminal or not, it seems to always think so. Is it even possible?
||
within[ … ]
like that? If you use[[ … ]]
then it would be fine, but normally the||
is used to separate commands, and[ -t 0
is an incorrect invocation of[
because its last]
is missing. There typically isn't a command-p
either. I agree with testing for a terminal; that's probably the way to do it. It's just the syntax I'm concerned about. – Jonathan Leffler Oct 18 '14 at 16:14/dev/stdin
exists in the file system depends on the operating system. – chepner Oct 18 '14 at 16:40-p
is a POSIX-specified primary for thetest
command. – chepner Oct 18 '14 at 16:42-p
is after the||
operator that separates commands. – Jonathan Leffler Oct 18 '14 at 17:34||
is seen before the required final]
argument to[
. – chepner Oct 18 '14 at 17:44csh
doesn't handle a lot of the in/out stuff inherited from the traditional Bourne style shells. On the other hand, the tradtional Bourne-style shells didn't handle the history and job control offered incsh
. general consensus was eventually found withksh
- and that is why it is the basis for the POSIXsh
standard. – mikeserv Oct 18 '14 at 18:06bash
does the same as most modern shells - it detects automatically if it should start interactive based on whether its i/o is connected to a terminal. It is forced interactive with-i
. Testing for an interactive shell is not the question - the question is testing for connection to a terminal. – mikeserv Oct 18 '14 at 18:11printf %b\\n 'PS1="this is an interactive shell\n"' '[ -t 0 ] || [ -t 1 ] || exit' | bash -i 2>&1 | sed '$a\and it is not connected to a terminal'
– mikeserv Oct 18 '14 at 18:25bash
. Pragmatically, yours is perhaps the better test (+1) but not perfect; I wanted to post this because there is something canonical about it (considering the specifics of the question) and its use value for people who may find their way here. – goldilocks Oct 18 '14 at 18:49PS1
is often set in a shell that isn't interactive, because people often set it in.profile
or/etc/profile
. – Gilles 'SO- stop being evil' Oct 18 '14 at 20:31PS1
is not a reliable test to tell whether the shell is interactive. “If a script needs to test whether it is running in an interactive shell” is also confusing: it should be if some code needs to test — a script is usually not running in an interactive shell (but it can be, if it's sourced). Testing fori
in$-
is the correct way to test if the shell is interactive. Testing-t 0
or-t 2
is the correct way to tell if the script is running in a terminal, which is different from being interactive. – Gilles 'SO- stop being evil' Oct 18 '14 at 20:37-p
bit, it contradicts the comment above that mentions a socket, for which the test would be-S
. But I don't know what John Lange is trying to do there: if stdin isn't a terminal over SSH, it means that the script is not executed interactively, in which case it probably shouldn't do whatever it likes to do in a terminal, and in particular it shouldn't try to perform any user interaction. – Gilles 'SO- stop being evil' Oct 18 '14 at 20:39i
in$-
).Why? It's pretty clear that OP does not want to be able to double-click the script in Nautilus, and would rather it be run in the terminal (so that they can interact with it). However, if OP decides to redirect input for some reason at a later time, then the script breaks.
– Franz Kafka Oct 18 '14 at 22:30The following paragraphs describe how bash executes its startup files."
– Franz Kafka Oct 18 '14 at 22:37So, in essence it would seem that both you and mikeserv disagree with the bash man page on this one.
– Franz Kafka Oct 18 '14 at 22:38#!/bin/bash
) is never interactive, even if it's running in a terminal. The remark aboutPS1
is admittedly misleading: bash sets it in an interactive shell, but it can also be set in a non-interactive shell, so it isn't a reliable test. – Gilles 'SO- stop being evil' Oct 19 '14 at 10:10