News for hackers. Stuff that matters.
Re:Command Line Interface Guidelines by saghm on Tuesday February 06, 2024 @11:12AM (#39276029)
I think there are ways to detect if stdout is a pipe and operate differently (e.g. by using different defaults for coloring output), but I'm not sure if there are ways to detect what the other side of the pipe is, much less what `dry-run` would actually be expected to be in this case
Re:Command Line Interface Guidelines by jasonjmcghee on Tuesday February 06, 2024 @11:31AM (#39276377)
Maybe I’m misunderstanding but sounds like you’re proposing the modifying the command on the left to detect whether it’s piping it’s output (curious how you do that btw sounds pretty cool / useful)

But at that point you could just handle —dry-run directly

Re:Command Line Interface Guidelines by kergonath on Tuesday February 06, 2024 @11:38AM (#39276497)
> Maybe I’m misunderstanding but sounds like you’re proposing the modifying the command on the left to detect whether it’s piping it’s output (curious how you do that btw sounds pretty cool / useful)

You can do that. Possibly not from all languages but for anything that can call functions in the c standard library, that’s what isatty() is for (among other uses). It takes a file descriptor and returns whether it is a terminal or not. If you do this with stdout, this tells you whether it goes to a terminal or whether it is redirected in a way.

As the parent suspects, though, this won’t tell you anything about what is on the other side of the redirection.

Re:Command Line Interface Guidelines by jasonjmcghee on Tuesday February 06, 2024 @11:45AM (#39276632)
Very cool function - thank you!
Re:Command Line Interface Guidelines by epcoa on Wednesday February 07, 2024 @08:38AM (#39288378)
It also doesn’t tell you what is the terminus of a pipeline. Because often with isatty what you really want to know is if the pipeline ends with a tty. `ls` is screen formatted but `ls | less` is not without extra work.
Re:Command Line Interface Guidelines by gpderetta on Tuesday February 06, 2024 @12:00PM (#39276912)
on the other hand you could have 'dry-run <command>' that via .so interposition tricks could intercept and list all destructive changes done by an arbitrary <command>, as a form of sandboxing.
Re:Command Line Interface Guidelines by account42 on Wednesday February 07, 2024 @09:22AM (#39288886)
Not generally because you can't e.g. know if a write to a socket is destructive or just a query for information needed to decide the next steps/output.
Re:Command Line Interface Guidelines by gpderetta on Wednesday February 07, 2024 @10:27AM (#39289661)
True, I was only thinking of fs access!
Re:Command Line Interface Guidelines by kjs3 on Tuesday February 06, 2024 @11:44AM (#39276614)
If it operated differently based on what it was outputting to, that kinda defeats the point of 'dry run', which is to see exactly what's going to happen based on what's on the other side of the pipe when I run it for real. "Did this blow up because it's a bad command, or because there's a bug in the 'only kinda real dry run' code?".
Re:Command Line Interface Guidelines by samstave on Tuesday February 06, 2024 @04:10PM (#39280660)
+++

-

What if there wasa 'deep-pipe' '||' which would be based on a set env/docker/blah - which would launch an env and execute your '||'d code in it, and output some log/metrics/whatever?

Re:Command Line Interface Guidelines by sokoloff on Tuesday February 06, 2024 @12:37PM (#39277556)
I often pipe output to tee and would be pretty annoyed if that changed the behavior of the original command to not do anything because stdout was a pipe.
Re:Command Line Interface Guidelines by indymike on Tuesday February 06, 2024 @03:49PM (#39280370)
> I often pipe output to tee and would be pretty annoyed if that changed the behavior of the original command

The output sent to tee is usually not the same as the output from the command to the terminal, so you are getting something different than most human users expect from original command... the reason is that terminal escape codes and other formatting for humans may need to be omitted from output to a pipe. You do this by asking the OS, "is this thing a terminal?".

Python example

"terminal" if sys.stdout.isatty() else "something else"

C is very similar:

if (isatty (1)) fprintf (stdout, "Terminal."); else fprintf (stdout, "Not Terminal.");

(printf works, too).

Re:Command Line Interface Guidelines by sokoloff on Tuesday February 06, 2024 @03:57PM (#39280474)
Sure; the output format changes, but the functionality doesn't.

Even ls outputs a tabular format by default when it's on a terminal and a list one file/dir per line when it's not on a terminal (if it's piped to cat for example or why ls | wc -l correctly counts the entries).

But the (essential) behavior of the command remains the same. ls still lists files/dirs... scp still copies files, etc.

Re:Command Line Interface Guidelines by indymike on Wednesday February 07, 2024 @02:17PM (#39292912)
> But the (essential) behavior of the command remains the same.

Of course. A command needs to do it's defined function.

You'll find some programs that are quite a bit different when invoked from outside the terminal vs inside the terminal. Developers need to take into account both situations, which is really the point the original post.

Re:Command Line Interface Guidelines by saghm on Tuesday February 06, 2024 @07:43PM (#39283025)
You can see this pretty easily with `ls` as well by using different options for the color. If you run `ls --color=auto`, you'll get colored output when running directly but black and white output if you pipe to `less`. However, if you pass `--color=always`, you'll get colored output when running directly and a bunch of garbage around some of the entries when piping to `less` because it doesn't interpret ANSI escape codes for color by default (although depending on what the output you're piping into `less` is, there are some workarounds like https://www.gnu.org/software/src-highlite/)
Re:Command Line Interface Guidelines by funcDropShadow on Wednesday February 07, 2024 @06:57AM (#39287459)
You can use ls --color=always | less -R to render the colors in less.
Re:Command Line Interface Guidelines by samstave on Wednesday February 07, 2024 @08:08AM (#39288083)
Thats fn cool
Re:Command Line Interface Guidelines by saghm on Friday February 09, 2024 @08:21PM (#39322540)
Huh, not sure how I've never found that option before. Thanks for the lesson, I'll need to rtfm a bit more closely!