0

I wrote a function to read the time from a config file and calculate it to seconds.

The line in the config file looks like that:

timeend=30h

The function was written in for bash, but in the Linux Distro I use there's no bash avalaible, only sh and ash.

In sh and ash, it seems that I can't use the -1.
How do I need to change the code to get it working in ash or sh?

getTimeEndFromConfig(){
case $timeend in
*s )
echo ${timeend::-1};;
*m )
zeit=${timeend::-1}
TIMEEND=$(($zeit*60))
echo ${TIMEEND};;
*h )
zeit=${timeend::-1}
TIMEEND=$(($zeit*3600))
echo ${TIMEEND};;
esac
}

Update
So I changed it, but now I always get an arithmetic syntax error when I call the funtion.

aha364636
  • 365
  • 5
  • 23

1 Answers1

0

${timeend::-1} is a bash extension (in fact a relatively recent one with negative offsets).

POSIX shell supports the following syntax:

zeit=${timeend%?}

In this context, ? means any character and the % means remove the shortest occurrence of the pattern from the end of the string.

Alternatively, you could use sed:

zeit=$(printf '%s' "$timeend" | sed 's/.$//')

This also removes the last character from the string.

In older shells that doesn't support the arithmetic expansion, I'd suggest going with awk:

echo "$(printf '%d' "${timeend%?}" | awk '{print $1 * 3600 }')"

Here I combined the substring extraction and the multiplication in the final branch of your case statement.

Sevle
  • 3,109
  • 2
  • 19
  • 31
Tom Fenech
  • 72,334
  • 12
  • 107
  • 141
  • Or just `zeit=${timeend%?}` – tripleee Dec 21 '15 at 09:37
  • @tripleee didn't realise that worked in POSIX shell but if it does, then yes! – Tom Fenech Dec 21 '15 at 09:39
  • It absolutely does. The `%%` variant is a Bash extension but plain old `%` and its sister `#` are definitely in POSIX. – tripleee Dec 21 '15 at 09:40
  • @tripleee by the way, looking at the reference I found, it seems like `%%` is also supported in POSIX. – Tom Fenech Dec 21 '15 at 09:46
  • 1
    If I call the function, after I changed it, I always get an arithmetic syntax error. Is the calculation syntax also not supported? – aha364636 Dec 21 '15 at 09:55
  • Yup, you'll need to use `bc` or Awk or `expr`. Traditionally, `expr` would be the simplest and most idiomatic of these, but it's kind of horrible. – tripleee Dec 21 '15 at 09:57
  • Thanks for the POSIX reference, and apologies for the misleading tangent. – tripleee Dec 21 '15 at 09:58
  • Both POSIX `sh` and (according to [this](http://linux.die.net/man/1/ash)) `ash` support `$((...))` for arithmetic expressions. – chepner Dec 21 '15 at 14:13
  • @chepner I thought so too - I guess that the syntax hasn't been around forever though, so maybe it's not supported on the OP's version. – Tom Fenech Dec 21 '15 at 14:21
  • 2
    The fact that the error reads `arithmetic syntax error` implies that the shell understands `$((...))`; it just doesn't understand what's inside. I'd examine the resulting value of `zeit` more closely to make sure it really is just a number. – chepner Dec 21 '15 at 17:58