Unix/Linux Go Back    


UNIX for Beginners Questions & Answers 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

UNIX for Beginners Questions & Answers


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 12-08-2017   -   Original Discussion by andysensible
andysensible's Unix or Linux Image
andysensible andysensible is offline
Registered User
 
Join Date: Dec 2017
Last Activity: 13 December 2017, 11:29 AM EST
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
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
Sponsored Links
    #2  
Old Unix and Linux 12-08-2017   -   Original Discussion by andysensible
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 10 May 2018, 11:06 AM EDT
Location: Galactic Empire
Posts: 3,670
Thanks: 257
Thanked 1,330 Times in 1,246 Posts
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

Sponsored Links
    #3  
Old Unix and Linux 12-09-2017   -   Original Discussion by andysensible
MadeInGermany's Unix or Linux Image
MadeInGermany MadeInGermany is online now Forum Staff  
Moderator
 
Join Date: May 2012
Last Activity: 27 May 2018, 11:24 AM EDT
Location: Simplicity
Posts: 4,086
Thanks: 351
Thanked 1,379 Times in 1,243 Posts
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 Unix and Linux 12-09-2017   -   Original Discussion by andysensible
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 27 May 2018, 11:08 AM EDT
Location: Amsterdam
Posts: 11,812
Thanks: 544
Thanked 3,443 Times in 3,036 Posts
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 View Post
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 View Post
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)
Sponsored Links
    #5  
Old Unix and Linux 12-09-2017   -   Original Discussion by andysensible
apmcd47's Unix or Linux Image
apmcd47 apmcd47 is offline
Registered User
 
Join Date: Feb 2011
Last Activity: 25 May 2018, 10:49 AM EDT
Posts: 343
Thanks: 16
Thanked 96 Times in 91 Posts


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
Sponsored Links
    #6  
Old Unix and Linux 12-09-2017   -   Original Discussion by andysensible
wisecracker's Unix or Linux Image
wisecracker wisecracker is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 27 May 2018, 6:08 AM EDT
Location: Loughborough
Posts: 1,262
Thanks: 377
Thanked 339 Times in 266 Posts
Hi andysensible...
Quote:
Originally Posted by Scrutinizer View Post
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..
Sponsored Links
    #7  
Old Unix and Linux 12-11-2017   -   Original Discussion by andysensible
MadeInGermany's Unix or Linux Image
MadeInGermany MadeInGermany is online now Forum Staff  
Moderator
 
Join Date: May 2012
Last Activity: 27 May 2018, 11:24 AM EDT
Location: Simplicity
Posts: 4,086
Thanks: 351
Thanked 1,379 Times in 1,243 Posts
Quote:
Originally Posted by apmcd47 View Post


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...
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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
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
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 11:44 AM.