0

I have a file that contains a number. I want to read this number into a variable and then use it in an if statement. I am using the following command to populate the variable:

step='cat ./update_step'

The file, update_step has a single number stored in it. The number can be 0, 1, 2, 3, 4, or 5. For the sake of this example, the file contains the number "0".

if I check the variable as so:

$step

Then I get "0" as a return; which is expected.

But then if I try to use $step in an if statement like:

if [ $step -eq 0 ]
then
    echo "this is an integer"
fi

I get a, "too many arguments" error.

If I check the variable with echo:

echo "$step"

Then the variable returns "cat ./update_step"

How do I read in the number that is stored in update_step as an integer (honestly it could even be a string at this point) so that I can use it with an if statement?

Ken
  • 125
  • 1
  • 12
  • 1
    `step='cat ./update_step'; echo $step` will echo `cat ./update_step`, not "0". – jordanm May 18 '22 at 16:14
  • 1
    see the accepted answer here: https://stackoverflow.com/questions/7427262/how-to-read-a-file-into-a-variable-in-shell – j_b May 18 '22 at 16:18
  • Running `$step` _doesn't_ "check the variable". It _parses the content of the variable as a command_ ([badly](https://mywiki.wooledge.org/BashFAQ/050)), and runs that command. – Charles Duffy May 18 '22 at 16:55

1 Answers1

-1

Try

step=$(cat ./update_step)

echo $step
malkaroee
  • 187
  • 1
  • 6
  • 1
    Code only answers are considered low quality. Without sufficient explaination, your answer is hard to understand. If the OP can't understand your answer, then he also won't be able to reproduce your possible solution. As such it would be worthless to him/her. Please add a sufficient explaination of your possible solution. – tacoshy May 21 '22 at 03:37