I was trying to write a shell script that will take a directory path d as an argument and recursively print a depth indented list of all files and directories in d. However, the argument d is optional and if no argument is given, a depth indented list of all files and directories in the current directory will be printed. Here is my code:
# the method for tree
myspace=""
spaceCount=0
tree(){
cd "$1"
for i in *; do
if [ -d "$i" ]; then
echo -n "|"
for (( j=0;j<spaceCount;j++ ))
do
echo -n "$myspace"
done
echo "--$i"
spaceCount=$((spaceCount+1))
myspace=" |"
tree "$i"
else
echo -n "|"
for (( j=0;j<spaceCount;j++ ))
do
echo -n "$myspace"
done
echo "--$i"
fi
done
cd ../
spaceCount=$((spaceCount-1))
}
if [ "$1" != "" ]; then
file="$1"
fi
echo ".$file"
tree "$file"
But when a folder is empty, it's printing a star like this:
How can I solve the problem?