【问题标题】:How can I link linux shell files? [duplicate]如何链接 linux shell 文件? [复制]
【发布时间】:2021-08-25 19:21:29
【问题描述】:
  #!/bin/bash
  echo "Please, select your choice (1-4):"
  echo 1- Add Course
  echo 2- Delete Course
  echo 3- Search Course
  echo 4- exit
  read input

  case $input in
    1)
            script1
            ;;
    2)
            script2
            ;;
    3)
            script3
            ;;
    4)
            exit 0
            ;;
    *)
            echo Invalid choice
  esac

1 与文件 Add.sh 关联

2与文件delete.sh相关

3 与文件 seach.sh 关联

4 是退出

【问题讨论】:

  • 有人按下1会发生什么?
  • add.sh 中的命令必须执行。但是代码不起作用
  • /path/to/your/add.sh替换script1并确保它是可执行的(chmod u+x /path/to/your/add.sh)。
  • 可能是复制/粘贴错误,但在“无效选择”后缺少双分号。

标签: bash shell


【解决方案1】:

使用内置 select 命令意味着您不需要重新设计菜单。

#!/usr/bin/env bash

# assuming all the scripts reside in the same directory as this one
script_path="$(dirname "$0")"

PS3="Please, select your choice: "
select choice in "Add Course" "Delete Course" "Search Course" "exit"; do
    case $input in
        "Add Course")    bash "$script_path/add.sh" ;;
        "Delete Course") bash "$script_path/delete.sh" ;;
        "Search Course") bash "$script_path/search.sh" ;;
        exit)            break ;;
    esac
done

我可以更巧妙地避免重复的字符串,但这是不必要的复杂性。

根据这些脚本的内容,您可能希望source 它们而不是在不同的进程中运行它们。甚至将它们放在当前脚本中的函数中可能很简单,而不必维护几个单独的文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-16
    • 1970-01-01
    • 1970-01-01
    • 2011-07-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多