Parsing null or empty output

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Parsing null or empty output
# 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:
# 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...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Identify empty file with null record

Hi Team, I have a file abc.dat which is a empty file. But it has null record in first line. I need to identify this unique file and handle it separately. scenario 1: abc/dw> wc abc.dat 1 0 1 abc.dat abc/dw> cat abc.dat abc/dw> scenario 2: abc/dw> wc pqr.dat 0 0 0 pqr.dat... (3 Replies)
Discussion started by: kmanivan82
3 Replies

2. Shell Programming and Scripting

How to control a null output in EMC storage?

I dont want to print the output in a EMC VMAX storage if it says "The specified device was not found", however it is not letting me do it. I am trying to run this command: symaccess -sid xxxx list -type storage -devs 1234 output: The specified device was not found I just want the script... (1 Reply)
Discussion started by: prodigy06
1 Replies

3. Shell Programming and Scripting

How to handle NULL value output from ISQL command?

I am using ISQL command in ksh script. Suppose if i get NULL value from the query which i run,how can i handle it? I am getting a NULL result set and the following error is coming. ############### output of isql command for getting the sum of JEs ################ ----------- NULL... (4 Replies)
Discussion started by: Sharma331
4 Replies

4. Shell Programming and Scripting

redirect the audio output to /dev/null

I'm using an text-to-speech synthesis in a script, and I need to redirect it's output to /dev/null how can I do that ? And how to redirect the stream to his normal output then (sound card ) ? thankx (2 Replies)
Discussion started by: firelink
2 Replies

5. Shell Programming and Scripting

Redirect system output to null in perl

Hi Guys, Please help me.. it is urgent. I am writing a perl script to capture command output and redirect it to a logfile.At the same i want to check the return code of the command and log it if the command is not succesful in my logfile.. Here is my code, it is working but system command inside... (2 Replies)
Discussion started by: sriramperumalla
2 Replies

6. Shell Programming and Scripting

PHP: how can I delete empty/NULL elements from a multi-dimensional array.

Hi all I have a file that i'm running and exec(cat ./dat) against..and putting its contents into any array, then doing an exploding the array into a multi-dimension array... The 15 multi-dimensional arrays have elements that are null/empty, I would like to remove/unset these elements and then... (2 Replies)
Discussion started by: zeekblack
2 Replies

7. UNIX for Dummies Questions & Answers

cp output /dev/null results in not a directory

Hello, I am working on a script to measure the read performance of a busybox environment. The logical choice is to use a command line like: (time cp * /dev/null) 2> /tmp/howlong.txt Ah, the rub is cp or /dev/null will only accept a single file at a time. The result in the txt file is and... (1 Reply)
Discussion started by: stevesmo
1 Replies

8. Shell Programming and Scripting

Table null is empty

hi I m executing a shell script in which records are to be fetched from a table. If the sql that is running empty result set it is displaying "table null is empty !". I dont want this msg on the standard output. COuld you please help me in this regard (3 Replies)
Discussion started by: priyanka3006
3 Replies

9. Shell Programming and Scripting

Removing Null data in output

Hello all, I have a script that has an infile with system package information. For the most part the script is looking well. The only thing i need help is in testing for null entries and removing null data. #!/usr/bin/ksh for i in `cat /mwncps/bin/eco_pack` do NAME=`pkginfo -l |... (2 Replies)
Discussion started by: liketheshell
2 Replies

10. Shell Programming and Scripting

How to check for null or empty string

Hi, I need to check for value not equal (<>) to 21 and not equal empty or null values. Please modify this script if then echo "$VALUE,$BSC_NAME,$BSC_ID" > $OUT_FILE/power_up.out end if TQ (5 Replies)
Discussion started by: doer
5 Replies
Login or Register to Ask a Question