3

I have a bash script to download data from a website. For this I need to pass the username and password using a java client for the website. My password has a special character that is causing problems. I have tried the usual solutions but none are working.

#!/bin/sh
#$ -cwd

MYPASSWORD='password&123'

java -jar client.jar -p username $MYPASSWORD file.gz

Tried using \ either directly in the command or using the variable. But that didnt work. Login failed!

MYPASSWORD=password\&123
java -jar client.jar -p username $MYPASSWORD file.gz

Tried "" or ''. Login failure.

MYPASSWORD='password&123'
java -jar client.jar -p username "\"$MYPASSWORD\"" file.gz

Also tried it, no luck. MYPASSWORD='password'"&"'123'

Any suggestions would be most helpful. Resetting the password on this website also seems problematic.

user19758
  • 131
  • 1
  • 5

1 Answers1

4

Just quote the parameter.

mypassword='password&123'
java -jar client.jar -p username "$mypassword" file.gz

The quotes are only there to keep the shell from performing any further processing of the value of mypassword after the parameter is expanded. You don't actually pass quotes to your program.

chepner
  • 497,756
  • 71
  • 530
  • 681
  • 1
    Then the problem isn't with the shell; you've either mistyped the password, or the Java program is mishandling it internally. – chepner Nov 08 '16 at 17:13
  • (The only other case would be if your password contains a single quote, which can't be included in a single-quoted string: `foo='bob'spassword'` doesn't set `foo` to `bob'spass'word`, but to the concatenation of the quoted string `bob` and the unquoted string `spass`, and the unquoted string `word`). – chepner Nov 08 '16 at 17:15
  • Maybe because your shebang is `/bin/sh` instead of `/bin/bash`. – SLePort Nov 08 '16 at 17:16
  • @Kenavoz No. The code I posted contains no `bash` extensions. – chepner Nov 08 '16 at 17:17
  • I am sure the password is correct since I can login interactively and that doesnt have a problem. I just need to run 400 jobs and so cant run it in interactive mode. Also no single quotes in the password. And have tried both /bin/sh instead of /bin/bash and removed it entirely too. Could be a problem due to java...any suggestions how to pass special chars in java? – user19758 Nov 08 '16 at 17:21
  • 1
    The special characters *are* passed to Java. The question is, what does your Java program do with that data? (Rhetorical question; you'll have to post a new question if you want to dig into your Java code.) – chepner Nov 08 '16 at 17:26
  • 1
    How did you determine that the problem is a special character and not general invocation? Are you sure `-p username password filename` is the correct syntax? That it's not `-p username@password filename` or `-u username -p password` or anything like that? Also, check your script for carriage returns. – that other guy Nov 08 '16 at 17:35
  • The download, format, password etc work interactively and so I dont think thats the problem. @chepner: Its not my java problem but an executable provided by the download website and so cant really debug that. Could def. be the source of the problem. – user19758 Nov 08 '16 at 19:46