I have a variable, vbLevel and want to assign it to one of several values which are "none", "low", "medium", "high".
Then I want to do something like
if [ vbLevel -gt low ]; then
...
fi
I have a variable, vbLevel and want to assign it to one of several values which are "none", "low", "medium", "high".
Then I want to do something like
if [ vbLevel -gt low ]; then
...
fi
your text comparing doesn't makes sense to me. Please elaborate more
If you like to perform some action based in text value following code will help you
for var in none low medium high;
do
case $var in
none)
<your statements here>
;;
low)
<your statements here>
;;
medium)
<your statements here>
;;
high)
<your statements here>
;;
esac
done
You can do this in ksh93 and bash, without the dollar signs I mean...
none=0 low=1 medium=2 high=3
vbLevel=2
if [[ vbLevel -gt low ]]; then
echo hello
fi
The assign can also be done with the enumerated type let vbLevel=medium
Indeed. let is an arithmetic evaluation. A synonymous notation would be (ksh/bash):
(( vbLevel=medium ))
And you can also do this:
none=0 low=1 medium=2 high=3
vbLevel=2
if (( vbLevel > low )); then
echo hello
fi
So one does not use the $ sign for the variables?
Inside the double parentheses you are allowed to leave the $-signs out if you wish..
Anything that uses bash expressions does not require the $ sign eg:
let expression
(( expression ))
[[ expression ]]
Anywhere else will eg:
find . -mtime +$vbLevel
if [ $vbLevel -eq $none ]
case $vbLevel in
($medium) echo yes ;;
esac
Any idea why
printf "-- Name --"
gives invalid option and how can I make it print without error
---------- Post updated at 07:02 PM ---------- Previous update was at 06:58 PM ----------
This seems to work. I rather set vbLevel to none, medium, ... and do as below. or is it better using
(( vbLevel=medium ))
vbLevel=$medium
if (( vbLevel > low )); then
echo hello
fi
This works too
vbLevel=$medium
if [ $vbLevel -gt $low ]; then
echo hello2
fi
---------- Post updated at 07:10 PM ---------- Previous update was at 07:02 PM ----------
I want to stick with the one below. Would this be ok?
vbLevel=$medium
if (( vbLevel > low )); then
echo hello1
fi
---------- Post updated at 07:23 PM ---------- Previous update was at 07:10 PM ----------
I get the user to supply none, low, medium and high from a user option.
Example
/home/chrisd/srv/sunny/hstmy/bin/bash/raytrac.bash --vblv=high
My code then uses
vbLevel=$medium
if [ $hasArgument_vbLevel == "true" ]; then
vbLevel=$value_vbLevel
fi
echo "vbLevel = $vbLevel"
if (( vbLevel > medium )); then
echo "Hello done"
fi
Thus I replace vbLevel by the user string, which is either none, low, medium or high which then gets associated with the number and then I can do
if (( vbLevel > medium )); then
---------- Post updated at 07:31 PM ---------- Previous update was at 07:23 PM ----------
I thought to use indirect substitution for this
vbLevel=${!medium} # Indirect substitution
if [ $hasArgument_vbLevel == "true" ]; then
vbLevel=${!value_vbLevel} # Indirect substitution
fi
if (( vbLevel > medium )); then
echo "Hello done"
fi
Try:
printf "%s" "-- Name --"