How to write an if-statement within a if-statement - bash

How can I write something like:
if $1 = a then check second statement if $2 is b then echo a and b
else $1 = 1 then check second statement if $2 = 2 then echo 1 and 2
...where all of the variables are strings?
This is what I have:
fun() {
if [ "$1" == "a" ]; # when $1 is a then
then
if [ "$2" == "" ]; # $1 is a and $2 is empty string
echo a
elif [ "$2" == "b" ]; # $1 is a and $2 is b
then
echo "a and b"
fi
fi
else
if [ "$1" == "1" ]; # when $1 is 1 then
then
if [ "$2" == "" ]; # $1 is 1 and $2 is empty string
echo a
elif [ "$2" == "2" ]; #$1 is 1 and $2 is 2
then
echo "1 and 2"
fi
fi
}

Using a nested case statement could help you: Nested case in bash script
Your function would look like this:
fun(){
case "$1" in
"a") # $1 is 'a'
case "$2" in
"") echo "$1";; # only $1 present
"b") echo "a and b";; # $1 is 'a' and $2 is 'b'
esac;;
"1") # $1 is '1'
case "$2" in
"") echo "$1";; # only $1 present
"2") echo "1 and 2";; # $1 is '1' and $2 is '2'
esac;;
esac
}

fun() {
if [ "$1" == "a" ]; # when $1 is a then
then
if [ "$2" == "" ]; # $1 is a and $2 is empty string
then # was missing
echo a
elif [ "$2" == "b" ]; # $1 is a and $2 is b
then
echo "a and b"
fi
# fi # shouldn't be here if you want to have else
else
if [ "$1" == "1" ]; # when $1 is 1 then
then
if [ "$2" == "" ]; # $1 is 1 and $2 is empty string
then
echo a
elif [ "$2" == "2" ]; #$1 is 1 and $2 is 2
then
echo "1 and 2"
fi
fi
fi
}

"then" should be after each "if"
fun() {
if [ "$1" == "a" ]; # when $1 is a then
then
if [ "$2" == "" ]; # $1 is a and $2 is empty string
then #### 1st omitted "then"
echo a
elif [ "$2" == "b" ]; # $1 is a and $2 is b
then
echo "a and b"
fi
# fi #### this fi should be in the end
else
if [ "$1" == "1" ]; # when $1 is 1 then
then
if [ "$2" == "" ]; # $1 is 1 and $2 is empty string
then #### 2nd omitted "then"
echo a
elif [ "$2" == "2" ]; #$1 is 1 and $2 is 2
then
echo "1 and 2"
fi
fi
fi #### here
}

Related

How to bypass an optional argument for the following argument in bash?

