I have this script that will show the user the head or tail of the files that are inside a set directory (currently that is set as '~/bin/new/*'). This script works but i would like to allow the user to not only show the head or the tail, but so that they can select the directory.

The current script is written as:

while true;do
echo "Would you like to list the head or tail?"
    read headortail
if [[ $headortail = head ]]; then
    head -n 4 ~/bin/new/*
elif [[ $headortail = tail ]]; then
    tail -n 4 ~/bin/new/*
    echo "Invalid Input - Please Input head or tail"

If I add another line under the first echo such as:

echo "Please type the directory"
    read dirLocation

How could i implement this to the script, would I do:

head -n 4 $dirLocation

Would that work?

  • Almost; you probably would want to use if [[ -d "$dirLocation" ]]; then head -n4 ${dirLocation}/*; else echo "No such directory"; fi or the like. There are many other improvements that could be made, such as using select rather than read ... if. – DopeGhoti Dec 9 '15 at 18:07

After read dirLocation simply replace




Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.