Handling null Integer/string variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Handling null Integer/string variables
# 1  
Old 03-05-2012
Network Handling null Integer/string variables

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.

Code:
#!/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.

Last edited by joeyg; 03-05-2012 at 03:21 PM.. Reason: Please wrap scripts and data with CodeTags
# 2  
Old 03-05-2012
A much easier way, and a way that can be handled in any shell not just BASH, is to use -z to check for blank variables.

Code:
if [ -z "$VARIABLE" ]
then
...
else
...
fi

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 03-05-2012
Quote:
LAN_VARIABLE= echo `ps -ef | grep mintty | awk '{print $2}'`
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" .

My guess for the first fix (untested):
Code:
LAN_VARIABLE=$(ps -ef | grep mintty | grep -v grep |awk '{print $2}')

This User Gave Thanks to methyl For This Post:
# 4  
Old 03-05-2012
Also eliminate the grep -v grep and save a process by doing this:
Code:
LAN_VARIABLE=$(ps -ef | grep "[m]intty" | awk '{print $2}')

Matches the string "mintty" but not itself, thus eliminating the need to grep -v grep..

Last edited by gary_w; 03-05-2012 at 08:16 PM.. Reason: Corrected thanks to Alister's comment below.
This User Gave Thanks to gary_w For This Post:
# 5  
Old 03-05-2012
Quote:
Originally Posted by gary_w
Also eliminate the grep -v grep and save a process by doing this:
Code:
LAN_VARIABLE=$(ps -ef | grep [m]intty | awk '{print $2}')

Matches the string "mintty" but not itself, thus eliminating the need to grep -v grep..
[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.

Regards,
Alister
These 2 Users Gave Thanks to alister For This Post:
# 6  
Old 03-05-2012
Excellent point, thanks. I corrected my example above for future searchers.
This User Gave Thanks to gary_w For This Post:
# 7  
Old 03-06-2012
Hi guys, thanks a lot for so many replies....

It was good solution to use
Code:
ps -ef | grep "[m]intty" | awk '{print $2}'

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.
Code:
LAN_VARIABLE= echo `ps -ef | grep "[m]intty" | awk '{print $2}'`

echo $LAN_VARIABLE always returned null, hence the if loop had the ambiguity.


I used the following and it worked.
Code:
if [-z `ps -ef | grep [m]intty | awk '{print $2}'`]; then
echo "not an Integer"
else
echo "An Integer"
fi


Last edited by Franklin52; 03-06-2012 at 03:09 AM.. Reason: Please use code tags for code and data samples, thank you
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to compare floating variables , integer value expected?

I am running some commands and I am trying to get an output into a variable. I am having problem when I try to put that value in while loop, it says integer value expected. What's the best way to accomplish this remaining=$(symclone -sid XXX -f Clone_test query | grep MB | awk '{print... (1 Reply)
Discussion started by: rajsan
1 Replies

2. Shell Programming and Scripting

how to compare string integer with an integer?

hi, how to I do this? i="4.000" if ; then echo "smaller" fi how do I convert the "4.000" to 4? Thanks! (4 Replies)
Discussion started by: h0ujun
4 Replies

3. Shell Programming and Scripting

Handling Variables

Experts & Learned Folks, Im asking for a solution that Im not sure as how to handle. Owing to reasons of anonymity, I masked some of the text that Im searching for here. Ok. Here it goes. I have an output of script like below - THIS IS THE DATALEFT ON FIRST LINE WITH COUNT 75. THIS IS THE ... (7 Replies)
Discussion started by: ManoharMa
7 Replies

4. Programming

Question on integer variables (c++)

Hello guys! It's orszhak and in my book I am currently studying incrementing values in c++ and it states thant I could do this to increment the value of nVariable nVariable = nVariable + 2; it states that I could also do this and assign the same value nVariable += 2; but can't I also do this and... (1 Reply)
Discussion started by: orszhak
1 Replies

5. Shell Programming and Scripting

Insert string 'NULL' where there is a null value

I have an input file having 7 fields delimited by , eg : 1,ABC,hg,1,2,34,3 2,hj,YU,2,3,4, 3,JU,kl,4,5,7, 4,JK,KJ,3,56,4,5 The seventh field here in some lines is empty, whereas the other lines there is a value. How do I insert string NULL at this location (7th loc) for these lines where... (8 Replies)
Discussion started by: zilch
8 Replies

6. Shell Programming and Scripting

Null Handling in Until loop. . .loop won't stop

Hi Im running this script, which is supposed to find the max value build some tables and then stop running once all the tables are built. Thing is , it keeps assigning a null value to $h and then $g is null so it keep building tables i.e. testupdateNUL. How can I stop this? Here is what I have: ... (4 Replies)
Discussion started by: brandono66
4 Replies

7. Shell Programming and Scripting

handling null values in files

Hi , I have a script where i will remove header and trailer record and ftp to another server. I'm using the code: latestfilename=`ls filename_* | tail -1` echo "Latest filename = $latestfilename" sed '1d;$d' $latestfilename > a.ftpedfile I have an issue if input data is having null... (1 Reply)
Discussion started by: ammu
1 Replies

8. Shell Programming and Scripting

Null Character Handling

Hi All, I have a problem with Null values while reading line by line from a text file. I wrote a shell script to read set of file names from a text file line by line, and zipping the each individual file and copying those zip files into some separate directory, and removing the original file... (3 Replies)
Discussion started by: npk2210
3 Replies

9. Shell Programming and Scripting

Handling null input...

Ok, so when a user inputs nothing, simply pressing enter when prompted for a phone number, I get a "./addrbkfct.sh: test: argument expected" error message. I have the following function: addNumber(){ echo "Phone number: \c"; read number; echo $number; if ;... (2 Replies)
Discussion started by: DrRo183
2 Replies

10. Shell Programming and Scripting

Null handling in scripts

Hi, I face some problem with handling of nulls. I declare a variable - say i - and intialise to 0. Later I read it from console, wherein if I dont give any variable and press return key, I get this error: "0403-004 Specify a parameter with this command" Is there anyway to handle this error? ... (3 Replies)
Discussion started by: mohanprabu
3 Replies
Login or Register to Ask a Question