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: 17 January 2018, 2:27 PM EST
Location: Galactic Empire
Posts: 3,630
Thanks: 256
Thanked 1,323 Times in 1,240 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 offline Forum Staff  
Moderator
 
Join Date: May 2012
Last Activity: 17 January 2018, 2:10 PM EST
Location: Simplicity
Posts: 3,929
Thanks: 333
Thanked 1,314 Times in 1,184 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: 17 January 2018, 12:17 PM EST
Location: Amsterdam
Posts: 11,704
Thanks: 530
Thanked 3,398 Times in 2,994 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 11: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: 17 January 2018, 11:48 AM EST
Posts: 293
Thanks: 13
Thanked 79 Times in 76 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: 15 January 2018, 11:29 AM EST
Location: Loughborough
Posts: 1,239
Thanks: 372
Thanked 326 Times in 256 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 06: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 offline Forum Staff  
Moderator
 
Join Date: May 2012
Last Activity: 17 January 2018, 2:10 PM EST
Location: Simplicity
Posts: 3,929
Thanks: 333
Thanked 1,314 Times in 1,184 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 05:28 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
Table null is empty priyanka3006 Shell Programming and Scripting 3 04-21-2009 06:40 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



All times are GMT -4. The time now is 04:15 PM.