I kind of found out the hard way that I am not able to manipulate the null value, the long silence that happens when there is no value returned.
I am looking for PIDs, and when there is no PID return, I wanted to handle this special scenario.
Here is my script.
#!/bin/bash
LAN_VARIABLE= echo `ps -ef | grep mintty | awk '{print $2}'`
echo $LAN_VARIABLE
if [[ $LAN_VARIABLE =~ ^[0-9]+$ ]]; then
echo "An Integer"
else
echo "not an integer"
fi
When there is no mintty process running, i wanted the else loop to be printed and when there is a pid , then the if part to be printed. But it always prints "not an integer", even though the echo $LAN_VARIABLE returns PID.
The reason it always finds a PID is because it's finding the grep.
I don't have "bash" hand to try a fix but the "echo" is not needed or desirable (it introduces an extra line terminator which could be upsetting your numeric comparison). Also the script will misbehave if there is more than one line in "ps -ef" which contains the string "mintty" .
[m]intty is both a valid regular expression for grep and a valid sh pattern for pathname expansion. If you don't quote that argument, and if the current working directory has a matching item, the pathname expansion would consume the brackets before grep is exec'd. The pipeline could give an erroneous result if its grep is listed in ps' output.
Obviously, what I just pointed out is not likely to happen, but it's not impossible. I mention it merely for thoroughness.
This works and gives only one PID. The actual problem I found out was that, the variable never gets a value assigned. The PID value getting printed was from the following step.