Skip to content

Instantly share code, notes, and snippets.

@j03m
Created May 6, 2014 22:35
Show Gist options
  • Save j03m/177e0e768b570132be95 to your computer and use it in GitHub Desktop.
Save j03m/177e0e768b570132be95 to your computer and use it in GitHub Desktop.
lc = ls || cat depending on context
# If stdin present, assume user meant cat
#
if [ ! -t 0 ]
then
cat "$@"
exit
fi
# Find the argument that is file/directory name and test it
# to find out if it is an existing directory. Other arguments are
# options and thus will begin with an "-".
#
for v in "$@"
do
if [ '-' != `echo "$v" | cut -c1 ` ]
then
if [ -d "$v" ]
then
# A directory, use "ls".
#
ls "$@"
exit
else
# Not a directory, use 'cat'.
# If this is not a file or a directory then cat will
# print an error message:
# cat: xxx: No such file or directory
#
cat "$@"
exit
fi
fi
done
# No file name provided, assume the user is trying to ls
# the working directory
#
ls "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment