Results 1 to 4 of 4
I've written a bash script to show all files within a directory. The user inputs the directory they want and the script prints the directory listing and size of the ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 03-03-2010 #1
Bash Script Issue
I've written a bash script to show all files within a directory. The user inputs the directory they want and the script prints the directory listing and size of the directory. I coded the script so that the user can input another directory location if they like, if not it asks if they want to quit.
Problem is if the user enters more than one directory, the script asks for another location several times even after the enter "y" to quit. It just doesn't seem to want to exit out of the bash script.
Can you please help?
Here's the script:
# lookup
clear
echo "This utility allows you to list all files in a directory."
echo "------------------------------------------------------"
echo -ne "Enter a location and press [ENTER]: "
read file
if [ "$file" != "" ]; then
clear
ls -lh --color=always $file;
echo
echo -ne "The size of this directory is: "
du -sh $file;
while true; do
echo
read -p "Do you want to enter another location? (y/n) " yn
case $yn in
[Yy]* ) clear; lookup;;
[Nn]* ) exit;;
* ) echo "Please answer yes or no.";;
esac
done
elif [ "$file" == "" ]; then
clear
while true; do
echo "You didn't enter a location."
read -p "Would you like to quit? (y/n) " yn
echo
case $yn in
[Yy]* ) exit;;
[Nn]* ) clear; lookup;;
* ) echo "Please answer yes or no.";;
esac
done
fi
exit 0
- 03-03-2010 #2
Well, I admittedly know almost nothing about scripting, but off the top of my head, I would change your test clause to check if the input is an existing directory, rather than just not empty.
Linux / UNIX: Find Out If a Directory Exists or Not
- 03-13-2010 #3Linux Newbie
- Join Date
- Oct 2008
- Posts
- 150
I also claim no expertise in shell scripts. Why not just use ls and du, directly? Or is this a learning experience? Good luck either way. Kurt
- 03-17-2010 #4Just Joined!
- Join Date
- Mar 2010
- Location
- Grand Rapids, MI
- Posts
- 15
Here are some thoughts for you:
1. Add the interpreter (sha-bang) line to ensure bash / readline is used:
2. lookup isn't defined as a function or sub routine, so the "[Nn]* ) clear; lookup;;" line isn't valid; you'll be stuck in the while loop. To restart from the beginning of the script, you could add another while loop:
#! /bin/bash
# lookup
while true;do
clear
echo "This utility allows you to list all files in a directory."
echo "------------------------------------------------------"
echo -ne "Enter a location and press [ENTER]: "
read file
if [ "$file" != "" ]; then
clear
ls -lh --color=always $file;
echo
echo -ne "The size of this directory is: "
du -sh $file;
while true; do
echo
read -p 'Do you want to enter another location? (y/n) ' yn
case $yn in
[Yy]* ) clear;;
[Nn]* ) exit;;
* ) echo "Please answer yes or no.";;
esac
done
elif [ "$file" == "" ]; then
clear
while true; do
echo "You didn't enter a location."
read -p 'Would you like to quit? (y/n) ' yn
echo
case $yn in
[Yy]* ) exit;;
[Nn]* ) break; clear;;
* ) echo "Please answer yes or no.";;
esac
done
fi
done
exit 0


Reply With Quote
