• 0

Bash comparing command in variable


Question

Hello,

Well I am writing my first bash script today and I've hit a snag. I am trying to compare the SHA1 of a file with what it is supposed to be. Here is an abbreviated version:


#!/bin/bash
clear
csha1='openssl sha1 "dliOS.sh"'
bsha1="SHA1(dliOS.sh)=f2533a75a69ab1cd87d93749449523d65d"
echo "Required SHA1: " $bsha1
echo "File SHA1: " $csha1
if [ $csha1 != $bsha1]
then
echo "SSH doesn't match!"
else
echo "SSH is good."
fi
exit
[/CODE]

The output of my script is:

[CODE]
Required SHA1: SHA1(dliOS.sh)=f2533a75a69ab1cd87d93749449523d65d
File SHA!: openssl sha1 "dliOS.sh"
./test.sh: line 7: [: too many arguments
SSH is good
[/CODE]

I've tried every method to include the command output in a variable to compare it with the SHA1 hash it is supposed to be, but all fail. [The whole programs purpose is to log me into my barracks internet (it logs everybody out after each hour, so my script detects the logout, then logs in) whenever it disconnects and then continues my iOS5 download [downloading at 10kb-20kbs a second, I need resume :(). But I want it to verify that the file is intact when it is done downloading. Also, I can't find any method of obtaining the file size in bytes only, no other bs outpust suck as user [i read the stat, ls, and file man pages].

Thank You.

Link to comment
https://www.neowin.net/forum/topic/1048093-bash-comparing-command-in-variable/
Share on other sites

6 answers to this question

Recommended Posts

  • 0

I revised your script to do what I think you want. I tested it, and it seems to work well. Hopefully this gives you the answer you seek.


#!/bin/bash
clear
fname="dliOS.sh"
csha1=$(sha1sum "$fname" | cut -d ' ' -f 1)
bsha1="SHA1($fname)=f2533a75a69ab1cd87d93749449523d65d"
echo -e "Required SHA1:\t\t" $bsha1
echo -e "File SHA1:\t\t" $csha1
if [ "$csha1" != "${bsha1:$((${#fname}+7))}" ]; then
echo "SSH doesn't match!"
else
echo "SSH is good."
fi
exit 0
[/CODE]

Edit: Just to clarify, the number 7 on line 8 represents the number of characters in "SHA1()=" in the bsha1 variable. Alternatively, you could extract the checksum from the variable like this: $(echo $bsha1 | cut -d '=' -f 2) That alleviates the necessity of the static number in the expression in question.

  • 0

When accessing the contents of a variable, always always always put quotes around the variable. Like this:

my_variable = 'This is a test'
echo "$my_variable"	  # <-- Notice the quotes
# This is a test
[/CODE]

In your case what happened is that your "if" statement read the contents of $csha1 as a list of arguments instead of a string. So the script expanded from this:
[code]if [ $csha1 != $bsha1]

to this

if [ openssl sha1 "dliOS.sh" != SHA1(dliOS.sh)=f2533a75a69ab1cd87d93749449523d65d ]

The test command (which is the opening square bracket) expects four arguments:

  1. Left operand
  2. Operator
  3. Right Operand
  4. A closing square bracket.

When Bash expanded your variables, it actually saw 6 arguments, since the contents of $csha1 was read as a list of arguments. By putting your variables inside double quotes, the if statement will read them as strings (as you were trying to do) correctly.

Also as Lant said, you need to use backticks to execute a command and store the output, although now the $() syntax is preferred, which is what you can see on line 4 of xorangekiller's response.

Hope this helps :)

  • 0
  On 30/12/2011 at 23:06, Majesticmerc said:

When accessing the contents of a variable, always always always put quotes around the variable. Like this:

my_variable = 'This is a test'
echo "$my_variable"	  # <-- Notice the quotes
# This is a test
[/CODE]

In your case what happened is that your "if" statement read the contents of $csha1 as a list of arguments instead of a string. So the script expanded from this:
[code]if [ $csha1 != $bsha1]

to this

if [ openssl sha1 "dliOS.sh" != SHA1(dliOS.sh)=f2533a75a69ab1cd87d93749449523d65d ]

The test command (which is the opening square bracket) expects four arguments:

  1. Left operand
  2. Operator
  3. Right Operand
  4. A closing square bracket.

When Bash expanded your variables, it actually saw 6 arguments, since the contents of $csha1 was read as a list of arguments. By putting your variables inside double quotes, the if statement will read them as strings (as you were trying to do) correctly.

Also as Lant said, you need to use backticks to execute a command and store the output, although now the $() syntax is preferred, which is what you can see on line 4 of xorangekiller's response.

Hope this helps :)

Aye that explains everything perfectly! I originally was going to create a PHP script as that is what I am best at, but I felt it best to use the shell for this.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • They looked at Apple's silly naming schemes and were ' Hold my beer...."
    • Microsoft are pretty good at giving up names without a fight
    • Brave... the crypto browser with Brave ad rewards and Leo AI everywhere is not an issue for you and Firefox is? At least in Firefox you can disable everything you don't want easily in about:config and everything you don't want is REALLY removed even from the UI. in Brave the only way to really disable all this stuff and them to completely removed from the UI is to use administrator policies. You can only have them turned off without admin policies.
    • The UK shouldn't copy Trump. If the UK wants its own AI Industry it needs to build one, it also need to sort out the issue of startups flying away to America.
    • Azure Linux 2.0 reaches end of life, requiring AKS Arc users to upgrade by Paul Hill Microsoft has warned that Azure Linux 2.0, used in Azure Kubernetes Service (AKS) enabled by Azure Arc, will reach its end of life on July 31, 2025, necessitating users to upgrade. After this date, Microsoft will no longer provide updates, security patches, or support for Azure Linux 2.0. The longer it is used after this date, the more vulnerable systems will become due to a lack of patches. Azure Linux 3.0 brings significant upgrades to core components that enhance performance, security, and the developer experience. The Linux kernel is upgraded from version 5.15 to 6.6, bringing performance and hardware compatibility improvements. The Containerd package has been updated from version 1.6.26 to 1.7.13, improving container management. The SystemD package has been upgraded from version 250 to 255, streamlining system and service management, and OpenSSL has jumped from version 1.1.1k to 3.3.0, providing enhanced encryption and security. Azure Linux 3.0 also brings more packages and better tooling. Major versions of Azure Linux are typically supported for three years, with Azure Linux 3.0’s EOL projected for Summer 2027. It became generally available in August 2024.Microsoft said that users must migrate to Azure Linux 3.0 by upgrading their Azure Local instances to the 2507 release when it becomes available. After the Azure Local instance has been upgraded, users can then upgrade their Kubernetes clusters. Microsoft gives you the option to remain on the same Kubernetes version during this upgrade by providing the same version number on the aksarc upgrade command. After upgrading, you can verify the kernel version on your Linux nodes by adjusting the file path in this command: kubectl --kubeconfig /path/to/aks-cluster-kubeconfig get nodes -o wide This upgrade is mandatory for continued support. If you want to learn more, check out Microsoft’s announcement which also includes how to reach out to the AKS Arc team if you need to.
  • Recent Achievements

    • Dedicated
      Daniel Pinto earned a badge
      Dedicated
    • Explorer
      DougQuaid went up a rank
      Explorer
    • One Month Later
      MIghty Haul earned a badge
      One Month Later
    • Week One Done
      MIghty Haul earned a badge
      Week One Done
    • Collaborator
      KD2004 earned a badge
      Collaborator
  • Popular Contributors

    1. 1
      +primortal
      597
    2. 2
      Michael Scrip
      201
    3. 3
      ATLien_0
      192
    4. 4
      +FloatingFatMan
      140
    5. 5
      Xenon
      127
  • Tell a friend

    Love Neowin? Tell a friend!