PDA

View Full Version : cd into dir if and only if a dir exists



larzconwell
February 11th, 2012, 09:02 AM
Okay I'm creating a compile script and I want it to automatically untar it into a dir I create then cd into that dir. But when it untars' there might be a dir OR the source code. It just depends on the dev I guess. So how would I go about cd-ing into a dir if and only if that dir exists, otherwise try to compile.



compile_source() {
if [[ "$pm" == "source" ]]; then
mkdir compile && tar xzf "$1" -C compile/ && cd compile | echo -e "==> Extracting file and cd-ing into it...\n"
if [[ #dir exists ]]; then
# if there is only a directory cd into it otherwise try to compile
fi
else
echo "This package should be available from your package manager..."
fi
}


That's what I have so far..

savanna
February 11th, 2012, 09:36 AM
MYDIR="~/foo"
if [ -d "$MYDIR" ] ; then
cd "$MYDIR"
...
...
fi

larzconwell
February 11th, 2012, 09:43 AM
That would work, but I won't know the name of the folder... if there's a folder.

savanna
February 11th, 2012, 09:53 AM
There's probably more elegant ways to do this, but...

if ls -l | grep -q '^d' ; then
mydir=`ls -l | grep '^d' | head -1 | awk '{print $8}'`
..
..
fi

larzconwell
February 11th, 2012, 09:56 AM
I'll worry about elegance when I finish the complete script! Haha
Thanks for the help!! I will have to try it in the morning

sisco311
February 11th, 2012, 05:47 PM
Check out BashFAQ 004 (link in my signature).

I'd try something like:


shopt -s nullglob

files=(*)
if (( ${#files[@]} == 1 )) && [[ -d ${files[0]} ]]; then
cd ${files[0]}
...
fi

erind
February 11th, 2012, 11:40 PM
Okay I'm creating a compile script and I want it to automatically untar it into a dir I create then cd into that dir. But when it untars' there might be a dir OR the source code. It just depends on the dev I guess. So how would I go about cd-ing into a dir if and only if that dir exists, otherwise try to compile.
...
Another way using shell built-ins:


compile_source() {
if [[ "$pm" == "source" ]]
then
mkdir compile && tar xzf "$1" -C compile/ && cd compile
echo -e "==> Extracting file and cd-ing into it...\n"

first_dir="$( printf "%s" */ )"
if [ -d "${first_dir%%/*}" ]
then
cd "${first_dir%%/*}"
## other actions here
else
## otherwise try to compile
fi
else
echo "This package should be available from your package manager..."
fi
}