Skip to content

Instantly share code, notes, and snippets.

@yoya
Last active May 26, 2025 09:23
Show Gist options
  • Save yoya/fa904d9495de2c8171cc0dc62498072b to your computer and use it in GitHub Desktop.
Save yoya/fa904d9495de2c8171cc0dc62498072b to your computer and use it in GitHub Desktop.
expr.sh
#! /bin/sh
# (c) 2025/05/26 yoya@awm.jp
# Usage: expr.sh 1 + .. 20 # => 210
# Usage: expr.sh 1 \* .. 5 # => 120
output=()
mode=0
for a in "$@"
do
if [ "$a" == ".." ]; then
n=`expr "${#output[@]}"`
left=${output[`expr $n - 2`]}
op=${output[`expr $n - 1`]}
mode=1 # loop mode
continue
fi
if [ $mode -eq 0 ]; then
output+=("$a")
elif [ $mode -eq 1 ]; then
left_next=`expr $left + 1`
right=$a
for i in `seq $left_next $right`; do
if [ $i -gt $left_next ]; then
output+=("$op")
fi
output+=("$i")
done
mode=0
fi
prev=$a
done
command="expr"
for a in "${output[@]}"
do
command="$command '$a'"
done
eval $command
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment