My assignment is to write a bash script that reads a directory and returns the file type of each file within, including all subdirectories. Using the find command is not allowed. I've tried to implement this using essentially two for loops, but I'm getting a segmentation fault. I found that the script does work however when I pass a directory without subdirectories. Would someone be willing to look over a noob's code and tell me what's wrong? Thank you very much.
#!/bin/bash func () { for name in $1 do if [ -f "$name" ] then file "$name" elif [ -d "$name" ] then func "$1" fi done } directory="$1/*" for name in $directory do if [ -f "$name" ] then file "$name" elif [ -d "$name" ] then func "$1" fi done
func
callsfunc
with the same argument if theelif
condition is met. That'll generate an infinite loop. I suspect you can do everything this script is trying to do with a one-liner:find dir/ -type f -exec file '{}' \;
.find
, that's mentioned in the question.func "$name"
instead offunc "$1"
?