Command found at the command line but not in script - android

I am trying to create a new android project using a script on mac. For this, I followed the following steps:
Add PATH of tools and platform-tools in my .bash_profile
Verify that the android command works in the shell.
Write a script to create a new project.
Here is the script I wrote:
NAME=$1
PATH=$2
PACKAGE=$3
echo $1
echo $2
echo $3
function create_new_android_project()
{
android create project -n "$NAME" -t 7 -p "$PATH" -k "$PACKAGE" -a MainActivity
}
create_new_android_project
echo
echo "******** Complete!!!"
The android command runs in the shell. But when I run the script with
sh script.sh project_name project_path package_name
it gives up with an error saying android: command not found.

The PATH variable has a special meaning to the shell. The shell expects it to contain a colon separated list of directories where it can look for programs, when you do not supply a complete path for them.
To solve your problem, use another name than PATH in your program.
As a general advice, I invite you to use a stronger discipline quoting arguments and avoiding the echo command:
There is few reasons why a variable name should not appear between double quotes. Therefore, unless you want to achieve something special, you should always use double quotes to control variable expansion.
Prefer printf over echo because it is easier to use, more reliable and more portable (also, a clear winner).

Related

'adb' is not recognized as a command

In my new MacBook, I installed latest Android Studio. Then, in terminal, I created the .bash_profile, and added following lines to the file:
export ANDROID_HOME=~/Library/Android/sdk
export ANDROID_TOOLS=$ANDROID_HOME/tools
export PATH=$PATH:$ANDROID_HOME/platform_tools
export PATH=$PATH:$ANDROID_TOOLS
After that, I run command source .bash_profile, then I type adb command, but get error that adb is not recognized as a command. Why? I also echo $PATH, I saw the platform_tools directory of Android sdk is there & there is adb file under that directory.
====== UPDATE =====
It is interesting, if I do following, it works:
export PATH=$PATH:/Users/myname/Library/Android/sdk/platform-tools
export PATH=$PATH:/Users/myname/Library/Android/sdk/tools
export ANDROID_HOME=/Users/myname/Library/Android/sdk
WHY? Isn't ~ above identical to /Users/myname? Why my original script doesn't work but the above one works? I don't understand....Please someone explain to me.
Depending on the shell, export variable definitions can be treated differently than normal assignments — in particular, tilde expansion might not occur at all.
If this is the case, then every unresolved tilde in a variable stays unresolved even if this variable gets expanded somewhere else. According to the docs:
The order of expansions is: brace expansion; tilde expansion, parameter and variable expansion, <…>
You might want to try this:
ANDROID_HOME=~/Library/Android/sdk
export ANDROID_HOME
export ANDROID_TOOLS=$ANDROID_HOME/tools
export PATH=$PATH:$ANDROID_HOME/platform_tools
export PATH=$PATH:$ANDROID_TOOLS
If it doesn`t help, you can also try $HOME:
ANDROID_HOME=$HOME/Library/Android/sdk
export ANDROID_HOME
This pretty fundamental stuff:
In the current directory (./) execute command "adb"
./adb (you say this works).
what you want is adb (execute command adb found from the PATH variable), if the command "adb" can be found after searching in ALL paths in the PATH variable, execute the command adb where it is found.
A common precursor in the path entries is "." (current directory) but is discouraged due to executable name abuse (same name in a random directory).
Different paths are separated by the path separation variable (in windows it is semicolon ";" UNIX usually colon ":" ALSO MAC).
Your multiple exports SEEM to be over writing each other, do it once with the appropriate separator ":" and all your paths in one (or ALWAYS include $PATH: to retain all previous exports).
it should be in ../somewhere../sdk/platform-tools/adb
(where ever your SDK resides) export PATH=$PATH:/Users/myname/Library/Android/sdk/platform-tools seems ok (be careful not to over-ride it[erase]).
After a re-boot all paths should be honored.
NB
in UNIX we have the "which adb" command which tells us where the executable resides. In Windows with tools like cygwin we can also do the same thing.
I believe Mac has Homebrew.
In your example all lines work OK (but pay particular attention to platform_tools folder's name):
export ANDROID_HOME=~/Library/Android/sdk
export ANDROID_TOOLS=$ANDROID_HOME/tools
export PATH=$PATH:$ANDROID_HOME/platform_tools:$ANDROID_TOOLS
source .bash_profile
adb
so, you've got a mistake:
platform_tools
instead of:
platform-tools
Also, you could try this solution, it works fine:
# print two export commands to your ~/.bash_profile
echo "export ANDROID_HOME=/Users/swift/Library/Android/sdk" >> ~/.bash_profile
echo "export PATH=${PATH}:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools" >> ~/.bash_profile
# Refresh bash profile (or restart Terminal.app)
source ~/.bash_profile
# Start using your adb command
adb --version
adb devices

Jenkins can not use adb when outside directory

As the title says, Jenkins can correctly switch directories to the adb and then execute it. But it can not use it when it is outside of adb's directory.
The following code :
.//opt/android-sdk-linux//platform-tools/adb --help
results in the following output :
+ .//opt/android-sdk-linux//platform-tools/adb
/tmp/jenkins882020622874679741.sh: rad 18: .//opt/android-sdk-linux//platform-tools/adb: Filen eller katalogen finns inte
Build step 'Execute shell' marked build as failure
Finished: FAILURE
While this code :
cd /opt/android-sdk-linux//platform-tools/
ls
./adb --help
correctly outputs the adb help manual.
Additionally, entering
adb --help
gives the same error even when the path given by "which adb" is appended to the $PATH variable.
ls /opt/android-sdk-linux//platform-tools/adb
also shows that it can see adb from its current directory.
How do I make jenkins run adb without switching directories to the adb directory?
edit 1:
Adding an alias to adb resulted in this error:
+ alias adb=.//opt/android-sdk-linux//platform-tools/adb
+ adb shell am instrument -w -r -e debug false -e class se.***** se.*******/android.support.test.runner.AndroidJUnitRunner
/tmp/jenkins523172794505644997.sh: rad 24: adb: kommandot finns inte
Build step 'Execute shell' marked build as failure
Finished: FAILURE
Clearly, something is still wrong with the PATH inclusion so maybe have Jenkins print the PATH variable to debug. A temporary solution could be using an alias:
$ alias adb=".//opt/android-sdk-linux//platform-tools/adb"
This command will result in .//opt/android-sdk-linux//platform-tools/adb being executed every time you execute adb.
After working for a while on this I found that Jenkins allows you to inject environment variables in the device configuration. You can also inject environment variables for each build separately. I do not know why exporting them through a bash script does not work but this is what worked for me. In summary, injecting the $PATH variable using Jenkins' built in tool fixed everything.

How to get list of currently working commands in Cygwin?

Currently working with the linphone-android integration. I have installed some packages during installation of Cygwin.
When I try some of the commands like ls, rm, cd, wget, tar and etc.. are working fine. But few commands like shell, clean are not working. It gives error command not found.
So, my question is,
Is there any way to get list of working commands for Cygwin?
Is there any way to install packages for these commands rather than installing them manually?
In my opinion the most elegant solution is to use the compgen command:
compgen -c lists all the available commands
compgen -a lists all the available aliases
You can also try a more brute approach:
Get all the paths from cygwin using echo $PATH and then for each folder execute ls -h <folder_name>
For installing a package the best way is to install first the equavalent of apt-get: apt-cyg from https://github.com/transcode-open/apt-cyg and put it in /usr/local/bin:
wget raw.github.com/transcode-open/apt-cyg/master/apt-cyg<br>
chmod +x apt-cyg<br>
mv apt-cyg /usr/local/bin<br>
You can also try running the setup executable used to install cygwin setup.exe -q -n -N -d -R c:\cygwin -s http://mirror_site_to_use -l c:\local_package_folder for a local package or setup.exe -q -P package_name to let the setup download the package

How do I execute a shell file?

I'm trying to download Whatsapp on my laptop. I am using Ubuntu desktop software. I've installed Android Emulator from http://dl.google.com/android/android-sdk_r16-linux.tgz
I unzipped the file, opened the folder, Android-sdk-linux, opened Tools and now I'm supposed to execute the file Android, and I'm not sure how to do that. Can anyone give me any help?
Ok, so this is what I thought I was supposed to do:
shell#shell:~$ cd Desktop
shell#shell:~/Desktop$ ./android.sh
bash: ./android.sh: No such file or directory
but then that happens...
Make the file executable first with
chmod +x filename.sh
Then start the script with
./filename.sh
or
/full/path/to/filename.sh
sh filename.sh
OR
bash filename.sh
Use the following command to install the WhatsApp on Ubuntu:
wget https://www.thefanclub.co.za/sites/all/modules/pubdlcnt/pubdlcnt.php?file=https://www.thefanclub.co.za/sites/default/files/public/downloads/whatsapp-webapp_1.0_all.deb&nid=200 && sudo dpkg -i whatsapp-webapp_1.0_all.deb
Then enter the password and open WhatsApp using the application key.
by default permission for any shell script is "-rw-rw-r--" first we need to change the permissions using the "chmod command" then we can run the shell script in the same way in which we run the C executable code.
To debug the shell script we need to run the shell script with the "bash -x" option as follow : $ bash -x ./

wrong commands in ubuntu terminal

I used a tutorial to allow me to access my Galaxy Nexus on my ubuntu machine using MTP, for some reason it doesn't work manually with android 4.0.
This is the tutorial i used:
http://www.omgubuntu.co.uk/2011/12/how-to-connect-your-android-ice-cream-sandwich-phone-to-ubuntu-for-file-access/
I got the part where you use 'echo' and i typed something wrong, this was the part i did wrong:
echo “alias android-connect=\”mtpfs -o allow_other /media/GalaxyNexus\”" >> ~/.bashrc
echo “alias android-disconnect=\”fusermount -u /media/GalaxyNexus\”" >> ~/.bashrc
source ~/.bashrc
I type this word alia instead of alias and now i can't run the script properly meaning i can't compy anything over to my nexus. and when i open a terminal window i get a warning on the top saying:
No command 'alia' found, did you mean:
Command 'ali' from package 'nmh' (universe)
Command 'ali' from package 'mailutils-mh' (universe)
Command 'alsa' from package 'alsa-base' (main)
alia: command not found
Is there a way i can restart thing, remove the command, im not the greatest when it comes to commandline, im trying but sometimes its hard to find help with what your looking for on the internet without going on sites like this and asking.
Thanks.
You can manually edit your .bashrc file in order to clean the situation.
You can try this opening a shell:
vi .bashrc
or, if you prefer alternate editors:
nano .bashrc
or going on window manager:
gedit .bashrc
Use real quotes : " is not “
Either run the following:
echo "alias android-connect=\"mtpfs -o allow_other /media/GalaxyNexus\"" >> ~/.bashrc
echo "alias android-disconnect=\"fusermount -u /media/GalaxyNexus\"" >> ~/.bashrc
source ~/.bashrc
Or add the following lines to your ~/.bashrc with the editor of your choice:
alias android-connect="mtpfs -o allow_other /media/GalaxyNexus"
alias android-disconnect="fusermount -u /media/GalaxyNexus"
And then run:
source ~/.bashrc

Categories

Resources