if you notice in @michaelk post the use of { } curly brackets. it is a got a be to show the contents of inside an array. to get the everything in the array{@] is used
array.length for conditional in a loop is better then a hard coded number because that gives the number you want to end with if going through the entire array elements. Because what you stated is in error,
This is the current iteration of the if loop.
"if [conditions]" are not loops.
Code:
for (( i = 0 ; i < "${#array[@]}" ; i++ ))
{
echo "${array[i]}"
}
point is the use of the # and @ in an array. Along with { curly brackets }
this too can be taken care of so you do not have to worry about Caps in first letter words.
Code:
echo "Please select the unit of measurement: Percent, Gigabytes, Count, etc. Make sure first letter is capitalized."
Code:
if [ "${UNIT,,}" =~ "${unitarray[@],,}" ]; then
this lower cases everything.
example
Code:
"Solid" Variable
userx%slackwhere ⚡ ~ ⚡> var="TheEW Thoe"
userx%slackwhere ⚡ ~ ⚡> var2=${var,,}
userx%slackwhere ⚡ ~ ⚡> echo $var2
theew thoe
an Array
userx%slackwhere ⚡ ~ ⚡> var3=(t t t t R ewe Trush)
userx%slackwhere ⚡ ~ ⚡> var4=${var3[@],,}
userx%slackwhere ⚡ ~ ⚡> echo $var4
t t t t r ewe trush
userx%slackwhere ⚡ ~ ⚡>
example 2
Code:
userx%slackwhere ⚡ ~ ⚡> var="HeLLo YoU"
userx%slackwhere ⚡ ~ ⚡> var1=(HELLO you)
userx%slackwhere ⚡ ~ ⚡> [[ "${var,,}" =~ "${var1[@],,}" ]] && echo "hey got a match"
hey got a match
userx%slackwhere ⚡ ~ ⚡> [[ "${var1[@],,}" =~ "${var,,}" ]] && echo "hey got a match"
hey got a match
as you can see on either end it still matches.
Now to nit pic your code. So you hopefully to pick up on a few things.
Hope you don't mind.
your code
Code:
echo "Please select the unit of measurement: Percent, Gigabytes, Count, etc. Make sure first letter is capitalized."
read UNIT
unitarray=(Seconds Microseconds Milliseconds Bytes Kilobytes Megabytes Gigabytes Terabytes Bits Kilobits Megabits Gigabits Terabits Percent Count Bytes/Second Kilobytes/Second Megabytes/Second Gigabytes/Second Terabytes/Second Bits/Second Kilobits/Second Megabits/Second Gigabits/Second Terabits/Second Count/Second)
if [[ ! $UNIT =~ $unitarray ]]; then
echo "Please select a unit of measurement from the following list:"
printf '%s\n' "${unitarray[@]}"
exit 1
fi
can you justify the need for the use of an array when you can just make that a hard coded string?
because of this read UNIT that is the real variable yes? Whereas what you are using as an array is hard coded and will not change during run time, YES?
Code:
unitarray=(Seconds Microseconds Milliseconds Bytes Kilobytes Megabytes Gigabytes Terabytes Bits Kilobits Megabits Gigabits Terabits Percent Count Bytes/Second Kilobytes/Second Megabytes/Second Gigabytes/Second Terabytes/Second Bits/Second Kilobits/Second Megabits/Second Gigabits/Second Terabits/Second Count/Second)
which can be written just using " " quotes and the same conditional comparison used without an array.
Code:
unitarray="Seconds Microseconds Milliseconds Bytes Kilobytes Megabytes Gigabytes Terabytes Bits Kilobits Megabits Gigabits Terabits Percent Count Bytes/Second Kilobytes/Second Megabytes/Second Gigabytes/Second Terabytes/Second Bits/Second Kilobits/Second Megabits/Second Gigabits/Second Terabits/Second Count/Second"
too
Code:
read -p "Please select the unit of measurement: Percent, Gigabytes, Count, etc." UNIT
puts it on one line. less for the interpreter to evaluate. Faster run times. But you may not even need that whole string anymore. So I Shortened It In This Example.
one more thing, the use of string and substring when comparing strings. where to put which one?
Ok if you want to know this then pls read it.
test script
Code:
#!/bin/bash
substring="Terabits"
unitarray="Seconds Microseconds Milliseconds Bytes Kilobytes Megabytes Gigabytes \
Terabytes Bits Kilobits Megabits Gigabits Terabits Percent Count Bytes/Second Kilobytes \
/Second Megabytes/Second Gigabytes/Second Terabytes/Second Bits/Second Kilobits/Second Megabits/Second \
Gigabits/Second Terabits/Second Count/Second"
echo "$unitarray"
if [[ ${unitarray,,} =~ ${substring,,} ]] ; then
echo "got a match"
else
echo "nO match"
fi
notice the use of the
\ to split the line so you can move it down onto another line and still have it read as one complete line. Along with no use of an array.
results
Code:
userx%slackwhere ⚡ scripts ⚡> ./LongString
Seconds Microseconds Milliseconds Bytes Kilobytes Megabytes Gigabytes Terabytes Bits Kilobits Megabits Gigabits Terabits Percent Count Bytes/Second Kilobytes /Second Megabytes/Second Gigabytes/Second Terabytes/Second Bits/Second Kilobits/Second Megabits/Second Gigabits/Second Terabits/Second Count/Second
got a match