0

i'm trying to use getopts to make a command: actu -c [credits to add] to add a value to a variable that is on a .txt file but I don't understand what i'm doing wrong:


#!/bin/bash
set -e
set -u
set -o pipefail
while getopts 'c:' OPTION; do
  case "$OPTION" in
    c)
      c="$OPTARG"
        value=$(<cred.txt)
      value=$((value+c))
        echo "There were added: "$c"  credits, total: "$value""
        echo $value>cred.txt
      ;;
    ?)
echo "actu [-c]" >&2
      exit 1
      ;;
  esac
done
shift "$((OPTIND -1))"

Any help would be apreciated!

henryke9
  • 23
  • 3
  • Not sure, when I copy that code, it works. What is the error message ? – Andre Gelinas May 07 '21 at 14:06
  • Oh, I had while getopts 'cb:' so it was't working thank you! – henryke9 May 07 '21 at 14:17
  • One more thing if you cold help, how would I do for the default number of credits added to be 100 unless the user specified another number. Thank you! – henryke9 May 07 '21 at 14:18
  • You would have to take the lines doing the actual treatment/addition out of the case statement (after the getops' while loop) leaving only "c=$OPTARG", AND have "c=100" before the getops' loop. That way if the user enter a value, "c" will be equal to that value, but if there is no value, 100 will be used. – Andre Gelinas May 07 '21 at 17:07

0 Answers0