寻找海盗的宝藏

ShellShellBeginner
立即练习

💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版

介绍

黑胡子船长(Captain Blackbeard)将他的宝藏藏在一个偏远的岛屿上,并留下了一系列线索,这些线索被编码为 shell 变量和算术运算。在这个挑战(Challenge)中,你将完成一个部分编写的 shell 脚本,以解码这些线索并找到宝藏。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL shell(("Shell")) -.-> shell/VariableHandlingGroup(["Variable Handling"]) linux(("Linux")) -.-> linux/BasicFileOperationsGroup(["Basic File Operations"]) shell(("Shell")) -.-> shell/AdvancedScriptingConceptsGroup(["Advanced Scripting Concepts"]) shell/VariableHandlingGroup -.-> shell/variables_decl("Variable Declaration") shell/VariableHandlingGroup -.-> shell/variables_usage("Variable Usage") linux/BasicFileOperationsGroup -.-> linux/chmod("Permission Modifying") shell/AdvancedScriptingConceptsGroup -.-> shell/arith_ops("Arithmetic Operations") shell/AdvancedScriptingConceptsGroup -.-> shell/arith_expansion("Arithmetic Expansion") subgraph Lab Skills shell/variables_decl -.-> lab-388807{{"寻找海盗的宝藏"}} shell/variables_usage -.-> lab-388807{{"寻找海盗的宝藏"}} linux/chmod -.-> lab-388807{{"寻找海盗的宝藏"}} shell/arith_ops -.-> lab-388807{{"寻找海盗的宝藏"}} shell/arith_expansion -.-> lab-388807{{"寻找海盗的宝藏"}} end

解码藏宝图

藏宝图已被编码成一个需要完善的 shell 脚本。该脚本使用基本的算术运算将初始坐标转换为最终位置。你的任务是填写缺失的值和计算,以揭示宝藏的埋藏地点。

任务

通过以下方式完成 /home/labex/project 目录中的 treasure_map.sh 脚本:

  • LATITUDELONGITUDE 变量分配正确的初始值。
  • 实现算术运算来计算 PACES_NORTHPACES_EAST

要求

  1. shell 脚本 treasure_map.sh 位于 /home/labex/project 中,具有以下代码结构:
#!/bin/bash

## Assign the correct values to these variables
## 为这些变量分配正确的值
LATITUDE=
LONGITUDE=

## Calculate the paces using arithmetic operations
## 使用算术运算计算步数
PACES_NORTH=
PACES_EAST=

## Don't modify the line below
## 不要修改下面这行
echo "The treasure is buried $PACES_NORTH paces north and $PACES_EAST paces east from the old oak tree."
  1. 使用以下值和计算:

    • LATITUDE 设置为 15
    • LONGITUDE 设置为 25
    • PACES_NORTH 计算为纬度(latitude)乘以 2。
    • PACES_EAST 计算为经度(longitude)除以 5。
  2. 该脚本将以此格式输出宝藏位置:

The treasure is buried X paces north and Y paces east from the old oak tree.

其中 X 和 Y 是计算出的值。

示例输出

The treasure is buried 30 paces north and 5 paces east from the old oak tree.

提示

  • 确保使用 chmod +x 使脚本可执行。
✨ 查看解决方案并练习

总结

在这个挑战(Challenge)中,你已经学习了 shell 脚本的基础知识:

  • 为变量赋值。
  • 在 shell 脚本中执行算术运算。
  • 执行脚本和格式化输出。

这些技能是更复杂的 shell 脚本任务的基础。