TABLE=`echo "${1}" | tr '[:upper:]' '[:lower:]'`
if [ $1 = -d ]
then TABLE=daminundation
elif [ $1 = -b ]
then TABLE=burnscararea
elif [ $1 = -r ]
then TABLE=riverpointinundation
elif [ $1 = " " ]
then echo "User must input -d (daminundation), -b (burnscararea)
or -r (riverpointinundation)."
fi
SHAPEFILEPATH=${2}
MERGEDFILENAME=${3}
if [ -z $3 ] ;
then MERGEDFILENAME=merged.shp
else
MERGEDFILENAME=${3}
fi
COLUMNNAME=${4}
if [ -n $4 ]
then COLUMNNAME=$4
fi
$3 & $4 are optional arguments. However, if I choose not to use $3 but I want to use $4, it will read the command as $3. Confused by other methods, how should I make it so that an undesired optional command can be bypassed for the next one?
You probably want this:
#!/bin/bash
while getopts ":b :d :r" opt; do
case $opt in
b)
TABLE=burnscararea
;;
d)
TABLE=daminundation
;;
r)
TABLE=riverpointinundation
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
esac
done
shift $((OPTIND-1))
[ -z "$TABLE" ] && ( echo "At least one of -b/-d/-r options must be provided"; exit 1; )
[ $# -ne 3 ] && ( echo "3 params expected!"; exit 1; )
SHAPEFILEPATH="$2"
MERGEDFILENAME="$3"
COLUMNNAME="$4"
# other stuff

shell script: integer expression expected

#!/bin/bash
if [$# -ne 1];
then
echo "/root/script.sh a|b"
else if [$1 ='a'];
then
echo "b"
else if [$1 ='b']; then
echo "a"
else
echo "/root/script.sh a|b"
fi
I'm getting below error while run above script in Linux.
bar.sh: line 2: [: S#: integer expression expected
a
Could you please help to remove this error?
if [$# -ne 1];
[ and ] requires spacing. Example:
if [ $# -ne 1 ];
And else if should be elif
#!/bin/bash
if [ "$#" -ne 1 ];
then
echo "/root/script.sh a|b"
elif [ "$1" ='a' ];
then
echo "b"
elif [ "$1" ='b' ]; then
echo "a"
else
echo "/root/script.sh a|b"
fi
Do not forget to quote variables. It is not every time necessary, but recommended.
Question: Why do i have -1?
Bash doesn't allow else if. Instead, use elif.
Also, you need spacing within your [...] expression.
#!/bin/bash
if [ $# -ne 1 ];
then
echo "/root/script.sh a|b"
elif [ $1 ='a' ];
then
echo "b"
elif [ $1 ='b' ]; then
echo "a"
else
echo "/root/script.sh a|b"
fi

Whats wrong with if statement in shell script

Here is my statements:
print "Ss $# $2" >&3
if [ $# -eq 4 || $# -eq 3 ] && [ $2 != "d" ]
then
print "sss"
else
print "lol"
fi
The output is:
Ss 4 s
lol
Why is "sss" not being displayed?
Your if-condition isn't syntactically correct. You can't have || inside the brackets. Change it to use -o instead:
if [ $# -eq 4 -o $# -eq 3 ] && [ $2 != "d" ]
then
print "sss"
else
print "lol"
fi
Or, even better, use [[ (if your shell supports it) which is safer and has more features. It supports ||:
if [[ ( $# -eq 4 || $# -eq 3 ) && $2 != "d" ]]
then
print "sss"
else
print "lol"
fi

Consolidate multiple if statements in Ksh

How can I consolidate the following if statements into a single line?
if [ $# -eq 4 ]
then
if [ "$4" = "PREV" ]
then
print "yes"
fi
fi
if [ $# -eq 3 ]
then
if [ "$3" = "PREV" ]
then
print "yes"
fi
fi
I am using ksh.
Why does this give an error?
if [ [ $# -eq 4 ] && [ "$4" = "PREV" ] ]
then
print "yes"
fi
Error:
0403-012 A test command parameter is not valid.
Try this:
if [[ $# -eq 4 && "$4" == "PREV" ]]
then
print "yes"
fi
You can also try putting them all together like this:
if [[ $# -eq 4 && "$4" == "PREV" || $# -eq 3 && "$3" == "PREV" ]]
then
print "yes"
fi
Do you just want to check if the last argument is "PREV"? If so, you can also do something like this:
for last; do true; done
if [ "$last" == "PREV" ]
then
print "yes"
fi
'[' is not a grouping token in sh. You can do:
if [ expr ] && [ expr ]; then ...
or
if cmd && cmd; then ...
or
if { cmd && cmd; }; then ...
You can also use parentheses, but the semantics is slightly different as the tests will run in a subshell.
if ( cmd && cmd; ); then ...
Also, note that "if cmd1; then cmd2; fi" is exactly the same as "cmd1 && cmd2", so you could write:
test $# = 4 && test $4 = PREV && echo yes
but if your intention is to check that the last argument is the string PREV, you might consider:
eval test \$$# = PREV && echo yes
Try this :
if [ $# -eq 4 ] && [ "$4" = "PREV" ]
then
print "yes"
fi

Bash always printing same value regardless of boolean value

Related to SO.
fizzy.sh:
#!/usr/bin/env sh
div3() {
expr $1 % 3 = 0
}
div5() {
expr $1 % 5 = 0
}
fizzy() {
if [ $(div3 $1) ] && [ $(div5 $1) ]; then
expr "FizzBuzz"
elif [ $(div3 $1) ]; then
expr "Fizz"
elif [ $(div5 $1) ]; then
expr "Buzz"
else
expr "$1"
fi
}
echo $(fizzy 1)
echo $(fizzy 2)
echo $(fizzy 3)
Example:
$ ./fizzy.sh
FizzBuzz
FizzBuzz
FizzBuzz
expr $1 % 3 = 0 yields 1 or 0, depending on whether the result of $1 % 3 is zero or not, but if treats 0 as true, not false.
sh-3.2$ if [ 0 ]; then echo ok; fi
ok
So you'd need to compare the output of your function against 1. Something like this:
#!/usr/bin/env sh
div3() {
expr $1 % 3 = 0
}
div5() {
expr $1 % 5 = 0
}
fizzy() {
if [ $(div3 $1) -eq 1 ] && [ $(div5 $1) -eq 1 ]; then
expr "FizzBuzz"
elif [ $(div3 $1) -eq 1 ]; then
expr "Fizz"
elif [ $(div5 $1) -eq 1 ]; then
expr "Buzz"
else
expr "$1"
fi
}
for (( i = 1; i <= 15; i++ ))
do
echo $(fizzy $i)
done
Without the need for div3 or div5 functions.
fizzbuzz() { # eg: fizzbuzz 10
((($1%15==0))&& echo FizzBuzz)||
((($1%5==0))&& echo Buzz)||
((($1%3==0))&& echo Fizz)||
echo $1;
}
Or you could do it all at once
fizzbuzz() { # eg: fizzbuzz
for i in {1..100};
do
((($i%15==0))&& echo FizzBuzz)||
((($i%5==0))&& echo Buzz)||
((($i%3==0))&& echo Fizz)||
echo $i;
done;
}
If your shell is bash, you don't need to call out to expr:
div3() { (( $1 % 3 == 0 )); }
div5() { (( $1 % 5 == 0 )); }
fizzbuzz() {
if div3 $1 && div5 $1; then
echo FizzBuzz
elif div3 $1; then
echo Fizz
elif div5 $1; then
echo Buzz
else
echo
fi
}
for ((n=10; n<=15; n++)); do
printf "%d\t%s\n" $n $(fizzbuzz $n)
done

Resources