Python Forum
option -c and semicolon not working as expected
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
option -c and semicolon not working as expected
#1
it was my understanding that a ";" character could be used to separate lines in the string given to the -c option of the python command. but i am finding that in many cases it does not work. a newline byte seems to work as encoded by bash in the 2nd example in this output.
Output:
lt1a/forums/3 /home/forums 4> /usr/bin/python3.8 -c 'for x in range(4):; print(x)' File "<string>", line 1 for x in range(4):; print(x) ^ SyntaxError: invalid syntax lt1a/forums/3 /home/forums 5> /usr/bin/python3.8 -c $'for x in range(4):\n print(x)' 0 1 2 3 lt1a/forums/3 /home/forums 6>
can someone tell me what i should be using for this case that the 1st example shows?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
(Aug-11-2022, 10:04 PM)Skaperen Wrote: can someone tell me what i should be using for this case that the 1st example shows?
Instead of the -c option, you can use a here document in bash
Output:
$ python <<EOF > for i in range(4): > print(i) > EOF 0 1 2 3 $
Reply
#3
i would like to execute the command in other ways, too. and what if the intent of the -c string is to read from stdin? the real point is, that i have seen people using -c and ";" and it works for them, even when i C&P their command into my shell. it's when i make my own it fails.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
(Aug-12-2022, 05:35 PM)Skaperen Wrote: the real point is, that i have seen people using -c and ";" and it works for them
The semicolon exists in Python's syntax too, which may be the reason why it works for them:
>>> for i in range(4): print(i); print('foo', i)
... 
0
foo 0
1
foo 1
2
foo 2
3
foo 3
>>> 
Reply
#5
any idea why it fails in my output in post #1?

edit:

copy & paste that one and it works. so it depends on who coded it?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
(Aug-12-2022, 09:51 PM)Skaperen Wrote: any idea why it fails in my output in post #1?
The ';' just after the ':' doesn't look right.

Also the semicolon does not separate lines in Python. It separates simple statements.
Reply
#7
experimenting more instead of trying to find subtle rules in the language reference i found that ";" fails when it immediately follows ":" even though a newline can be there.

edit:

Output:
lt1a/forums/3 /home/forums 6> python3.8 -c 'for x in range(4):; print(x)' File "<string>", line 1 for x in range(4):; print(x) ^ SyntaxError: invalid syntax lt1a/forums/3 /home/forums 7> python3.8 -c 'for x in range(4): pass;print(x)' 0 1 2 3 lt1a/forums/3 /home/forums 8>
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020