0

I'm very new to the world of bash scripts and require some assistance. I want to increment a variable by 1 when I provide the script with a certain command line input and decrement it by 1 when providing a different command line input but I don't want to be able to decrement lower than zero. How can I go about this?

My thought process is somewhat like this. I need a counter that starts at zero and some way to read the command line input. Then two if statements that check the input and the count and if both are valid then either do the increment or decrement.

count=0
read input
if(input == "in" && count >= 0)
let "count++"
if(input == "out" && count > 0)
let "count--"
echo $count

but I'm not sure about the syntax since I'm very new to bash. Am I on the right track at all? Any help would be appreciated.

2
  • 1
    Few misconceptions have already been addressed in the answers. Here's one more thing: "command line input" usually means positional parameters; read has nothing to do with them, it reads from stdin. Apr 4, 2021 at 15:12
  • ((...)) is preferred over let. See the let builtin command for details. Also the shellcheck wiki entry. Apr 5, 2021 at 13:26

1 Answer 1

2

Please read a basic guide to scripting. I'm afraid your syntax is completely wrong, and it will save you a lot of time to read up on it instead of just trying random things. I realize some of this syntax is obscure, but you really, really want to read up on a language before just blindly trying to write in it. Each language has its own syntax and you won't get far by trying to guess what it is.

First, if statements in bash are ended by fi. Then, you want [ ] or [[ ]] and not parentheses around the if condition. You also need then: if [ condition ]; then action; fi. Next, let "count++" is trying to increment a string ("count++") since you have it in quotes. The >= operator works in arithmetic context, so in (( )) blocks or with the let or declare builtins, but inside if you need -ge instead (see help test).

Here is a working version of your script:

#!/bin/bash

count=0
read -p "Input:" input
if [[ $input = "in" && $count -ge  0 ]]; then
  let count++
elif [[ $input = "out" && $count -gt 0 ]]; then
  let count--
fi
echo $count

Now, of course this will not actually work for decrementing since you are starting with a value of 0 and the script will only ever run once. Therefore, you will never have a situation where the input is out and $count is greater than 0. Perhaps you were thinking of running it in a loop?

#!/bin/bash

count=0
while true; do
  read -p "Input:" input
  if [[ $input = "in" && $count -ge  0 ]]; then
    let count++
  elif [[ $input = "out" && $count -gt 0 ]]; then
    let count--
  fi
  echo "$count"
done

That will run until you kill it with Ctrl+C.

1
  • +1 great first paragraph write-up.
    – Cbhihe
    Apr 5, 2021 at 10:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.