0

I want to write a condition if the output matches by executing the below command.

diff -is <(echo 'curl https://get.gravitational.com/teleport-v9.3.4-linux-amd64-bin.tar.gz.sha256') \
         <(shasum -a 256 teleport-v9.3.4-linux-amd64-bin.tar.gz)

The output of the above command come as

< 15c7fabe609513fdba95ff68ccc59f29502f32f4d20c163bd9ff47770b554d15 teleport-v9.3.4-linux-amd64-bin.tar.gz
> 15c7fabe609513fdba95ff68ccc59f29502f32f4d20c163bd9ff47770b554d15  teleport-v9.3.4-linux-amd64-bin.tar.gz

in this scenario since the checksum matches, how to declare each output to add in a condition. Eg: if output a==output b, checksum matches. Great if anyone can help.

Thank you for your answers. I have tried with the below answers and able to execute as per my requirement.

2 Answers2

2

Instead of diff-ing the *.sha256 and the output of shasum, I would let shasum use its own built-in "check" option (shasum -c) for this.

In action:

$ echo "This is a file" > file1
$ echo "This is another file" > file2
$ shasum -a 256 file* | tee files.sha256
0b7d91193b9c0f5cc01d40332a10cf1ed338a41640bd7f045f1087628c1d7a9b  file1
0290013ed1662eda102bee144a282ffe03d226b4dd9134c251c6b3be6d69d6ec  file2

$ shasum -c files.sha256 file1: OK file2: OK $ echo $? 0

$ echo "damaged file" > file1

$ shasum -c files.sha256 file1: FAILED file2: OK shasum: WARNING: 1 computed checksum did NOT match $ echo $? 1

If you are script this, then the exit-code from shasum -c can tell you if the verification was successful.

So in your case, I would:

wget https://get.gravitational.com/teleport-v9.3.4-linux-amd64-bin.tar.gz.sha256
shasum -c teleport-v9.3.4-linux-amd64-bin.tar.gz.sha256
rm teleport-v9.3.4-linux-amd64-bin.tar.gz.sha256

or

shasum -c <(curl https://get.gravitational.com/teleport-v9.3.4-linux-amd64-bin.tar.gz.sha256)
Stewart
  • 13,677
0

With the URL and filename substituted, the command you're actually running is this:

diff -is <(echo `curl "$url"`)  <(shasum -a 256 "$file")
                ^           ^

Note that's backticks in echo `curl...` , not single quotes as in your post(*). Passing the output of curl through that unquoted command substitution applies word splitting, giving two distinct arguments to echo, which then joins them with a single space. In effect, changing the double space in the file to a single one.

(* with single quotes, it'd output curl https://..., not run it as a command)

See:

That's also the reason diff gives any output at all: the lines differ in only that one space.

If you drop the useless command substitution and run

diff -is <(curl "$url")  <(shasum -a 256 "$file")

instead, it should recognize the files as identical, and with -s, tell you that. Then, you could just use the exit status of diff directly without caring about the printed output:

if diff -iq <(curl "$url")  <(shasum -a 256 "$file") > /dev/null; then
    echo "hashes are the same"
else
    echo "hashes differ"
fi

Though you don't need diff there except for the case-ignoring function, but I don't think that's necessary. You could store the outputs in shell variables and compare them:

their=$(curl "$url")
mine=$(shasum -a 256 "$file")
if [[ "$their" == "$mine" ]]; then
    echo "hashes match"
else
    echo "hashes do not match"
fi

Or, if you want to compare just the initial part with the hash:

their=$(curl "$url")
their=${their%% *}
mine=$(shasum -a 256 "$file")
mine=${mine%% *}
if [[ "$their" == "$mine" ]]; then
    ...

Or even something like

read -r hash1 filename1 < <(curl "$url")
etc.

to read both fields from the output to separate variables.

ilkkachu
  • 138,973
  • Thanks you. I'm able to execute directly in terminal. But while executing, i'm getting the below error. :syntax error near unexpected token (' /line 7:if diff -iq <(curl "https://get.gravitational.com/teleport-v9.3.4-linux-amd64-bin.tar.gz.sha256") <(shasum -a 256 "teleport-v9.3.4-linux-amd64-bin.tar.gz") > /dev/null; then'. if diff -iq <(curl "https://get.gravitational.com/teleport-v9.3.4-linux-amd64-bin.tar.gz.sha256") <(shasum -a 256 "teleport-v9.3.4-linux-amd64-bin.tar.gz") > /dev/null; then echo "hashes are the same" else echo "hashes differ" fi – user123 codebit Jul 26 '22 at 13:24
  • @user123codebit, run the script with bash, not sh. See: https://unix.stackexchange.com/questions/87560/does-the-shebang-determine-the-shell-which-runs-the-script – ilkkachu Jul 26 '22 at 14:56