Home Man
Search
Today's Posts
Register

If you're not sure where to post a Unix or Linux question, post it here. All unix and Linux beginners welcome in this forum!

Parsing null or empty output

Login to Reply

 
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 10:45 PM..
The Following User Says Thank You to Scrutinizer For This Useful 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 05: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...
Login to Reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Identify empty file with null record kmanivan82 Shell Programming and Scripting 3 07-01-2016 04:28 AM
How to control a null output in EMC storage? prodigy06 Shell Programming and Scripting 1 07-30-2014 09:21 AM
redirect the audio output to /dev/null firelink Shell Programming and Scripting 2 08-18-2012 03:10 AM
Error output of cat to /dev/null Nils88 Shell Programming and Scripting 7 02-05-2011 07:10 AM
PHP: how can I delete empty/NULL elements from a multi-dimensional array. zeekblack Shell Programming and Scripting 2 12-05-2009 11:03 PM
tar not reading if output directed to /dev/null bakunin Ubuntu 4 09-09-2009 01:28 AM
cp output /dev/null results in not a directory stevesmo UNIX for Dummies Questions & Answers 1 05-22-2009 03:20 PM
Table null is empty priyanka3006 Shell Programming and Scripting 3 04-21-2009 05:40 AM
Removing Null data in output liketheshell Shell Programming and Scripting 2 08-13-2007 04:59 PM
How to check for null or empty string doer Shell Programming and Scripting 5 07-24-2007 01:31 AM


All times are GMT -4. The time now is 01:49 AM.

Unix & Linux Forums Content Copyrightę1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password