user3753693 user3753693 - 1 month ago 9
Bash Question

KSH Conditional error

I have spent 24 hours trying to figure out who to do a If statement with parameters in KSH.

My script is :

VAR=failed
if [[$1 -gt $2]];then
$VAR=Success
fi

echo $VAR


and the output i am getting is

$FooBar.ksh 3 1
FooBar.ksh[2]: [[3: not found
failed

Answer

Always put spaces around variables, so :

VAR=failed
if [[ $1 -gt $2 ]]; then
    VAR='Success'
fi

echo "$VAR"

And no $ sigil when you assign a value to a variable

A bit of testing

% ksh script.ksh 1 2
failed

% ksh script.ksh 2 1                                                                                        
Success

Always quote your variables

"Double quote" every literal that contains spaces/metacharacters and every expansion: "$var", "$(command "$var")", "${array[@]}", "a & b". Use 'single quotes' for code or literal $'s: 'Costs $5 US', ssh host 'echo "$HOSTNAME"'. See
http://mywiki.wooledge.org/Quotes
http://mywiki.wooledge.org/Arguments
http://wiki.bash-hackers.org/syntax/words

A last thing

Avoid using upper case for simpl script variables, keep these for system variables