hello all. I need help creating an if else statement here. Ive tried and have failed so far.
This is the code I have so far:
printf "checking for hidden modules\n"
for mm in /sys/module/*; do
if test -d ${mm}/sections; then
MOD="$(basename ${mm})";
lsmod | grep -E "^${MOD}" > /dev/null || printf "${MOD}\n";
fi;
done
which prints out like this:
checking for hidden modules
any hidden modules here...
I would like it to print like this
checking for hidden modules [ok] \(or) [name of module]
#!/bin/bash
printf "checking for hidden modules: "
for mm in /sys/module/*; do
if test -d ${mm}/sections; then
MOD="$(basename ${mm})";
lsmod | grep -E "^${MOD}" > /dev/null || printf "[found hidden module] \n${MOD}\n";
fi;
done
which works as expected. Problem is that:
1) if it doesn't find any modules, i cant figure out with this particular command how to make it say [ok] insted of
[found hidden module]
diamorphine
2) if it doesnt find any modules, it doesn't return a new line. Which would be solved also if I could figure out 1) !
Hi, try using an if statement and a boolean. So try something like:
#!/bin/bash
printf "checking for hidden modules: "
hidden_found=false
for mm in /sys/module/*; do
if test -d ${mm}/sections; then
MOD="$(basename ${mm})";
if lsmod | grep -q "^${MOD} "
then
continue
else
hidden_found=true
printf "[found hidden module] \n${MOD}\n"
fi
fi
done
if [[ $hidden_found == false ]]; then
printf "OK\n"
fi
Note that a space after ^${MOD} ensures that the match is anchored at the end and that there can be no false positives..
Scrutinizer, Thank you!!!! Thats exactly what I was looking for. I will be sure to look it over very well, I already see what I could have done differently.