The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
EOF checking the below ramkrix High Level Programming 10 03-10-2008 10:43 PM
checking for non-zero value philplasma UNIX for Dummies Questions & Answers 6 01-08-2008 01:51 PM
checking uid filthymonk Shell Programming and Scripting 7 07-19-2007 07:40 PM
Checking for PXE maestro@altiris SUN Solaris 5 05-24-2004 09:06 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 06-28-2005
Registered User
 

Join Date: Feb 2005
Posts: 48
Checking variables

Firstly - aplogies to Vino

I need to check wether or not a variable called DATE is actually a date by doing the following - checking it is all numeric and secondly checking if it is 8 digits long.

Any help appreciated
Reply With Quote
Forum Sponsor
  #2  
Old 06-28-2005
Registered User
 

Join Date: Feb 2005
Posts: 48
clarification

Just to clarify my previous post - is there any function within UNIX that would allow me to check if something is 8 digits long etc. apart from grep
Reply With Quote
  #3  
Old 06-28-2005
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,699
Quote:
Originally Posted by penfold
Firstly - aplogies to Vino
penfold,

No ill-feelings from my side. And I hope none from yours too.

Just letting you know on the rules that are followed in the forum.

Quote:
Originally Posted by penfold
I need to check wether or not a variable called DATE is actually a date by doing the following - checking it is all numeric and secondly checking if it is 8 digits long.

Any help appreciated
Code:
echo $DATE | sed -n -e '/^[0-9]\{8\}$/p'
If there exists $DATE and is an 8-digit number, it will print the contents of DATE.

Vino
Reply With Quote
  #4  
Old 06-28-2005
Registered User
 

Join Date: Feb 2005
Posts: 48
#!/bin/sh

date1=01234567

echo $date1

echo $DATE | sed -n -e '/^[0-9]\{8\}$/p'

>> this produces the following

01234567
./check.sh: /: cannot execute
./check.sh: [0-9]{8}$/p: not found
sed: option requires an argument -- e


any ideas?
Reply With Quote
  #5  
Old 06-28-2005
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,699
You should echo $date1 to the sed command. Like this

Code:
#!/bin/sh

date1=01234567

echo $date1

echo $date1 | sed -n -e '/^[0-9]\{8\}$/p'
vino
Reply With Quote
  #6  
Old 06-28-2005
Registered User
 

Join Date: Feb 2005
Posts: 48
This works great - thanks

What I need to do is to be able to echo 'DATE incorrect' if it is not 8 digits long or 'DATE correct' if it is..any ideas how to do this..

many thanks
Reply With Quote
  #7  
Old 06-28-2005
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,699
I am lost on how to use sed along with &&. Probably somneone could help me out.

So a grep solution.

Code:
 echo $DATE | grep -q '/^[0-9]\{8\}$/'  && echo "DATE correct" || echo "DATE incorrect"
Vino

Last edited by vino; 06-28-2005 at 05:15 AM.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 05:15 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0