Skip to content

Instantly share code, notes, and snippets.

@ess
Forked from wisetara/hw22-3
Last active December 11, 2017 23:48
Show Gist options
  • Save ess/e69edcf81a06cd665f79b66ea6ad089e to your computer and use it in GitHub Desktop.
Save ess/e69edcf81a06cd665f79b66ea6ad089e to your computer and use it in GitHub Desktop.
Bash script
# Write a bash script named "hw22-3" that accepts a list of inode numbers from the command line,
# and displays
# the file name and physical file system of each file with that inode number. If no
# file exists with that inode number display "no such file".
#
# EXAMPLE:
# > hw22-3 8384567 12345
# Inode "8384567" has files:
# main.cpp on /dev/mapper/system-student
#
# Inode "12345" has files:
# No such file
# With this version, I can enter a known inode with a file, and I get stuff back that doesn't seem right, AND
# I also put in inode "fvdfvafd" and that shouldn't come up with anything, but it comes up with the same stuff.
# At one point, it was returning the right stuff. But... well, it was 4AM when last I worked on it.
#!/bin/bash
# The way I read the problem, this needs to act on ARGS rather than STDIN
# read -a inodes
for inode in ${@}
do
# This will always make $filename empty, because your main redirect is
# /dev/null. Also, adding the / as the path will keep most variants of
# `find` from screaming.
#filename=$(find -inum $i > /dev/null 2>&1)
echo "Inode \"${inode}\" has files:"
# Set up a loop-sentinel so we can determine if any files were found for the
# inode in question.
found="no"
# Note: This will likely die in a fire if a candidate comes up that has white
# space in the filename. For our purposes here, though, it ought to be
# good enough for government work.
for filename in $(find / -inum ${inode} 2>/dev/null)
do
# If there are any files returned by the `find`,
found="yes"
file_system=$(df "${filename}" 2>/dev/null | awk '{if(NR>1)print $1}')
echo -e "\t${filename} on ${file_system}"
done
# By this point, if the above inner loop ran, $found should be "yes."
# Otherwise, that means we didn't find any files for the inode in question.
if [ "${found}" == "no" ]
then
echo -e "\tNo such files"
fi
# Let's dump out a single echo just to separate the inode entries
echo
done
#!/bin/bash
# Return the file system device on which a given file lives
file_system() {
filename=${1}
df "${filename}" 2>/dev/null | awk '{if(NR>1)print $1}'
}
# Find and print out the details of each file associated with a given inode
# number on any file system mounted on the box.
find_files() {
inode=${1}
found=0
for filename in $(find / -inum ${inode} 2>/dev/null)
do
found=1
echo -e "\t${filename} on $(file_system "${filename}")"
done
# If we found something, return true. Otherwise, false.
[ "${found}" -eq 1 ]
}
main() {
for inode in ${@}
do
echo "Inode \"${inode}\" has files:"
# If files are found, we let `find_files` handle the output. Otherwise,
# we declare that no files were found.
find_files "${inode}" || echo -e "\tNo such file"
# Let's add a newline between each inode report
echo
done
}
# The way I read the problem, this needs to act on ARGS rather than STDIN
# read -a inodes
main $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment