bash - 如何删除终端中 ip 地址的最后一个八位字节?

标签 bash debugging terminal isolation

假设我有一个 IP 地址 123.456.789.01

我想删除最后一个八位字节 123.456.789.01 但不是句点。

所以我剩下的是 123.456.789。

感谢任何帮助。谢谢。

Linux 终端

我尝试分离最后一个八位位组,但这只给我最后一个八位位组 01 而不是 123.456.789。:

$ address=123.456.789.01 
$ oct="${address##*.}"
$ echo "$oct"
01

最佳答案

@WiktorStribiżew 的答案是正确的。

一些其他(更深奥的)技术:

  • 因为 ${address##*.} 删除了最后一个点,留下八位字节,我们可以从字符串的末尾删除 that :

    echo "${address%${address##*.}}"    # => 123.456.789.
    
  • 使用 extglob shell 选项,我们启用 extended patterns 并可以从字符串末尾删除最长的 一个或多个 数字序列

    shopt -s extglob
    echo "${address%%+([0-9])}"
    # or, with a POSIX character class
    echo "${address%%+([[:digit:]])}"
    

关于bash - 如何删除终端中 ip 地址的最后一个八位字节?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74199977/

相关文章:

debugging - 如何找到仅在Dart程序的Javascript版本中发生的错误?

android - 无法在 android studio Gradle : invalid symbol: 'switch' 中编译项目

linux - WebStorm 在任何 NIX/OSX 系统上的终端加载时均不加载 ~/.bashrc

linux - bash 脚本在 jee 上调用时不起作用

linux - 如何将管道字符串连接到文件中

bash - 重命名目录中具有相同扩展名的多个文件

linux - 创建运行手册以执行 bash 脚本

android - 如何查看 Android Studio 中发生异常的确切位置?

macos - 去安装不使用 zsh

mysql - 不能删除数据库不能创建数据库