简体   繁体   中英

Command found at the command line but not in script

I am trying to create a new project using a script on mac. For this, I followed the following steps:

  1. Add PATH of tools and platform-tools in my .bash_profile
  2. Verify that the android command works in the shell.
  3. 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:

  1. 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.

  2. Prefer printf over echo because it is easier to use, more reliable and more portable (also, a clear winner).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM