【问题标题】:[Google Cloud][Compute Engine] Linux basics commands not found on startu-scripts[Google Cloud][Compute Engine] 在启动脚本中找不到 Linux 基础命令
【发布时间】:2020-12-11 21:13:18
【问题描述】:

我在为实例组运行虚拟机启动脚本时遇到问题。找不到touchcatchmod 等命令。

ubuntu@instance-template-****-1:~$ sudo google_metadata_script_runner -d --script-type startup
startup-script: INFO Starting startup scripts.
startup-script: INFO Found startup-script-url in metadata.
startup-script: INFO Downloading url from https://storage.googleapis.com/bucket-*****/startup-image to /home/ubuntu/startup-scripts/script using authentication token.
startup-script: INFO startup-script-url: /home/ubuntu
startup-script: INFO startup-script-url: /home/ubuntu
startup-script: INFO startup-script-url: /home/ubuntu/startup-scripts/script: line 11: touch: command not found
startup-script: INFO startup-script-url: /home/ubuntu/startup-scripts/script: line 12: touch: command not found
startup-script: INFO startup-script-url: /home/ubuntu/startup-scripts/script: line 13: touch: command not found
startup-script: INFO startup-script-url: /home/ubuntu/startup-scripts/script: line 15: chmod: command not found
startup-script: INFO startup-script-url: /home/ubuntu/startup-scripts/script: line 16: chmod: command not found
startup-script: INFO startup-script-url: /home/ubuntu/startup-scripts/script: line 71: ssh-keyscan: command not found
startup-script: INFO startup-script-url: /home/ubuntu/startup-scripts/script: line 72: ssh-keygen: command not found
startup-script: INFO startup-script-url: /home/ubuntu/startup-scripts/script: line 74: cat: command not found
startup-script: INFO startup-script-url: /home/ubuntu/startup-scripts/script: line 76: git: command not found
startup-script: INFO startup-script-url: /home/ubuntu/startup-scripts/script: line 78: sh: command not found
startup-script: INFO startup-script-url: Return code 127.
startup-script: INFO Finished running startup scripts.

脚本:

#!/bin/bash

PATH=/home/ubuntu

cd $PATH

touch $PATH/.ssh/id_rsa
touch $PATH/.ssh/id_rsa.pub
touch $PATH/.ssh/known_hosts

chmod 644 $PATH/.ssh/id_rsa.pub
chmod 600 $PATH/.ssh/id_rsa

echo $IDRSA > $PATH/.ssh/id_rsa
echo $IDRSA_PUB > $PATH/.ssh/id_rsa.pub

ssh-keyscan bitbucket.org >> bitbucketKey
ssh-keygen -lf bitbucketKey

cat bitbucketKey >> $PATH/.ssh/known_hosts

git clone git@bitbucket.org:repo/repo.git

sh ./repo/prepare

PROJECT_ID=$(curl "http://metadata.google.internal/computeMetadata/v1/project/numeric-project-id" -H "Metadata-Flavor: Google")

./repo/deploy/run --dev --project-id=$PROJECT_ID

【问题讨论】:

  • 啊!这是上周的一个问题。你不能像你所做的那样定义PATHPATH 是用于查找所有命令的基本变量。将代码中的所有引用替换为$myHomePath 或类似名称,您将更接近工作系统。当您执行echo $PATH 时,您希望看到类似/usr/local/bin/:/usr/bin/:/bin:/usr/sbin 的内容。祝你好运。

标签: bash ubuntu google-cloud-platform google-compute-engine


【解决方案1】:

您在脚本中修改了 PATH 环境:

PATH=/home/ubuntu

但是,PATH 是 bash 的一个 Shell 变量:

PATH   The search path for commands.  It is a colon-separated list of directories in which the shell looks for commands (see
       COMMAND  EXECUTION below).  A zero-length (null) directory name in the value of PATH indicates the current directory.
       A null directory name may appear as two adjacent colons, or as an initial or trailing colon.   The  default  path  is
       system-dependent, and is set by the administrator who installs bash.  A common value is ``/usr/local/bin:
       /usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin''.

因此几乎所有命令都找不到。 (除非它们在 /home/ubuntu 下)


如果你只是想设置一个变量作为用户文件夹来操作脚本,请将变量名改为像MY_HOME。

#!/bin/bash

MY_HOME=/home/ubuntu

cd $MY_HOME

touch $MY_HOME/.ssh/id_rsa
touch $MY_HOME/.ssh/id_rsa.pub
touch $MY_HOME/.ssh/known_hosts

...

【讨论】:

    【解决方案2】:

    当您设置 PATH=/home/ubuntu 时,它会覆盖您应该在 shell 中获取的 PATH 环境变量的值,并强制后续命令以 "command not found" 错误结束,因为相应的可执行文件用于运行这些命令在您刚刚设置的新路径 /home/ubuntu 上不可用。您可以通过将该变量重命名为 PATH 以外的任何名称以及它在脚本中出现的所有名称来解决此问题。您也可以使用小写的相同变量名称。 (比如path=home/ubuntu)。

    PATH 环境变量是一个以冒号分隔的目录列表,当您输入命令时,shell 会搜索这些目录。程序文件(可执行文件)保存在 Unix 系统上的许多不同位置。当您请求特定程序时,您的路径会告诉 Unix shell 在系统上的哪个位置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-23
      • 2018-05-07
      相关资源
      最近更新 更多