Skip to content

Instantly share code, notes, and snippets.

@yaauie
Last active October 6, 2022 22:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yaauie/cf8152514028d9575fb1869e9b6f2f43 to your computer and use it in GitHub Desktop.
Save yaauie/cf8152514028d9575fb1869e9b6f2f43 to your computer and use it in GitHub Desktop.
#!/bin/sh
#
# Utility for determining why we cannot list the
# contents of a deeply-nested directory.
#
# Usage:
# listup.sh /deeply/nested/path
#
##############################################################################
# Copyright 2022 Ry Biesemeyer
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
##############################################################################
set -e
# error early and helpfully if pre-requisites are not met
if ! command -v realpath &> /dev/null; then echo "realpath required"; exit 127; fi
if ! command -v xargs &> /dev/null; then echo "xargs required"; exit 127; fi
if ! command -v grep &> /dev/null; then echo "grep required"; exit 127; fi
if ! command -v awk &> /dev/null; then echo "awk required"; exit 127; fi
if ! command -v id &> /dev/null; then echo "id required"; exit 127; fi
# capture and expand the provided path
curr="$(realpath --canonicalize-missing "${1:?path required}")"
path_init="${curr}"
prev=""
# walk up
until ls -la "${curr}" &>/dev/null; do
if [ "${curr}" == "/" ]; then break; fi
prev="${curr}"
curr="$(dirname "${curr}")"
done
if [ "${curr}" == "${path_init}" ]; then
exit 0
fi
echo "Failed to list: ${prev}"
peek=$(ls -la "${curr}")
info=$((echo "${peek}" | awk -v prv="$(basename "${prev}")" '$9 == prv' | grep .) || (echo "${peek}" | grep -e "$(basename "${prev}")$"))
if [[ ! -z "${info}" ]]; then
echo "=============================================================="
echo "Parent listing: ${curr}"
echo "--------------------------------------------------------------"
echo "${info}"
echo "--------------------------------------------------------------"
echo "User Info: "
echo "--------------------------------------------------------------"
echo "$(id | xargs -n1 echo)"
echo "--------------------------------------------------------------"
fi
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment