Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have android sdk installed, and I want the binaries in build-tools/android-VERSION to be available in PATH, so I add few lines:

ANDROID_SDK=/Application/Binaries/adt-bundle-linux-x86_64-20140321/sdk/
ANDROID_PATH=$ANDROID_SDK/tools:$ANDROID_SDK/platform-tools:$ANDROID_SDK/build-tools/android-4.4.2/

Here's the problem, I might update the SDK at any time, so build-tools/android-4.4.2/ might change upon that. If so I would have to edit the file again and update the android-version part in $PATH

So I changed the profile to something like this,

ANDROID_SDK=/Application/Binaries/adt-bundle-linux-x86_64-20140321/sdk/
ANDROID_PATH=$ANDROID_SDK/tools:$ANDROID_SDK/platform-tools
for x in $ANDROID_SDK/build-tools/android-*/; do
    export PATH=$PATH:$x
done

That looks dumb, any better way to write it?

share|improve this question
    
@devnull not files, "paths" –  warl0ck Mar 30 at 8:07
    
@devnull okay added a trailing slash –  warl0ck Mar 30 at 8:08
    
It's not clear what you're trying to achieve here. Are you trying to do something so as to avoid editing your ~/.bashrc or ~/.profile? –  devnull Mar 30 at 8:21
    
@devnull Yes exactly –  warl0ck Mar 30 at 8:22
    
@devnull Just elaborated the question –  warl0ck Mar 30 at 8:23

3 Answers 3

up vote 3 down vote accepted

The ideal way is creating a soft link directory called adt, and re-creating it after SDK changes.

Thus, your PATH will stay the same.

share|improve this answer
    
This looks like the most reasonable one. –  warl0ck Jul 14 at 0:17

In this approach, on two occasions the output of ls, sorted on time and for directories only (-td), is piped to the head command which then selects the first line. The result is that the most recent version of both adt-bundle-linux-x86_64-*/sdk and of build-tools/android-*/ should be used for the modification of the PATH environment variable.

ANDROID_SDK=$( ls -td /Application/Binaries/adt-bundle-linux-x86_64-*/sdk/ | head -n 1 )
if ! [ -z $ANDROID_SDK ] ; then
    ANDROID_PATH=$ANDROID_SDK/tools:$ANDROID_SDK/platform-tools
    ANDROID_BUILDTOOLS=$( ls -td $ANDROID_SDK/build-tools/android-*/ | head -n 1 )
    if ! [ -z $ANDROID_BUILDTOOLS ] ; then
        echo "Adding $ANDROID_BUILDTOOLS to PATH"
        export PATH=$PATH:$ANDROID_BUILDTOOLS
    else
        echo "Couldn't find build tools"
    fi
else
    echo "Couldn't find SDK"
fi
share|improve this answer
PATH="$PATH:$(printf '%s:' $ANDROID_SDK/build-tools/android-[0-9.]*/)"

Should work.

cd ~ ; mkdir dir1 dir2 dir3
( PATH=$(printf %s: $HOME/dir[12])
echo "$PATH" )

/home/mikeserv/dir1:/home/mikeserv/dir2:

Another method:

PATH="$PATH:$( set -- `printf '%s\n' "$ANDROID_SDK"*/android[0-9.]*/ |
    sort -rV` ; [ -d "$1" ] && echo "$1" )"

That takes advantage of sort's -Version handling, which should ensure that the positional parameter "$1" is always set to the latest version of your Android SDK. For example:

WITHOUT SORT:

% printf %s\\n ~/dir[0-9]*
/home/mikeserv/dir1
/home/mikeserv/dir2
/home/mikeserv/dir23
/home/mikeserv/dir3
/home/mikeserv/dir45

WITH IT:

%  printf %s\\n ~/dir[0-9]* | sort -V
/home/mikeserv/dir1
/home/mikeserv/dir2
/home/mikeserv/dir3
/home/mikeserv/dir23
/home/mikeserv/dir45

AND REVERSED:

% printf %s\\n ~/dir[0-9]* | sort -rV
/home/mikeserv/dir45
/home/mikeserv/dir23
/home/mikeserv/dir3
/home/mikeserv/dir2
/home/mikeserv/dir1
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.