Do comparison without triggering command with the same name - sh

So I'm trying to do a comparison and it just happens to be that the value I'm checking for has the same name as a command on the computer with is a problem.
This is the command i run.
if $current_branch != "HEAD"; then echo '1'; fi;
And here is the result as you can see the HEAD program ran.
400 URL must be absolute
Content-Type: text/plain
Client-Date: Thu, 01 Mar 2018 16:45:48 GMT
Client-Warning: Internal response
200 OK
Cache-Control: no-store, no-cache, must-revalidate, max-age=0
Connection: keep-alive
Date: Thu, 1 Mar 2018 16:45:49 GMT
Server: PWS/8.3.1.0.8
Vary: Accept-Encoding
Content-Type: text/html; charset=utf-8
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Access-Control-Allow-Origin: https://www.head.com
Client-Date: Thu, 01 Mar 2018 16:45:49 GMT
Client-Peer: 151.249.91.221:443
Client-Response-Num: 1
Client-SSL-Cert-Issuer: /C=US/O=DigiCert Inc/OU=www.digicert.com/CN=DigiCert SHA2 High Assurance Server CA
Client-SSL-Cert-Subject: /C=US/ST=California/L=Campbell/O=CDNetworks Inc./CN=support2.cdnetworks.net
Client-SSL-Cipher: ECDHE-RSA-AES256-SHA
Client-SSL-Socket-Class: IO::Socket::SSL
Content-Security-Policy: frame-ancestors 'self' https://head.testing-varnish.symmetrics.de
Set-Cookie: geoip=IC; expires=Sat, 31-Mar-2018 16:45:49 GMT; Max-Age=2592000; path=/; domain=head.com
Set-Cookie: lang=en; expires=Sat, 31-Mar-2018 16:45:49 GMT; Max-Age=2592000; path=/; domain=head.com
Set-Cookie: currentLangId=1; expires=Sat, 31-Mar-2018 16:45:49 GMT; Max-Age=2592000; path=/; domain=head.com
Set-Cookie: root=1; expires=Sat, 31-Mar-2018 16:45:49 GMT; Max-Age=2592000; path=/; domain=head.com
X-Frame-Options: SAMEORIGIN
X-Px: nc h0-s42.p1-arn ( h0-s4012.p6-lhr>CONN), nc h0-s4012.p6-lhr ( h0-s4102.p11-fra), nc h0-s4102.p11-fra ( origin)
So how do i avoid this from happening and not have command collide with the string value i want to compare?

