AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / unix / 问题 / 532233
Accepted
IISomeOneII
IISomeOneII
Asked: 2019-07-26 23:10:09 +0800 CST2019-07-26 23:10:09 +0800 CST 2019-07-26 23:10:09 +0800 CST

Shell 脚本开关功能

  • 772

我想制作具有开关功能的shell脚本
就像这样
第一次:

echo -e "Activated!"  

第二次:

echo -e "Deactivated!" 

有没有办法做到这一点?

shell-script
  • 2 2 个回答
  • 359 Views

2 个回答

  • Voted
  1. Best Answer
    Kusalananda
    2019-07-26T23:43:50+08:002019-07-26T23:43:50+08:00

    这样做意味着您必须保存激活的当前状态。

    最简单的方法是为其中一种状态(可能是“开启”或“激活”状态)创建一个文件,并在进入另一种状态(“关闭”或“停用”)时删除该文件.

    创建一个空文件是用 完成的touch filename,并且该文件是否存在的测试是用 完成的if [ -e filename ]; then ...; fi。用 . 删除文件rm filename。

    下面为您提供了通过将变量的值保存到文件中来在该状态下存储一些信息的选项。在这种情况下,状态由每次运行脚本时更改的持久文件携带(而不仅仅是每次调用脚本时创建或删除的文件)。

    假设您正在使用bash:

    #!/bin/bash
    
    statefile=$HOME/.script.state
    
    if [ -f "$statefile" ]; then
        . "$statefile"
    fi
    
    case $state in
        on) state=off ;;
        *)  state=on
    esac
    
    printf 'Current state is "%s"\n' "$state"
    
    declare -p state >"$statefile"
    

    测试:

    $ bash script.sh
    Current state is "on"
    $ bash script.sh
    Current state is "off"
    $ bash script.sh
    Current state is "on"
    $ bash script.sh
    Current state is "off"
    

    该脚本在每次运行结束时通过in将变量保存state到,并通过在开始时获取该文件从那里读取它(如果文件存在)。$HOME/.script.statedeclare -p statebash

    这个文件最终看起来像

    declare -- state="off"
    

    这是declare -p stateif的输出$state是 string off。


    ,/bin/sh上面的脚本可以写成

    #!/bin/bash
    
    statefile=$HOME/.script.state
    
    if [ -f "$statefile" ]; then
        read state <"$statefile"
    fi
    
    case $state in
        on) state=off ;;
        *)  state=on
    esac
    
    printf 'Current state is "%s"\n' "$state"
    
    printf '%s\n' "$state" >"$statefile"
    

    ...只需将状态的读取和写入替换为readand printf,这只会将状态字符串本身保存在状态文件中。

    • 2
  2. LL3
    2019-07-27T14:51:35+08:002019-07-27T14:51:35+08:00

    如果您希望在每次运行时自动反转状态,您还可以利用“逻辑否定”数字运算,这对于反转值很有用。也就是说,0的逻辑否定是1,反之亦然。

    POSIX shell 能够通过算术扩展来执行最常见的数字和按位运算,而Bash 也不短,它还提供了可以用 0 或 1(以及更多)等整数索引的数组。

    在实践中:

    (
    # our persistent file to save state into
    file=./state
    # array of two states, "off" is the first so that it will be 0, and "on" will be 1
    states=(off on)
    # import what's been saved in our state file, if present
    [ -f "$file" ] && source "$file"
    
    # within the arithmetic expansion syntax,
    # we compute new state as the logical negation (the `!` operator) of the current state
    state=$(( !(state) ))
    printf 'state=%d\n' $state > "$file"  # save new state to persistent file
    
    # print current state from the array using the numeric state as index 
    printf 'currently %s\n' "${states[state]}"
    )
    

    在严格的 POSIX shell 中,它有点复杂,因为我们需要解决缺少数组的问题:

    (
    file=./state
    # here we use a simple string instead of an array
    states='off on'
    [ -f "$file" ] && . "$file"  # the `source` command POSIXly is just `.`
    
    state=$(( !(state) ))
    printf 'state=%d\n' $state > "$file"
    
    # here we use the `cut` tool to select the piece of string based on numeric state
    echo -n 'currently '; printf '%s\n' "$states" | cut -d ' ' -f $((state+1))
    )
    
    • 1

相关问题

  • 在awk中的两行之间减去相同的列

  • 打印文件行及其长度的脚本[关闭]

  • 通过命令的标准输出以编程方式导出环境变量[重复]

  • 按分隔符拆分并连接字符串问题

  • MySQL Select with function IN () with bash array

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    模块 i915 可能缺少固件 /lib/firmware/i915/*

    • 3 个回答
  • Marko Smith

    无法获取 jessie backports 存储库

    • 4 个回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    user12345 无法获取 jessie backports 存储库 2019-03-27 04:39:28 +0800 CST
  • Martin Hope
    Carl 为什么大多数 systemd 示例都包含 WantedBy=multi-user.target? 2019-03-15 11:49:25 +0800 CST
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve