1

I have extracted a 7-zip binary file and I want to compare two hashfile outputs to ensure they are the same. I am calling the file.sh in 'git bash' not in wsl because that is what we have access to. Note that means the environment is MINGW64 so I an unsure if that changes the shell script.

In foe.txt:

line explaining how to get hashfile
other notes
last line

SHA384:  32930dae794520cf08db88d0ef9fb123eff51b7e92b6fd91bb8ea722d67e19260c093c95c8ed693fecc598f342113442

Then backpedal the folder and certutil the lib file to get the hash number and compare the two.

When just doing a while loop and echo line it stops before the last line. But there is no error until I add the if statement then it fails on done

If I try the for loop after the folder back it fails on fi. Therefore the syntax or "meat" of the if and for statements are causing an error.

#!/bin/bash
while read -r line
do
if [SHA: in line]
do
set serial=$line
done < foe.txt # error syntax near unexpected token
cd ../
set hashfil=sha384sum foe.lib
# example hash: 32930dae794520cf08db88d0ef9fb123eff51b7e92b6fd91bb8ea722d67e19260c093c95c8ed693fecc598f342113442 *foe.lib
if [hashfil in serial]
echo "valid"
else
echo "double check hash"
fi
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Nikki
  • 31
  • 5
  • please copy/paste your complete code (missing at least a `fi`) into https:/shellcheck.net and fix any issues flagged. If your script still isn't working, update your question with the corrected code. Note that you verbal descriptions may be hard for others to understand (me too). Better to focus on showing a sample input, expected output, currrent output and exact text of any error messages. Maybe read the section [How to turn a bad script into a good question](https://stackoverflow.com/tags/bash/info) . Search for that heading, skip the version information at the top. Good luck. – shellter Apr 24 '23 at 19:31
  • `for /f "delims"= %%A ...` is MS-DOS batch file syntax. The `bash` shell will not recognize that. – shellter Apr 24 '23 at 19:32
  • @shellter I don't think I have ever posted something on here without getting that first comment. When I tried putting in ```fi``` the script would fail so I stopped putting it in, the file didn't fail when I didn't have ```fi`` in. Do you know how to do the for loop for certutil? Thank you for the help I have been struggling with putting questions on here – Nikki Apr 24 '23 at 21:06
  • So I think I found something: I can change the MS-DOS to ```sha384sum foe.lib $serial``` to check the hash. against a variable but I would have to have just the hash in serial? – Nikki Apr 24 '23 at 21:18
  • Good show updating your question, although I still encourage you to copy/paste your code into https://shellcheck.net . But first, you want to get all your code so it will work with `bash`. You can goto https://batsh.org and convert the `.bat` code into bash code. Replace that and then do shellcheck. For now, quit trying to read from files. Get the script to work with hard coded values, ie. `serial=ABCwhateverYouSeeInThatFileThanYouNeed`. (So you took out the use of `certutil`, for now that is good.). Good luck. – shellter Apr 24 '23 at 23:44
  • @shellter I am not understanding what you are asking for. I copied it into spellcheck and I was getting the same errors that I don't know how to fix. And my file is .sh would converting it to .bat fix my errors? The hash number verification is in the file and I have to use either certutil or sha384sum to see what the hash number is. Then compare the two numbers to ensure they are the same. I an unsure what you mean by hard coding, how would that fix the errors? – Nikki Apr 26 '23 at 02:50
  • Yes, I understand your problem (mostly). Most veteran users of StackOverflow want to help educate new users about the topic that is causing a problem. I am like that too. We offer recommendations on tools that should help you fix your problem and give advice on better code. We don't want to write the solution for you. We want to help you figure it out so you have a "learning moment" (Ah -HA!) .. So I'm giving you hints the the simplest way to see where your current script is failing. – shellter Apr 26 '23 at 02:56
  • Did you copy/paste your code to https://shellcheck.net . Did you fix any errors flagged there. If that didn't solve it, then I'm encouraging you to further simplify your code, by using only hard-coded values. If it doesn't work with hard coded values, its not going to work with retrieving *"The hash number verification is in the file"* . Once the basic logic works with hard-coded values, then you can start re-adding "features" (values stored in files, certutil, etc). Everyone struggles with this, if it was easy, we wouldn't have jobs, right? I am not around all the time, but will chk in. – shellter Apr 26 '23 at 03:02
  • I mentioned https://batsh.org because your early version of your script included both `bash` and `.bat` code. You can only use one at a time. I wasn't sure what the `.bat`` for loop was trying to do, and hoped you could translate it (at batsh.org) into bash. This is the general idea of the early messages. I'm sorry if they weren't clear. Communicating by comments can have its problems, yes? So that's what I know. Reread and do step-by-step testing/debug. Good luck! – shellter Apr 26 '23 at 03:07
  • @shellter I agree it is hard to talk via comment. I always feel like I ask questions and they never get solved. I don't know what a .bat file is I thought it was bash (you said ms-dos) because that was the fix I found first before I asked here. Thank you for the help. I did add the output I get from sha and what is in the txt as far as hash number. If that helps, as I was thinking I might have to allocate memory as the hash number is too large. – Nikki Apr 26 '23 at 03:18
  • Shell script variables (linux/unix) can store very long values and it is all done automatically, you only need to make the assigment. you don't have to allocate it (as you would in the `c` language and many others). `.bat` files work in MS-Windows. They go back to the 1985+. New Windows uses powershell. We're not using either of those here. Don't worry further about `.bat` files as we are both clear now that you are NOT using them (-;. Go thru my list of tests and fixes. I don't see any indication you are using shellcheck.net . Going to bed now. Good luck. – shellter Apr 26 '23 at 04:07
  • What are you hoping to accomplish with `if [SHA: in line]` ? Are you trying to test if the literal string `SHA:` appears in the line that was read from the file? If so, start by learning the correct way to do that. I'm sure that question has been asked on SO many, many times, so maybe start at https://stackoverflow.com/questions/229551/how-to-check-if-a-string-contains-a-substring-in-bash – William Pursell May 19 '23 at 12:48

0 Answers0