How to test if files differ in BASH and test that those differences are expected

A script to test if files are different, and to further test if the differences in those files are the expected differences:

#!/bin/bash

echo "text to find" | tee subject.log
echo "text to find" | tee same.log
echo "text which differs" | tee differs.log

if diff subject.log same.log; then
  echo "They are the same (expected)"
else
  echo "They are different"
fi

if diff subject.txt differs.txt > /dev/null; then
  echo "They are the same"
else
  echo "They are different (expected)"
fi

echo "Capturing diff output..."
mydiff=`diff subject.txt differs.txt`

echo "Printing test output:"
echo "$mydiff"

echo "Test that expected 123 & 12 diff exists:"
if echo "$mydiff" | grep '^< text to find$'; then 
  echo "Found 'text to find'"
else 
  echo "Not Found 'text to find'"
fi
if echo "$mydiff" | grep '^> text which differs$'; then
  echo "Found 'text which differs'"
else
  echo "Not Found 'text which differs'"
fi

Advertisements

Saturate (max out) Memory Utilization

To Max out memory use:

#!/usr/bin/python
 
# To eat 40GB of RAM and hold it for 5 seconds type:
#./memconsume.py 5 40
 
import time
import sys
 
seconds = int(sys.argv[1])
gb = int(sys.argv[2])
 
mem = gb * 1073741824;
cap = 1 * 1073741824;
calls = ( mem // cap );
print "consuming, using %s" % (mem);
 
memstr = []
for call in range( calls ):
    print " adding: %s" % (cap);
    memstr.append(' ' * cap);
 
print "waiting..."
 
time.sleep(seconds)

Creating a Directory Archive (.tar.gz/tar ball) in that Same Directory

It’s pretty simple:

cd ~/my_dir && tar --exclude=my_dir.tar.gz -czf my_dir.tar.gz ./*

The -c argument says to compress, z tells tar to create a gz type, f is to specify the file. The –exclude is the trick here so that tar doesn’t attempt to archive the file it is creating.

I’ve found it best to do this in the directory you are archiving, so yes cd into it first. If you don’t you will end up with problems such as longer paths in the tar archive, the exclusion specified won’t match, etc.

Opening a VMCX File to Run a Windows XP Mode VM from Windows 10

Enable Client Hyper-V:

  1. Press Windows Key
  2. Type (i.e. search) for “windows features”
  3. Select “Turn Windows features on or off”
  4. Press enter
  5. Enable the following:

Hyper-V

Go install Windows XP Mode for Windows 10 from Microsoft.com:

Download the WindowsXPMode_en-us.exe and using 7-zip extract this executable to¬† folder: “WindowsXPMode_en-us”

Once that is done, use 7-zip to open WindowsXPMode_en-us\sources\xpm and extract: VirtualXPVHD

Rename VirtualXPVHD to VirtualXPVHD.vhd

Start “Hyper-V Manager”

Connect to local server if you are not already, you can tell by checking if you see “Import Virtual Machine…” then you are already connected and can ignore this step.

Action > Virtual Switch Manager > Create New > External Network > [X] Allow management operating system to share this network adapter > Apply > OK

Action > New > Virtual Machine > Next > Name it your preference:

  • WindowsXPTestVM

Generation 1, 1024 MB of RAM is sufficient, configure network to same one you created earlier.

That VM should run and work like a fresh install of Windows XP.

Now we need to open the VMCX file with a text editor and read through it to understand what VHD file and VMC files were loaded for the VMCX file.

Select network “New Virtual Network”

I generally copy these files (which can be several GB) to my local disk so they run faster.

Then I modify the Test VM and add a secondary IDE connection to a new drive which has the older VHD file.

Or you can just try to boot directly into the VHD file:

Action > New > Virtual Machine > Next > Name it your preference:

  • MyVMFromVMCX

Use the VHD file from your VMCX (preferably a local copy).