2

My python script can take a series of bitwise operators as one of its arguments. They all work fine except for "=<<" which is roll left, and "=>>" which is roll right. I run my script like ./script.py -b +4,-4,=>>10,=<<1, where anything after -b can be any combination of similar operations. As soon as the terminal sees "<<" though, it just drops the cursor to a new line after the command and asks for more input instead of running the script. When it sees ">>", my script doesn't process the arguments correctly. I know it's because bash uses these characters for a specific purpose, but I'd like to get around it while still using "=>>" and "=<<" in my arguments for my script. Is there any way to do it without enclosing the argument in quotation marks?

Thank you for your help.

2
  • cat | ./script.py -b ?
    – ChatterOne
    Commented Nov 2, 2020 at 7:30
  • A shell command is necessarily written in the shell language, and in the shell language an unquoted >> means append output to a file, and << means read input from a here-document. You cannot make your command have its own custom syntax; it's a shell command, and therefore subject to shell syntax. Commented Nov 2, 2020 at 8:45

1 Answer 1

1

You should enclose the parameters that contain special symbols into single quotation marks (here, echo represents your script):

> echo '+4,-4,=>>10,=<<1'
+4,-4,=>>10,=<<1

Alternatively, save the parameters to a file (say, params.txt) and read them from the file onto the command line using the backticks:

> echo `cat params.txt`
+4,-4,=>>10,=<<1

Lastly, you can escape some offending symbols:

> echo +4,-4,=\>\>10,=\<\<1
+4,-4,=>>10,=<<1
6
  • And also maybe remove the single quotation marks when reading the parameter from the script. Or maybe echo '+4,-4,=>>10,=<<1' |./script.py -b
    – ChatterOne
    Commented Nov 2, 2020 at 7:32
  • @ChatterOne I am not sure what you mean. Nothing is read from a script.
    – DYZ
    Commented Nov 2, 2020 at 7:34
  • I mean that the python script (the ./script.py ) will read the parameter as literally '+4,-4,=>>10,=<<1', so when using that value, the quotation marks will need to be removed first
    – ChatterOne
    Commented Nov 2, 2020 at 7:38
  • @ChatterOne That would be an answer to a different question, and surely not related to my answer.
    – DYZ
    Commented Nov 2, 2020 at 7:39
  • 1
    @ChatterOne This is not true. The quotes will not be seen by the script. Have you tried?
    – DYZ
    Commented Nov 2, 2020 at 7:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.