LinuxQuestions.org
Review your favorite Linux distribution.
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 05-09-2020, 09:53 PM   #1
Skaperen
Senior Member
 
Registered: May 2009
Location: center of singularity
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386)
Posts: 2,684
Blog Entries: 31

Rep: Reputation: 176Reputation: 176
argument - vs --


on a command, if the argument "--" means end of options and only parse for regular arguments, then what can "-" mean?
 
Old 05-09-2020, 10:05 PM   #2
ehartman
Senior Member
 
Registered: Jul 2007
Location: Delft, The Netherlands
Distribution: Slackware
Posts: 1,674

Rep: Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888
Quote:
Originally Posted by Skaperen View Post
then what can "-" mean?
Often in programs with more then 1 input file: this argument should be taken as stdin instead of a file.

Last edited by ehartman; 05-11-2020 at 05:20 PM.
 
1 members found this post helpful.
Old 05-11-2020, 04:51 PM   #3
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,808

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
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.
 
Old 05-12-2020, 08:06 PM   #4
Skaperen
Senior Member
 
Registered: May 2009
Location: center of singularity
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386)
Posts: 2,684

Original Poster
Blog Entries: 31

Rep: Reputation: 176Reputation: 176
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.
 
Old 05-12-2020, 08:49 PM   #5
ehartman
Senior Member
 
Registered: Jul 2007
Location: Delft, The Netherlands
Distribution: Slackware
Posts: 1,674

Rep: Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888
Quote:
Originally Posted by Skaperen View Post
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.

Last edited by ehartman; 05-12-2020 at 08:50 PM.
 
Old 05-12-2020, 10:00 PM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,873
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
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
 
Old 05-13-2020, 01:43 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,966

Rep: Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332
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.
 
Old 05-13-2020, 03:43 AM   #8
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,677

Rep: Reputation: Disabled
Quote:
Originally Posted by ehartman View Post
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

Last edited by shruggy; 05-13-2020 at 11:10 AM.
 
1 members found this post helpful.
Old 05-14-2020, 12:20 AM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,364

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
... 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
 
  


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] iptables bad argument Bad argument `5060' treschen Linux - Newbie 3 11-10-2012 09:29 AM
[SOLVED] shell scripting: value of argument dissapears depending on argument order akelder Programming 5 03-21-2011 11:27 PM
message sending failed : Error[22 ] invalid argument .....but each and every argument rakeshranjanjha Linux - Software 2 01-07-2008 11:22 PM
couldn't attach tty to PPP unit 0: Invalid argument TheGrimJester Linux - Networking 3 07-28-2001 05:54 PM
invalid numeric argument Copenhagen Cowboy Linux - General 4 03-29-2001 02:01 PM

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

All times are GMT -5. The time now is 11:22 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