I try to generate a csv file through a lot of functions like that :
function get_sudo_version {
sudo -V 2>/dev/null|grep -i "sudo version"
}
sudo_version=$(get_sudo_version)
Function above is a simple example but in some cases i cannot be sure the output is correct.
I would like to know what is the best way to validate the function return one text line only.
I thought about something like that
function validate_output {
output=$1;
echo $1|grep -q "\n";
echo $?;
}
mytest="val1
err2
err3"
But it’s obviously not working because the variable does not keep the retrun line character:
echo $mytest
val1 err2 err3
So if someone has a good idea of how i could wirte a generic check function i would be glad.
Thanks
If you have GNU grep, you could simply ensure that grep doesn’t produce more than one line of output in the first place via
grep -m 1. Alternatively, usesed '/sudo version/!d;q'instead ofgrep.A function that simply checks lines of input while passing them through might look like:
Many variations on that possible of course. IMO it’s pretty pointless and shouldn’t be used for something like this. Just design your
get_sudo_versionso that it guarantees the right results.