2

I'm having trouble inputting random generated passwords into some scripts.

I generate the password, and paste it between "". But sometimes it contains characters that sends the whole thing crashing down, like " # or whatever.

EDIT: I have been pointed to Quoting for a solution

There are several options for Quoting ( using \ , " " and ' ' ) but each require checking the string for special characters, like single quotes in a single quote block. Is there a way to really paste a string without needing to check it or is doing that check always necessary when your random string can contain the ' character at least.

metichi
  • 897
  • So is there no way to just paste a random string of characters and be treated as such without knowing wether there are " or ' or \ characters in it? You always need to check for those and mask them? – metichi May 29 '19 at 10:43
  • Excellent edit! I wanted to ask you for it, but then realized it was really me who didn’t read thoroughly enough. Now it’s very clear and I hope my answer helps. :) – dessert May 29 '19 at 11:09
  • It’s been some time, did you resolve the issue? Does my answer help? Is there anything I can do to improve it? – dessert Aug 10 '19 at 11:11

1 Answers1

2

You can use a Here document:

This type of redirection instructs the shell to read input from the current source until a line containing only delimiter (with no trailing blanks) is seen. (…) If any part of word is quoted, the delimiter is the result of quote removal on word, and the lines in the here-document are not expanded.

The delimiter (here “EOF”) has to occur on a single line to end the here document and it needs to be quoted in the first line, for example

$ cat <<"EOF"
a string \ with ' st$(echo)ra"nge chars?
EOF

gives you the literal:

a string \ with ' st$(echo)ra"nge chars?

To use the string in a script, adjust it to read from stdin and replace cat with the script’s path in the example above.

Note that the occurence of the delimiter string inside the password is not a problem, as the delimiter has to be alone on the line. The only way this does not work is if the delimiter matches exactly the password string, and as most passwords are created with certain requirements (e.g. “at least one number”) it should be easy to choose a delimiter that simply doesn’t meet one of the requirements. One could use “123456”, with that password failure is expected.

dessert
  • 39,982