Run shell script on specified external HDD mount
In this example the output of df is piped to grep which is looking to match the hard disk mount point- /Volumes/2TB. The exit status of grep determines the outcome of the condition and the ! reverses the logic.

if ! df | grep -q '\/Volumes\/2TB$'
then 
    exit 0
fi


In a sentence this says, if grep does not match /Volumes/2TB then exit the script.

I would write the script like this to put it all together.

#!/bin/sh
#
#
if ! df | grep -q '\/Volumes\/2TB$'
then 
    exit 0
fi

ls -R /Volumes/2TB/ |
    sed -e '/^[^:]*$/d' \
        -e 's/://' \
        -e 's/[^-][^\/]*\//--/g' \
        -e 's/^/ /' \
        -e 's/-/|/' &gt;/Volumes/SSD/Users/V/Desktop/File.txt