简体   繁体   English

在命令行找到但不在脚本中找到的命令

[英]Command found at the command line but not in script

I am trying to create a new 我正在尝试创建一个新的 project using a script on mac. 在Mac上使用脚本的项目。 For this, I followed the following steps: 为此,我遵循以下步骤:

  1. Add PATH of tools and platform-tools in my .bash_profile 在我的.bash_profile添加工具和平台工具的PATH
  2. Verify that the android command works in the shell. 验证android命令在外壳中是否可用。
  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. android命令在外壳中运行。 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 . 但是当我使用sh script.sh project_name project_path package_name运行脚本时,它放弃了一个错误,说android: command not found

The PATH variable has a special meaning to the shell. PATH变量对外壳具有特殊含义。 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. 如果您没有为程序提供完整的路径,则shell希望它包含用冒号分隔的目录列表,可以在其中查找程序。

To solve your problem, use another name than PATH in your program. 要解决您的问题,请在程序中使用除PATH之外的其他名称。

As a general advice, I invite you to use a stronger discipline quoting arguments and avoiding the echo command: 作为一般建议,我邀请您使用更严格的规则引用参数并避免使用echo命令:

  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). 优先选择printf不是echo因为它更易于使用,更可靠,更便携(也是明显的赢家)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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