• 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.