gpt4 book ai didi

linux - bash 创建一个将名称作为参数并递增结果的函数

转载 作者:太空宇宙 更新时间:2023-11-04 10:18:55 25 4
gpt4 key购买 nike

我想创建一个函数,例如,如果我输入一个名为 myfunc arsenal1 的函数,我得到的输出为 arsenal1_2

最佳答案

执行:

#!/usr/bin/env bash

increment() {
n=`echo "$1" | sed -r 's/.*([0-9]+)$/\1/'`
echo "$1_$((n+1))"
}

increment arsenal1
increment arsenal09
increment 10arsenal1

输出:

arsenal1_2
arsenal09_10
10arsenal1_2

关于linux - bash 创建一个将名称作为参数并递增结果的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44756396/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com