Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Newline characters in password from curl #6

Open
taylor-madeak opened this issue Jul 21, 2017 · 0 comments
Open

Newline characters in password from curl #6

taylor-madeak opened this issue Jul 21, 2017 · 0 comments

Comments

@taylor-madeak
Copy link

Opening this as an issue because a pull request with my current repo would include too many other changes.

ISSUE
When using curl | xpath | awk to retrieve the LAPS password from the API, the resulting value contains multiple leading newline characters.

Example:

udid=$(/usr/sbin/system_profiler SPHardwareDataType | /usr/bin/awk '/Hardware UUID:/ { print $3 }')
extAttName="\"LAPS\""
oldPass=$(/usr/bin/curl -skfu "$apiUser":"$apiPass" -H "Accept: application/xml" "$apiURL/JSSResource/computers/udid/$udid/subset/extension_attributes" | /usr/bin/xpath "//extension_attribute[name=$extAttName]" 2>&1 | awk -F"<value>|</value>" '{ print $2 }')

echo "==$oldPass=="

Result:

==

aXyl47ctwer6==

This will work most of the time if you don't quote the variable when using it because bash will interpret it as "split using $IFS," but will break as soon as $IFS changes. However, proper syntax calls for quoting to prevent splitting and globbing, and that will break this method every time.

SOLUTION
Trim the whitespace characters off before using the variable. This can be accomplished a number of ways, I chose to use bash parameter expansion syntax.

Example using the variable set with curl above:

oldPass=${oldPass//[$'\t\r\n ']}
echo "==$oldPass=="

Result:
==aXyl47ctwer6==

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant