Search code examples
pythonshellparameter-passingcsh

Problem with reading in parameters with special characters in Python


I have a scripts (a.py) reads in 2 parameters like this:-

#!/usr/bin/env python

import sys
username = sys.argv[1]
password = sys.argv[2]

Problem is, when I call the script with some special characters:-

a.py   "Lionel"   "my*password"

It gives me this error:-

/swdev/tools/python/current/linux64/bin/python: No match.

Any workaround for this?


Solution

  • The problem is in the commands you're actually using, which are not the same as the commands you've shown us. Evidence: in Perl, the first two command-line arguments are $ARGV[0] and $ARGV[1] (the command name is $0). The Perl script you showed us wouldn't produce the output you showed us.

    "No match" is a shell error message.

    Copy-and-paste (don't re-type) the exact contents of your Python script, the exact command line you used to invoke it, and the exact output you got.

    Some more things to watch out for:

    You're invoking the script as a.py, which implies either that you're copying it to some directory in your $PATH, or that . is in your $PATH. If the latter, that's a bad idea; consider what happens if you cd info a directory that contains a (possibly malicious) command called ls. Putting . at the end of your $PATH is safer than putting it at the beginning, but I still recommend leaving it out altogether and using ./command to invoke commands in the current directory. In any case, for purposes of this exercise, please use ./a.py rather than a.py, just so we can be sure you're not picking up another a.py from elsewhere in your $PATH.

    This is a long shot, but check whether you have any files in your current directory with a * character in their names. some_command asd*123 (without quotation marks) will fail if there are no matching files, but not if there happens to be a file whose name is literally "asd*123".

    Another thing to try: change your Python script as follows:

    #!/usr/bin/env python
    
    print "before import sys"
    
    import sys
    
    print "after import sys"
    
    username = sys.argv[1]
    password = sys.argv[2]
    

    This will tell you whether the shell is invoking your script at all.