The argument to if is always a command. You are probably looking for the special command [ (aka test) which compares strings.
if [ "$current_branch" != "HEAD" ]; then
echo '1'
fi
Take note of the requirement to have spaces on both sides of the [ and ] tokens, and the requirement to quote strings which might contain shell metacharacters.
Depending on whether you require pre-POSIX compatibility, you might also want to invert the negation. On the other hand, in modern shells like Bash and Ksh, you probably want to use the replacement [[ which is somewhat more robust and versatile.
The requirement to have a terminating ] for symmetry is purely aesthetic, but still a requirement. It's pretty quirky that the original Bourne shell didn't have string comparison features built into the shell itself, but, well, this is what we ended up with.
As an aside, if your example is representative of your actual code, you might want to use the somewhat more parsimonious shorthand syntax
[ "$current_branch" = "HEAD" ] || echo '1'

Related

Invoke-WebRequest returning unexpected results for some addresses

I seem to be having issues with certain addresses when trying to process with Invoke-WebRequest
The address below is one example of a few that I have come across.
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
(Invoke-WebRequest https://s3.amazonaws.com/lists.disconnect.me/simple_ad.txt).Content
(Invoke-WebRequest https://s3.amazonaws.com/lists.disconnect.me/simple_ad.txt).RawContent
The Content property seems to only return an Array of numbers. RawContent displays a result closer to what I would expect.
What is the reason for this?
Content
35
32
65
100
32
102
105
108
116
101
RawContent
HTTP/1.1 200 OK
x-amz-id-2: juI0bRXZacf19D5/sjmsw53XudBjlbSYtO0XKVQP0N+dWXvixKoWs8mV8oF23SLt8iGjaE/CKco=
x-amz-request-id: 5B68B280AD366CDE
Accept-Ranges: bytes
Content-Length: 43642
Content-Type:
Date: Fri, 13 Jul 2018 23:25:09 GMT
ETag: "0a43bd5ebd9b3e2ee08a205d163cb0ac"
Last-Modified: Wed, 09 Mar 2016 19:46:05 GMT
Server: AmazonS3
# Ad filter list by Disconnect
# License: GPLv3
# Contact: support [at] disconnect.me
htmlcontent
If you look at the headers, the Content Type is empty, therefore Invoke-WebRequest doesn't know what it's dealing with and treats the data as binary data (byte array).
(Invoke-WebRequest https://s3.amazonaws.com/lists.disconnect.me/simple_ad.txt).Headers
Key Value
--- -----
Accept-Ranges bytes
Content-Length 43642
Content-Type
Date Sat, 14 Jul 2018 02:28:36 GMT
ETag "0a43bd5ebd9b3e2ee08a205d163cb0ac"
Last-Modified Wed, 09 Mar 2016 19:46:05 GMT
Server AmazonS3
Whilst for example calling Invoke-WebRequest on this question:
(Invoke-WebRequest https://stackoverflow.com/questions/51333965/invoke-webrequest-returning-unexpecte
d-results-for-some-addresses).Headers
Key Value
--- -----
X-Frame-Options SAMEORIGIN
X-Request-Guid 9366c316-b9b3-467b-8911-b18bb4752e1c
Strict-Transport-Security max-age=15552000
Content-Security-Policy upgrade-insecure-requests
Age 0,0
Accept-Ranges bytes,bytes
Content-Length 108783
Cache-Control private
Content-Type text/html; charset=utf-8
…
EDIT: If you want to overrule the content-type you could do something like this:
$url = 'https://s3.amazonaws.com/lists.disconnect.me/simple_ad.txt'
[System.Net.HttpWebRequest]$req = [System.Net.WebRequest]::Create($url) -as [System.Net.HttpWebRequest]
[System.Net.HttpWebResponse]$resp = $req.getResponse()
$respStream = $resp.getResponseStream()
$sr = new-object IO.StreamReader($respStream)
$result = $sr.ReadToEnd()
$resp.Close()

What could cause a 302 redirect?

I have a wordpress site and I have a blog. When I try to share my blog post on FB http://videoefekt.cz/?p=472 , it shows the thumb of my homepage. I tried to change permalinks to clear, deactivate plugins and chceck .htaccess file, everything looks fine, but the FB sharing debugger shows I have 302 HTTP Redirect to my homepage, can anyone tell me what could cause this problem ?
Simulate the facebook crawler access to your site and it made redirect to home page when using ipv6 address. Possibly you have misconfig when the request is from ipv6 address.
curl -H "User-Agent: facebookexternalhit/1.1" "http://videoefekt.cz/?p=472" -vvvv -6
* About to connect() to videoefekt.cz port 80 (#0)
* Trying 2a02:2b88:1:4::a6...
* Connected to videoefekt.cz (2a02:2b88:1:4::a6) port 80 (#0)
> GET /?p=472 HTTP/1.1
> Host: videoefekt.cz
> Accept: */*
> User-Agent: facebookexternalhit/1.1
>
< HTTP/1.1 302 Found
< Date: Thu, 04 Jan 2018 04:16:32 GMT
< Server: Apache
< Pragma: no-cache
< Expires: Wed, 11 Jan 1984 05:00:00 GMT
< Cache-Control: no-cache, must-revalidate, max-age=0
< Link: <http://videoefekt.cz/wp-json/>; rel="https://api.w.org/"
< Set-Cookie: _icl_current_language=cs; expires=Fri, 05-Jan-2018 04:16:33 GMT; Max-Age=86400; path=/
< Set-Cookie: wfvt_3708104896=5a4daaa1e2090; expires=Thu, 04-Jan-2018 04:46:33 GMT; Max-Age=1800; path=/; HttpOnly
< Set-Cookie: PHPSESSID=8a1a1d9247ac9d9707b616c6c0ad2ccc; path=/
< Upgrade: h2,h2c
< Connection: Upgrade
< Location: http://videoefekt.cz/
< Vary: User-Agent
< Content-Length: 0
< Content-Type: text/html; charset=UTF-8
<
* Connection #0 to host videoefekt.cz left intact

Empty response from Bitstamp.net

I am trying to use the private api from Bitstamp to query for my account's balance. I believe I am posting the right data as no error is returned but Bitstamp's answer is always empty
* Trying 107.154.249.133...
* Connected to www.bitstamp.net (107.154.249.133) port 443 (#0)
* ALPN, offering http/1.1
* Cipher selection:
ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:#STRENGTH
* successfully set certificate verify locations:
* CAfile: /etc/ssl/certs/ca-certificates.crt
CApath: /etc/ssl/certs
* NPN, negotiated HTTP1.1
* SSL connection using TLSv1.2 / ECDHE-RSA-AES256-GCM-SHA384
* ALPN, server did not agree to a protocol
* Server certificate:
* subject: OU=Domain Control Validated; CN=*.bitstamp.net
* start date: Apr 8 14:11:39 2015 GMT
* expire date: Apr 8 15:48:13 2018 GMT
* subjectAltName: www.bitstamp.net matched
* issuer: C=US; ST=Arizona; L=Scottsdale; O=GoDaddy.com, Inc.;
OU=http://certs.godaddy.com/repository/; CN=Go Daddy Secure Certificate
Authority - G2
* SSL certificate verify ok.
> POST /api/v2/balance HTTP/1.1
Host: www.bitstamp.net
User-Agent: Bitstamp C++ API Client
Accept: */*
Content-Length: 134
Content-Type: application/x-www-form-urlencoded
* upload completely sent off: 134 out of 134 bytes
< HTTP/1.1 301 MOVED PERMANENTLY
< Access-Control-Allow-Headers: x-requested-with, Content-Type, origin, accept, cache-control
< Access-Control-Allow-Methods: POST, GET
< Access-Control-Allow-Origin: *
< Content-Language: en
< Content-Type: text/html; charset=utf-8
< Date: Fri, 08 Sep 2017 17:46:41 GMT
< Location: http://www.bitstamp.net/api/v2/balance/
< Server: Apache
< Strict-Transport-Security: max-age=63072000; includeSubDomains
< Vary: Accept-Language
< X-Frame-Options: SAMEORIGIN
< Content-Length: 0
< Connection: keep-alive
< Set-Cookie: visid_incap_99025=wHhR5uPzT92ReB9XbeNToIHXslkAAAAAQUIPAAAAAAAbNQJEog5ZojcqdOv0dFhd; expires=Sat, 08 Sep 2018 10:32:13 GMT; path=/; Domain=.bitstamp.net
< Set-Cookie: nlbi_99025=VyBXXwUOsQHckThX8F1n9AAAAABOW3tLZ0nUHyp47yinIimv; path=/; Domain=.bitstamp.net
< Set-Cookie: incap_ses_569_99025=04PeXe3KGFAAVw//8n7lB4HXslkAAAAA09BkGnsaiskl2dkFxzhFXQ==; path=/; Domain=.bitstamp.net
< X-Iinfo: 9-15073063-15060114 PNNN RT(1504892800858 31) q(0 0 0 -1) r(2 2) U6
< X-CDN: Incapsula
<
* Connection #0 to host www.bitstamp.net left intact
result: 0
response:
terminate called after throwing an instance of 'std::runtime_error'
what(): Bitstamp response doesn't contain result data
Any idea ?
Thanks a lot in advance !
Initially I had the balance path to be /balance, but after changing to /balance/ it worked and got 200 back with payload.
So instead of http://www.bitstamp.net/api/v2/balance use http://www.bitstamp.net/api/v2/balance/, note the trailing backslash

How do I tell spray-json on Akka HTTP to "assume application/json"?

Using spray-json on an Akka HTTP server; how can I "assume Content-type: application/json" for requests when not supplied?
Issue
curl localhost:12345/request -d'{"sample":"json"}'
doesn't work unless I add
-H "Content-type: application/json"
explicitly.
Why I ask
not ideal for showing succint examples on a README.md
annoying to add the header manually all the time while debugging
You should have defined you API to accept content-type json only, thats why it does not accept other content-types.
Just doing curl -XPOST -d 'content' URL will post your content as application/x-www-form-urlencoded (default behaviour).
See --verbose of curl POST, see Content-Type in example below
$ curl -XPOST -d '{"test" : "testValue"}' "https://jsonplaceholder.typicode.com/posts" -v
* Trying 104.31.87.157...
* Connected to jsonplaceholder.typicode.com (104.31.87.157) port 443 (#0)
* TLS 1.2 connection using TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
* Server certificate: sni233425.cloudflaressl.com
* Server certificate: COMODO ECC Domain Validation Secure Server CA 2
* Server certificate: COMODO ECC Certification Authority
* Server certificate: AddTrust External CA Root
> POST /posts HTTP/1.1
> Host: jsonplaceholder.typicode.com
> User-Agent: curl/7.43.0
> Accept: */*
> Content-Length: 22
> Content-Type: application/x-www-form-urlencoded
>
* upload completely sent off: 22 out of 22 bytes
< HTTP/1.1 201 Created
< Date: Sat, 18 Mar 2017 04:54:41 GMT
< Content-Type: application/json; charset=utf-8
< Content-Length: 51
< Connection: keep-alive
< Set-Cookie: __cfduid=d91d0ebbaa5f7b40a998d327ffce2b5671489812881; expires=Sun, 18-Mar-18 04:54:41 GMT; path=/; domain=.typicode.com; HttpOnly
< X-Powered-By: Express
< Vary: Origin, X-HTTP-Method-Override, Accept-Encoding
< Access-Control-Allow-Credentials: true
< Cache-Control: no-cache
< Pragma: no-cache
< Expires: -1
< X-Content-Type-Options: nosniff
< Etag: W/"33-cwlRczABGtsoiZUP8lw9WSIt684"
< Via: 1.1 vegur
< Server: cloudflare-nginx
< CF-RAY: 3415986bb9f50c35-SEA
<
{
"{\"test\" : \"testValue\"}": "",
"id": 101
* Connection #0 to host jsonplaceholder.typicode.com left intact
}
You have to change your client headers to match what the API server accepts.
$ curl -H "Content-type: application/json" -XPOST -d '{"test" : "testValue"}' "https://jsonplaceholder.typicode.com/posts" -v
* Trying 104.31.87.157...
* Connected to jsonplaceholder.typicode.com (104.31.87.157) port 443 (#0)
* TLS 1.2 connection using TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
* Server certificate: sni233425.cloudflaressl.com
* Server certificate: COMODO ECC Domain Validation Secure Server CA 2
* Server certificate: COMODO ECC Certification Authority
* Server certificate: AddTrust External CA Root
> POST /posts HTTP/1.1
> Host: jsonplaceholder.typicode.com
> User-Agent: curl/7.43.0
> Accept: */*
> Content-type: application/json
> Content-Length: 22
>
* upload completely sent off: 22 out of 22 bytes
< HTTP/1.1 201 Created
< Date: Sat, 18 Mar 2017 04:53:22 GMT
< Content-Type: application/json; charset=utf-8
< Content-Length: 38
< Connection: keep-alive
< Set-Cookie: __cfduid=d8e90de27fc1fa87fb42b763a4199ba7d1489812801; expires=Sun, 18-Mar-18 04:53:21 GMT; path=/; domain=.typicode.com; HttpOnly
< X-Powered-By: Express
< Vary: Origin, X-HTTP-Method-Override, Accept-Encoding
< Access-Control-Allow-Credentials: true
< Cache-Control: no-cache
< Pragma: no-cache
< Expires: -1
< X-Content-Type-Options: nosniff
< Etag: W/"26-Zx6VPi+rfbM5YFlBzT2pzGEHcgg"
< Via: 1.1 vegur
< Server: cloudflare-nginx
< CF-RAY: 3415967b2af12a19-SEA
<
{
"test": "testValue",
"id": 101
* Connection #0 to host jsonplaceholder.typicode.com left intact
}
You can change the content-type on you server, and change whatever content you receive to JSON by yourself (which might not a good idea though)

PayPal REST API: trying to activate a billing plan, but getting "HTTP/1.1 401 Unauthorized"

I am building an EC site, where my customers can pay through PayPal API. Now, I am learning how it works, using Sandbox; referring to this page: https://developer.paypal.com/docs/integration/direct/billing-plans/
I have succeeded the prerequisites, i.e., having created a PayPal app,
gotten an access token, and made an API call. And then, I have succeeded making a billing plan, copying-and-pasting the sample command on the page, just replacing the Access Token.
Now, I want to activate the billing plan, but couldn't have succeeded.
The command used is, once again, copied-and-pasted from the page, as below.
curl -v -k -X PATCH https://api.sandbox.paypal.com/v1/payments/billing-plans/P-7DC96732KA7763723UOPKETA/ \
-H 'X-PAYPAL-OAUTH-CONTEXT: {"consumer":{"accountNumber":1181198218909172527,"merchantId":"5KW8F2FXKX5HA"},"merchant":{"accountNumber":1659371090107732880,"merchantId":"2J6QB8YJQSJRJ"},"apiCaller":{"clientId":"AdtlNBDhgmQWi2xk6edqJVKklPFyDWxtyKuXuyVT-OgdnnKpAVsbKHgvqHHP","appId":"APP-6DV794347V142302B","payerId":"2J6QB8YJQSJRJ","accountNumber":"1659371090107732880"},"scopes":["https://api.paypal.com/v1/payments/.*","https://uri.paypal.com/services/payments/futurepayments","openid"]}' \
-H 'Content-Type: application/json' \
-d '[{
"op": "replace",
"path": "/",
"value": {
"state": "ACTIVE"
}
}]'
First, as instructed, I just replaced the Plan ID "P-7DC96732KA7763723UOPKETA" with mine, provided in the previous response for creating the plan.
* Trying 173.0.82.78...
* Connected to api.sandbox.paypal.com (173.0.82.78) port 443 (#0)
* TLS 1.2 connection using TLS_RSA_WITH_AES_256_CBC_SHA256
* Server certificate: api.sandbox.paypal.com
* Server certificate: Symantec Class 3 Secure Server CA - G4
* Server certificate: VeriSign Class 3 Public Primary Certification Authority - G5
> PATCH /v1/payments/billing-plans/P-8AX21799EN516221GJMBOECA/ HTTP/1.1
> Host: api.sandbox.paypal.com
> User-Agent: curl/7.43.0
> Accept: */*
> X-PAYPAL-OAUTH-CONTEXT: {"consumer":{"accountNumber":1181198218909172527,"merchantId":"5KW8F2FXKX5HA"},"merchant":{"accountNumber":1659371090107732880,"merchantId":"2J6QB8YJQSJRJ"},"apiCaller":{"clientId":"AdtlNBDhgmQWi2xk6edqJVKklPFyDWxtyKuXuyVT-OgdnnKpAVsbKHgvqHHP","appId":"APP-6DV794347V142302B","payerId":"2J6QB8YJQSJRJ","accountNumber":"1659371090107732880"},"scopes":["ttps://api.paypal.com/v1/payments/.*","ttps://uri.paypal.com/services/payments/futurepayments","openid"]}
> Content-Type: application/json
> Content-Length: 78
>
* upload completely sent off: 78 out of 78 bytes
< HTTP/1.1 401 Unauthorized
< Date: Fri, 17 Feb 2017 12:18:47 GMT
< Server: Apache
< paypal-debug-id: c46e304b4bb46
< Paypal-Debug-Id: c46e304b4bb46
< Set-Cookie: X-PP-SILOVER=name%3DSANDBOX3.API.1%26silo_version%3D1880%26app%3Dplatformapiserv%26TIME%3D669689432%26HTTP_X_PP_AZ_LOCATOR%3D; Expires=Fri, 17 Feb 2017 12:48:47 GMT; domain=.paypal.com; path=/; Secure; HttpOnly
< Set-Cookie: X-PP-SILOVER=; Expires=Thu, 01 Jan 1970 00:00:01 GMT
< Content-Length: 0
< Connection: close
< Content-Type: text/plain; charset=ISO-8859-1
<
* Closing connection 0
And I tried with some other parameter replacements, like the Client ID, but no luck.
Do I need to replace other parameters?
Or, have I done something wrong?
SOLVED.
On this page, I found simpler and more straightforward command
and made up below:
curl -v -X PATCH https://api.sandbox.paypal.com/v1/payments/billing-plans/<Plan ID>/ \
-H "Content-Type:application/json" \
-H "Authorization: Bearer <Access Token>" \
-d '[
{
"path":"/",
"value":{"state":"ACTIVE"},
"op":"replace"}
]'
Got the response as below:
* Trying 173.0.82.78...
* Connected to api.sandbox.paypal.com (173.0.82.78) port 443 (#0)
* TLS 1.2 connection using TLS_RSA_WITH_AES_256_CBC_SHA256
* Server certificate: api.sandbox.paypal.com
* Server certificate: Symantec Class 3 Secure Server CA - G4
* Server certificate: VeriSign Class 3 Public Primary Certification Authority - G5
> GET /v1/payments/billing-plans/<Plan ID> HTTP/1.1
> Host: api.sandbox.paypal.com
> User-Agent: curl/7.43.0
> Accept: */*
> Content-Type:application/json
> Authorization: Bearer <Access Token>
>
< HTTP/1.1 200 OK
< Date: Sun, 19 Feb 2017 11:36:50 GMT
< Server: Apache
< paypal-debug-id: 3ca31778e271e
< Content-Language: *
< Paypal-Debug-Id: 3ca31778e271e
< Set-Cookie: X-PP-SILOVER=name%3DSANDBOX3.API.1%26silo_version%3D1880%26app%3Dplatformapiserv%26TIME%3D1384360280%26HTTP_X_PP_AZ_LOCATOR%3D; Expires=Sun, 19 Feb 2017 12:06:52 GMT; domain=.paypal.com; path=/; Secure; HttpOnly
< Set-Cookie: X-PP-SILOVER=; Expires=Thu, 01 Jan 1970 00:00:01 GMT
< Vary: Authorization
< Content-Length: 1391
< Connection: close
< Content-Type: application/json
<
* Closing connection 0
{"id":"<Plan ID>","state":"ACTIVE","name":"Sample Plan",
...
"rel":"self","method":"GET"}]}
Got HTTP 200 and the state "ACTIVE".
Thanks for your trying to help, many thanks!