将秒转换为天小时分钟秒

Sha*_*ava 1 bash time sh

执行代码的以下部分时,出现错误:语法错误:需要操作数(错误标记为“/(60*60*24)”)

# \function  convertsecs2dhms
# \brief     convert seconds to days hour min sec
#####################################################################################################################

function convertsecs2dhms()
{
    ((d=${1}/(60*60*24)))
    ((h=(${1}%(60*60*24))/(60*60)))
    ((m=(${1}%(60*60))/60))
    ((s=${1}%60))
     printValue=`printf "%02d days %02d hours %02d minutes %02d seconds \n" $d $h $m $s`
     printInfo "$printValue"
}
Run Code Online (Sandbox Code Playgroud)

这是我收到的错误:

# \function  convertsecs2dhms
# \brief     convert seconds to days hour min sec
#####################################################################################################################

function convertsecs2dhms()
{
    ((d=${1}/(60*60*24)))
    ((h=(${1}%(60*60*24))/(60*60)))
    ((m=(${1}%(60*60))/60))
    ((s=${1}%60))
     printValue=`printf "%02d days %02d hours %02d minutes %02d seconds \n" $d $h $m $s`
     printInfo "$printValue"
}
Run Code Online (Sandbox Code Playgroud)

tri*_*eee 5

仅双括号不足以选择 pure 中的算术上下文sh

# don't use non-portable "function" keyword
convertsecs2dhms () {
    # use $((...)) for arithmetic
    d=$((${1}/(60*60*24)))
    h=$(((${1}%(60*60*24))/(60*60)))
    m=$(((${1}%(60*60))/60))
    s=$((${1}%60))
    # use printf -v
    printf -v printValue "%02d days %02d hours %02d minutes %02d seconds \n" $d $h $m $s
    printInfo "$printValue"
}
Run Code Online (Sandbox Code Playgroud)

另请参阅https://mywiki.wooledge.org/ArithmeticExpression但请注意,其中大部分描述的是 Bash,而不是 POSIX sh