I have been trying to get this to work for a while now, and I am still confused as to why it doesnt work. I'm trying to add a function to my bashrc to cd down to the next directory that has files or more than one directory in it. But I cant get this test for files to work, and I do not understand the problem. find . -maxdepth 1 -type f works when I type it into the terminal, but here it doesn't seem to be working. And -z should test if it is null, which it should be when Icall it in an empty directory. But it just returns files detected every time... is it the use of the dot operator in doFilesExist?
function cdwn(){ # check if there are files or multiple directories in current wd doFilesExist="find . -maxdepth 1 -type f" if [ -z '$doFilesExist' ]; then echo "no files detected" else echo "files detected" fi } Thanks guys, seems to be working with the following:
function cdwn(){ # check if there are files or multiple directories in current wd doFilesExist=`find . -maxdepth 1 -type f` if [ -z "$doFilesExist" ]; then echo "no files detected" else echo "files detected" fi } But I am not satisfied as I do not understand why I was having problem, can you suggest some guides I can follow to get a better understanding? I clearly have either forgotten or not understood things in the past!
[ -z $var ]checks if the variablevaris set or not. In your case there's a string invarso it is always set. -- BTW add double quotes when using unary comparison ([ ... ]).$doFileExistinterpreted, otherwise it will just check if the string$doFilesExistinstead of its value:[ -z "$doFilesExist" ]