chrisrth
chrisrth

Reputation: 1212

Shell Conditional Issues

I have searched and tried numerous solutions but being new to shell I am lost. I am setting a variable in one .sh, calling a second, then trying to act on whether it is true or not.

testA.sh
#!/bin/sh

DB=1
. testB.sh

------------------------------------

testB.sh

if ${DB}==1; then
echo "DB is on"
fi

I get 1==1 command not found.

Upvotes: 1

Views: 92

Answers (4)

William Pursell
William Pursell

Reputation: 212298

if you want to test whether or not a variable is set, the standard way is:

if test "${DB+set}" = set; then
  # here, $DB is set
fi

If you want to check that it is set to a particular string value, use:

if test "$DB" = value; ...

if you want to check for a particular integer value:

if test "$DB" -eq 4; ...

The last form will generate an error message if DB is a string value that does not look like an integer.

Upvotes: 1

theglauber
theglauber

Reputation: 29625

should be:

if [[ "$DB" = "1"]]; then

or

if [[ $DB -eq 1 ]]; then

Upvotes: 5

Eugen Rieck
Eugen Rieck

Reputation: 65294

testB.sh

if test ${DB} == 1; then
echo "DB is on"
fi

Upvotes: 0

ChristopheD
ChristopheD

Reputation: 116207

That's not the way you do equality testing in Bash...

Upvotes: 2

Related Questions