⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 max2.sh

📁 一本完整的描述Unix Shell 编程的工具书的所有范例
💻 SH
字号:
#!/bin/bash# max2.sh: Maximum of two LARGE integers.#  This is the previous "max.sh" example,#+ modified to permit comparing large integers.EQUAL=0             # Return value if both params equal.E_PARAM_ERR=-99999  # Not enough params passed to function.#           ^^^^^^    Out of range of any params that might be passed.max2 ()             # "Returns" larger of two numbers.{if [ -z "$2" ]then  echo $E_PARAM_ERR  returnfiif [ "$1" -eq "$2" ]then  echo $EQUAL  returnelse  if [ "$1" -gt "$2" ]  then    retval=$1  else    retval=$2  fifiecho $retval        # Echoes (to stdout), rather than returning value.                    # Why?}return_val=$(max2 33001 33997)#            ^^^^             Function name#                 ^^^^^ ^^^^^ Params passed#  This is actually a form of command substitution:#+ treating a function as if it were a command,#+ and assigning the stdout of the function to the variable "return_val."# ========================= OUTPUT ========================if [ "$return_val" -eq "$E_PARAM_ERR" ]  then  echo "Error in parameters passed to comparison function!"elif [ "$return_val" -eq "$EQUAL" ]  then    echo "The two numbers are equal."else    echo "The larger of the two numbers is $return_val."fi# =========================================================  exit 0#  Exercises:#  ---------#  1) Find a more elegant way of testing#+    the parameters passed to the function.#  2) Simplify the if/then structure at "OUTPUT."#  3) Rewrite the script to take input from command-line parameters.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -