1

I want to check whether the pull frequency limit has been reached before downloading the github file, and print out the prompt information, but the output prompt information is out of order.

The specific command is as follows:

githubGetRateInfo=$(curl -s -I -X POST https://api.github.com/users/octocat)
postLimit=$(echo "${githubGetRateInfo}"|awk /^X-RateLimit-Limit/'{print $2}')
postRemaining=$(echo "${githubGetRateInfo}"|awk /^X-RateLimit-Remaining/'{print $2}')
echo "GitHub rate limit is ${postLimit} per hour"

It should print like this:

GitHub rate limit is 60 per hour

The real output is:

 per hourte limit is 60

The following is the information output by the curl command, the purpose is to filter out the values ​​of X-RateLimit-Limit and X-RateLimit-Remaining

HTTP/1.1 404 Not Found
Server: GitHub.com
Date: Mon, 27 Feb 2023 17:37:26 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 84
X-GitHub-Media-Type: github.v3; format=json
x-github-api-version-selected: 2022-11-28
X-RateLimit-Limit: 60
X-RateLimit-Remaining: 57
X-RateLimit-Reset: 1677522152
X-RateLimit-Used: 3
X-RateLimit-Resource: core
Access-Control-Expose-Headers: ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset
Access-Control-Allow-Origin: *
Strict-Transport-Security: max-age=31536000; includeSubdomains; preload
X-Frame-Options: deny
X-Content-Type-Options: nosniff
X-XSS-Protection: 0
Referrer-Policy: origin-when-cross-origin, strict-origin-when-cross-origin
Content-Security-Policy: default-src 'none'
Vary: Accept-Encoding, Accept, X-Requested-With
X-GitHub-Request-Id: xxxxxxxxxxx

What's wrong with echo output? Am I missed something?


Update1

Perfect!!! Thank you all of you :-)

[root@node177 ~]# od -c <<< ${postRemaining}
0000000   5   7  \r  \n
0000004
[root@node177 ~]# cat -v <<< ${postRemaining}
57^M
[root@node177 ~]# [[ "$(cat -v <<< ${postRemaining})" =~ \^M$ ]] && echo yes
yes

That's the problem. So just change:

githubGetRateInfo=$(curl -s -I -X POST https://api.github.com/users/octocat)

To:

githubGetRateInfo=$(curl -s -I -X POST https://api.github.com/users/octocat|tr -d '\r')

or anything to delete the last carriage return character.


Update2

githubGetRateInfo=$(curl -s -I -X POST https://api.github.com/users/octocat | tr -d '\r')
if grep "^X-RateLimit-Limit" <<< "${githubGetRateInfo}" >/dev/null 2>&1; then
    postLimit=$(echo "${githubGetRateInfo}" | awk /^X-RateLimit-Limit/'{print $2}')
    postRemaining=$(echo "${githubGetRateInfo}" | awk /^X-RateLimit-Remaining/'{print $2}')
elif grep "^x-ratelimit-limit" <<< "${githubGetRateInfo}" >/dev/null 2>&1; then
    postLimit=$(echo "${githubGetRateInfo}" | awk /^x-ratelimit-limit/'{print $2}')
    postRemaining=$(echo "${githubGetRateInfo}" | awk /^x-ratelimit-remaining/'{print $2}')
fi
postLimit=$(( postLimit ))
postRemaining=$(( postRemaining ))
if [ "${postRemaining}" -eq 0 ]; then
    _error "$(date +%F%k:00) to $(date +%F%k:00 -d "+1 hour"): The remaining callable time is ${postRemaining}. Please try an hour later. Exiting."
    exit 1
elif [ "${postRemaining}" -lt 10 ]; then
    _errornoblank "$(date +%F%k:00) to $(date +%F%k:00 -d "+1 hour"): The remaining callable time is ${postRemaining}."
else
    _infonoblank "$(date +%F%k:00) to $(date +%F%k:00 -d "+1 hour") The remaining callable time is ${postRemaining}."
fi
saberlion
  • 11
  • 2
  • 6
    The output contains a carriage return character, you need to remove that. – Barmar Feb 27 '23 at 18:32
  • See ["Why does my tool output overwrite itself and how do I fix it?"](https://stackoverflow.com/questions/45772525/why-does-my-tool-output-overwrite-itself-and-how-do-i-fix-it) and ["Are shell scripts sensitive to encoding and line endings?"](https://stackoverflow.com/questions/39527571/are-shell-scripts-sensitive-to-encoding-and-line-endings) – Gordon Davisson Feb 27 '23 at 18:59
  • We can't see the details of that output, but it is very likely a carriage return. Try `${postLimit%?}` should remove the last char from the string. You might need `%??` . Good luck. – shellter Feb 27 '23 at 19:10

0 Answers0