Is Rule 7 of POSIX shell grammar rules written correctly?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Is Rule 7 of POSIX shell grammar rules written correctly?
# 1  
Old 08-04-2016
Is Rule 7 of POSIX shell grammar rules written correctly?

The POSIX shell standard grammar rules are at

Shell Command Language

I am trying to understand Rule 7 and I don't. I think there may be some mistakes there. I am not complaining about the standard; rather, I am concerned that my perception is wrong, and I don't understand something important.

Here are my questions. Please tell me for each question, if the standard is simply written incorrectly, or what is it that I don't understand.

1. Rule 7 is only referenced with yacc symbols cmd_name and cmd_word, not with any symbols that have to do with assignments, yet it is labeled "Assignment preceding command name". Is that a misleading label ?

2. Rule 7a for the first word, in some cases refers to Rule 7b, which is labeled "not the first word". Is that a misleading label ?

3. The yacc symbol cmd_word refers to rule 7b, which says, if the word contains '=' after its first character, then it is an assignment - and not anything to do with a command. Should rule 7b simply state that it is not allowed to have '=' after the first character?

4. Rule 7a is only referenced during parsing simple commands, which cannot have any reserved words in them. Yet Rule 7a in some cases refers to Rule 1, which differentiates between a reserved word and ordinary word.

If there is no possibility of having a reserved word in Rule 7a, there would be no point to refer to any additional Rule 1 at all. Then, there would be no point in having a separate Rule 7a at all. That is because, if the word did not contain '=', Rule 7b says nothing, so we have WORD, and if it did contain '=', then 7b applies. Thus we would only need 7b. Then also, there would be no need for cmd_name symbol, we could just have cmd_word.

Is Rule 7a and cmd_name symbol needed?

Moderator's Comments:
Mod Comment edit by bakunin: made the link working.
# 2  
Old 08-04-2016
Quote:
Originally Posted by Mark_Galeck
2. Rule 7a for the first word, in some cases refers to Rule 7b, which is labeled "not the first word". Is that a misleading label ?
No. Consider the following: you have an executable called "foo=bar", which is a valid name for a file. What would these lines do:

Code:
# cat foo=bar
echo "hello world"

# ./foo=bar
??

# typeset foo=bar
??

# x=$(foo=bar)
??

# foo=bar
??

Would it assign the value "bar" to a variable "foo" or would it execute the command foo=bar?

Quote:
Originally Posted by Mark_Galeck
3. The yacc symbol cmd_word refers to rule 7b, which says, if the word contains '=' after its first character, then it is an assignment - and not anything to do with a command. Should rule 7b simply state that it is not allowed to have '=' after the first character?

4. Rule 7a is only referenced during parsing simple commands, which cannot have any reserved words in them. Yet Rule 7a in some cases refers to Rule 1, which differentiates between a reserved word and ordinary word.
And all this deals with similar situations. That may seem pretty picky, but when designing a language you cannot assume that one only uses things that make sense at first glance.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 3  
Old 08-04-2016
In addition to what bakunin has already said, rule 7 applies in cases like:
  1. IFS=, for i in abc,def,chi,jul;do echo "$i";done which generates a syntax error because for is not recognized as a keyword because it is not the 1st word in the command,
  2. IFS=, PS2='Enter continuation line: ' read var1 var2 var3 forces the read command to be invoked with values for the environment variables IFS and PS2 that apply only during the execution of the read command (without changing the values of those variables in the current shell execution environment), and
  3. =abc read something tries to run a command named =abc with the operands read and something rather than attempting to set the variable with no name to the string abc in the environment of the command read invoked with the operand something.
# 4  
Old 08-05-2016
Thank you bakunin for your reply, but your reply is not relevant to my question. I spent a lot of time writing the question and I guess you did not have time to read it as carefully as I had wrote it.

However, your reply did help, in that, while thinking whether it is relevant or not, I finally understood the whole thing.

For the benefit of newbies, I will post my understanding and answers here:

1 and 2. Yes. Of course these labels are misleading, and make the grammar hard to understand for newbies.

3. No. Instead, it is best to apply Rule 7b also to the 2 productions in the grammar, that reduce cmd_prefix to ASSIGNMENT_WORD.

4. No. I misunderstood here how the "rules" are applied. I thought, you first perform the reduction, then somehow apply the rule (which makes no sense). Instead, you use the rule first, in order to allow the reduction.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Equivalent to let command in POSIX shell

Hi all, I am learning POSIX shell programming, and the book I read, uses the let command for integer arithmetic. I have downloaded and use the shellcheck program on Linux. This programs says: In POSIX sh, 'let' is undefined. See the screenshot attached. What is the POSIX... (1 Reply)
Discussion started by: johnprogrammer
1 Replies

2. Shell Programming and Scripting

Pure POSIX shell scripting...

Hi all... This is more of a concensus question than help... As many of you know I am experimenting with the limitations of Pure POSIX shell scripting. Q: Is the directory /bin considered part of the Pure POSIX shell or must I stick entirely with the builtins only? The reason is I... (2 Replies)
Discussion started by: wisecracker
2 Replies

3. Shell Programming and Scripting

Shell grammar question: logical OR in test

Hi, I am trying to check if two input files exist before the rest of the scripts is run. Following is the code that I have but it gives me syntax error. if then echo "File not found" else echo "File found" fi (3 Replies)
Discussion started by: nua7
3 Replies

4. Programming

How could I interact with shell script from webportal written in php?

Hello, I am new on PHP scripting .I have shell scripts which I an running currently from linux server but now I want to make a web portal from where I will run all my scripts but the problem is all my scripts ask for parameters so I am getting confused how could I run my shell script from web... (2 Replies)
Discussion started by: anuragpgtgerman
2 Replies

5. Solaris

Jumpstart -- Warning: Could not find matching rule in rules.ok

I just setup a new jumpstart server, and I'm having problems with rules.ok errors. I'm coming up blank after many Google searches, forum searches, etc..... This is the error I receive: Skipped interface e1000g1 Attempting to configure interface e1000g0... Configured interface e1000g0... (0 Replies)
Discussion started by: christr
0 Replies

6. UNIX for Dummies Questions & Answers

can a korn shell script be written on HP-UNIX?

Is HP UNIX and Korn different flavours of unix? can a korn shell script be written on HP Unix? (4 Replies)
Discussion started by: soujanya_srk
4 Replies

7. Shell Programming and Scripting

what's the largest shell script you've ever written?

I've just written a test harness using ksh. One of scripts weighed in at 1206 lines. I came across Eric Raymonds 'Art of Unix programming' on line and he advises against excessively large scripts. I am very interested to hear people's opinions on this and any experiences they have had. (2 Replies)
Discussion started by: OFFSIHR
2 Replies

8. Shell Programming and Scripting

Simple shell script to be written

There are 2 shell script files sh1 and sh2. sh2 contains records such as 01 02 03 on different lines. In sh1, I would like to read each record of sh2 till end of file, concatenate them with a comma(,) and store in a variable. Can somebody please help me with the sh1 script? Thanks in... (6 Replies)
Discussion started by: shsandeep
6 Replies

9. UNIX for Dummies Questions & Answers

Simple shell script to be written

There are 2 shell script files sh1 and sh2. sh2 contains records such as 01 02 03 on different lines. In sh1, I would like to read each record of sh2 till end of file, concatenate them with a comma(,) and store in a variable. Can somebody please help me with the sh1 script? Thanks in... (2 Replies)
Discussion started by: shsandeep
2 Replies
Login or Register to Ask a Question