LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   argument - vs -- (https://www.linuxquestions.org/questions/programming-9/argument-vs-4175674927/)

Skaperen 05-09-2020 09:53 PM

argument - vs --
 
on a command, if the argument "--" means end of options and only parse for regular arguments, then what can "-" mean?

ehartman 05-09-2020 10:05 PM

Quote:

Originally Posted by Skaperen (Post 6121268)
then what can "-" mean?

Often in programs with more then 1 input file: this argument should be taken as stdin instead of a file.

MadeInGermany 05-11-2020 04:51 PM

In traditional Unix a few tools took - to indicate the last option, others took - to indicate stdin.
Posix says that -- indicates the last option.
Today the - should indicate stdin. But a few commands might still treat it as "last option" in order to be compatible with traditional Unix.

Skaperen 05-12-2020 08:06 PM

so, basically, "-" would be an argument, not an option just as file names are arguments. but it would be up to the program to interpret the semantics, which for a program that is getting a bunch of file names there, would typically assume it to mean read STDIN for this data. but even if it allows the same file twice, it can only read STDIN once. of course, it could cache that data from the first "-" and read that cache for each subsequent "-" in its list of files. for programs meaning something other than a list of files, such as a list of users, it would have to give some particular meaning to "-" or flag it as an error.

i am currently developing a command line option/argument interpreter that will fit into a multi-layer interpreter for future commands i am writing which need some things other option parsers just don't seem to support. at the layer this new code is at it looks like "-" needs to be passed along as an argument. a lower layer will be dealing with understanding it as STDIN or whatever.

ehartman 05-12-2020 08:49 PM

Quote:

Originally Posted by Skaperen (Post 6122341)
it can only read STDIN once. of course, it could cache that data from the first "-" and read that cache for each subsequent "-" in its list of files.

No, normally the argument "-" can only be used to replace one of the filenames in the commandline, because - as you said - you can only read stdin once, it is a pure queue, once read that part of the data is lost. And - of course - it is only of use in programs that have multiple file arguments. From "man diff"
Quote:

FILES are `FILE1 FILE2' or `DIR1 DIR2' or `DIR FILE...' or `FILE... DIR'.
If a FILE is `-', read standard input.
Note the "a FILE" (singular) formulation.

NevemTeve 05-12-2020 10:00 PM

Though EOF-condition on a terminal is not persistent, so theoretically something like this could work (untested):
Code:

cat - /etc/motd - >merged.txt
Header
^D
Footer
^D


pan64 05-13-2020 01:43 AM

In general the shell script will not evaluate/modify - and --, so they will be passed to the command as is.
The command itself will process these arguments and the interpretation depends only on the developers/designers. There are conventions, like - means stdin, -- means the last option, but again, it depends only on the command. Fortunately we have several different and conflicting conventions. So better to read the man page if you are unsure.

As an interesting example see man startx.

shruggy 05-13-2020 03:43 AM

Quote:

Originally Posted by ehartman (Post 6122349)
No, normally the argument "-" can only be used to replace one of the filenames in the commandline

Normally, yes, but not always so:
Code:

$ seq 12|paste - - -
1        2        3
4        5        6
7        8        9
10        11        12


chrism01 05-14-2020 12:20 AM

... and as a bit of trivia,

Code:

# This logs you in as 'user', BUT you retain your orig env....
su user


# This logs you in as 'user', AND you get their env; iow exactly as if you had logged in as them from the start
su - user



All times are GMT -5. The time now is 02:31 AM.