Parsing null or empty output

 
Thread Tools Search this Thread
# 1  
Old 12-08-2017
Parsing null or empty output

I am working an some if then statements for a script.

I want to be able to check for alpha characters or empty out put then exit out.

Code:
if [[ $lastseen = [A-Z] ]]; 
echo "Serial Number Invaild"
then exit 3;

How do I account if the output is empty or null in this in this statement.

Many thanks
# 2  
Old 12-08-2017
In bash, for regular expression matching, you have to use a =~ regular expression matching operator

For checking if string is empty, you can use a -z string test operator

So you can try:-
Code:
if [[ "$lastseen" =~ '^[a-ZA-Z]*$' ]] && [ ! -z "$lastseen" ]
then
        echo "Serial Number Valid"
else
        echo "Serial Number Invaild"
        exit 3
fi

# 3  
Old 12-09-2017
With a shell glob you need two conditions
Code:
if [[ -z $lastseen ]] || [[ $lastseen == *[_a-zA-Z]* ]]
then
  echo "empty or has an alpha character"
fi

Code:
if [[ -z $lastseen ]] || [[ $lastseen == *[!0-9]* ]]
then
  echo "empty or has a non-digit character"
fi

# 4  
Old 12-09-2017
If the requirement is that only digits may occur, you can test for the negation of a string of 1 or more digits instead:
Code:
if ! [[ $lastseen =~ ^[0-9]+$ ]]; then
  echo "Serial Number is invalid: it does not consist entirely of digits"
  exit 3
fi

With regular patterns you could use:
Code:
case $lastseen in 
  (*[!0-9]*|"") 
    echo "Serial Number Invalid: it does not consist entirely of digits"
    exit 3
  ;;
esac



---
Quote:
Originally Posted by MadeInGermany
With a shell glob you need two conditions
Code:
if [[ -z $lastseen ]] || [[ $lastseen == *[_a-zA-Z]* ]]
then
  echo "empty or has an alpha character"
fi

Code:
if [[ -z $lastseen ]] || [[ $lastseen == *[!0-9]* ]]
then
  echo "empty or has a non-digit character"
fi

You can use one condition like so:

Code:
if [[ -z $lastseen || $lastseen == *[_a-zA-Z]* ]]; then

---

Quote:
Originally Posted by Yoda
In bash, for regular expression matching, you have to use a =~ [..]

So you can try:-
Code:
if [[ "$lastseen" =~ '^[a-ZA-Z]*$' ]] && [ ! -z "$lastseen" ]
then
        echo "Serial Number Valid"
else
        echo "Serial Number Invaild"
        exit 3
fi

To use regexes you need to leave them unquoted. But even without the quotes, this would seem to do the opposite of what is required. The left condition tests whether a string consists exclusively of alpha characters (letters) or is empty, whereas the requirement is that no non-digits may occur.

Last edited by Scrutinizer; 12-11-2017 at 11:45 PM..
This User Gave Thanks to Scrutinizer For This Post:
MadeInGermany (12-10-2017)
# 5  
Old 12-09-2017
Code:
if [[ ${lastseen:-a} =~ [^0-9] ]]
then 
   echo "Serial number invalid"
   exit 3
fi

If lastseen is null or unset replace with an invalid string sequence. In this case the letter a will suffice.

Andrew
# 6  
Old 12-09-2017
Hi andysensible...
Quote:
Originally Posted by Scrutinizer
You can test for numeric instead:
With regular patterns you could use:
Code:
case $lastseen in 
  (*[!0-9]*|"") 
    echo "Serial Number Invalid"
    exit 3
  ;;
esac

An additional note to Scrutinzer's post...

This is the only reply, in the few that have been posted, that is fully POSIX compliant and will work in 'sh' and 'dash' too, thus making it basically portable...

Last edited by wisecracker; 12-09-2017 at 06:52 PM..
# 7  
Old 12-11-2017
Quote:
Originally Posted by apmcd47
Code:
if [[ ${lastseen:-a} =~ [^0-9] ]]
then 
   echo "Serial number invalid"
   exit 3
fi

If lastseen is null or unset replace with an invalid string sequence. In this case the letter a will suffice.

Andrew
I call this a trick.
Nice somehow, but I am not going to recommend that as a common practice...

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Identify empty file with null record kmanivan82 Shell Programming and Scripting 3 07-01-2016 05:28 AM
How to control a null output in EMC storage? prodigy06 Shell Programming and Scripting 1 07-30-2014 10:21 AM
How to handle NULL value output from ISQL command? Sharma331 Shell Programming and Scripting 4 07-09-2014 07:43 AM
Aligning output with null fields in shell script gvolpini Shell Programming and Scripting 6 09-10-2012 09:54 PM
redirect the audio output to /dev/null firelink Shell Programming and Scripting 2 08-18-2012 04:10 AM
Redirect system output to null in perl sriramperumalla Shell Programming and Scripting 2 08-29-2011 08:02 AM
Error output of cat to /dev/null Nils88 Shell Programming and Scripting 7 02-05-2011 08:10 AM
Output only non-empty arguments moderwarfare Shell Programming and Scripting 6 09-10-2010 02:30 AM
PHP: how can I delete empty/NULL elements from a multi-dimensional array. zeekblack Shell Programming and Scripting 2 12-06-2009 12:03 AM
tar not reading if output directed to /dev/null bakunin Ubuntu 4 09-09-2009 02:28 AM
cp output /dev/null results in not a directory stevesmo UNIX for Dummies Questions & Answers 1 05-22-2009 04:20 PM
Table null is empty priyanka3006 Shell Programming and Scripting 3 04-21-2009 06:40 AM
The output file is empty. Please Help balzzz UNIX for Dummies Questions & Answers 4 02-01-2008 01:15 AM
Removing Null data in output liketheshell Shell Programming and Scripting 2 08-13-2007 05:59 PM
How to check for null or empty string doer Shell Programming and Scripting 5 07-24-2007 02:31 AM