• 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

    • Use this popular dating app? Hackers may have leaked your name, ID, and private photos by David Uzondu Image via Tea A dating app has been going viral lately (you have probably seen it on X), it's called "Tea," and it lets women anonymously post pictures of men to share dating "red flags." The app's marketing claims this is all for "safety," and you can see it on the company's website, where it states its mission is "to create a safer dating environment for women." In case you're confused and need a visual explanation, the following image pretty much sums it up (click to enlarge): Image: Tea's Website Tea has been around since 2023, but the surrounding controversy helped its recent rise to the top of the app charts. Now the platform has suffered its first notable breach courtesy of users from 4chan. This breach stemmed from a classic, sloppy development mistake. The Tea developers left a backend database wide open on Google's Firebase platform. Firebase allows for quick development, but its default security settings can be disastrously permissive if they are not locked down before an app goes live. Data in Firebase is stored in things called "buckets," which are just cloud storage folders. The leaked bucket in Tea's case contained the exact verification data the app requires from its users: selfies and ID photos, which it needs to confirm that users are women. It did not take long for users on 4chan to find this open door and walk right in. One user claimed, "Yes, if you sent Tea App your face and driver's license, they doxxed you publicly!" Another user claimed they downloaded as many as 3000 images before they got rate-limited by the server. Image: 404Media They described the trove of personal information as being "raw and uncensored." As per the Terms of Use (via 404Media) before you sign up for Tea, apart from selfie and ID photos, you are required to submit your location and birth date. All of this was reportedly accessible. 404 Media says that for a while, anyone with the right URL could view a list of user files. That page has since been locked down and now returns a "Permission denied" error, likely because the developers finally became aware of the leak. Tea has stellar reviews on both the Play Store and the App Store from users who see it as a more secure version of the "Are We Dating the Same Guy?" Facebook groups.
    • I always use O&O tools, it's a German software company and they release bunch of cool free tools like O&O Shutup to disable Windows crap or O&O AppBuster that allows you to remove apps that Windows doesn't allow to remove. Really good and reliable tools.
    • Ergh all the additional admin crap is gonna kill smaller UK forums All to please the religious puritans that are too illiterate to set up parental controls
    • I assume all the UK readers here are using a VPN now? Seriously who the hell is submitting their ID or doing a face scan to watch porn?
  • Recent Achievements

    • First Post
      Electronic Person earned a badge
      First Post
    • Week One Done
      CyberCeps666 earned a badge
      Week One Done
    • Very Popular
      d4l3d earned a badge
      Very Popular
    • Dedicated
      Stephen Leibowitz earned a badge
      Dedicated
    • Dedicated
      Snake Doc earned a badge
      Dedicated
  • Popular Contributors

    1. 1
      +primortal
      604
    2. 2
      ATLien_0
      234
    3. 3
      Xenon
      159
    4. 4
      neufuse
      131
    5. 5
      +FloatingFatMan
      124
  • Tell a friend

    Love Neowin? Tell a friend!