Checking variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Checking variables
# 1  
Old 06-28-2005
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
# 2  
Old 06-28-2005
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
# 3  
Old 06-28-2005
Quote:
Originally Posted by penfold
Firstly - aplogies to Vino
penfold,

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

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
# 4  
Old 06-28-2005
#!/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?
# 5  
Old 06-28-2005
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
# 6  
Old 06-28-2005
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
# 7  
Old 06-28-2005
I am lost on how to use sed along with &&. Probably somneone could help me out. Smilie

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 09:15 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to pass variables into anothother variables?

Below are three variables, which I want to pass into variable RESULT1 username1=userid poihostname1=dellsys.com port1=8080 How can I pass these variables into below code... RESULT1=$((ssh -n username1@poihostname1 time /usr/sfw/bin/wget --user=sam --password=123 -O /dev/null -q... (4 Replies)
Discussion started by: manohar2013
4 Replies

2. Shell Programming and Scripting

Passing awk variables to bash variables

Trying to do so echo "111:222:333" |awk -F: '{system("export TESTO=" $2)}'But it doesn't work (2 Replies)
Discussion started by: urello
2 Replies

3. Shell Programming and Scripting

BASH arrays and variables of variables in C++

Sometimes it is handy to protect long scripts in C++. The following syntax works fine for simple commands: #define SHELLSCRIPT1 "\ #/bin/bash \n\ echo \"hello\" \n\ " int main () { cout <<system(SHELLSCRIPT1); return 0; } Unfortunately for there are problems for: 1d arrays:... (10 Replies)
Discussion started by: frad
10 Replies

4. Shell Programming and Scripting

Running a script with multiple variables like 25 variables.

Hi All, i have a requirement where i have to run a script with at least 25 arguements and position of arguements can also change. the unapropriate way is like below. can we achieve this in more good and precise way?? #!/bin/ksh ##script is sample.ksh age=$1 gender=$2 class=$3 . . .... (3 Replies)
Discussion started by: Lakshman_Gupta
3 Replies

5. SCO

Stop boot system at "Checking protected password and checking subsystem databases"

Hi, (i'm sorry for my english) I'm a problem on boot sco unix 5.0.5 open server. this stop at "Checking protected password and checking subsystem databases" (See this image ) I'm try this: 1) http://www.digipedia.pl/usenet/thread/50/37093/#post37094 2) SCO: SCO Unix - Server hangs... (9 Replies)
Discussion started by: buji
9 Replies

6. Shell Programming and Scripting

checking variables + arithmetic operator help

i'm trying to make a script to simply add numbers together for example i have a file script file called add add 1 2 3 4 5 15 however i want the user to put at least 3 numbers so i did this if then echo "please enter more than two numbers" exit 1 fi but it's telling me i have... (3 Replies)
Discussion started by: el3ctr0nic47
3 Replies

7. Shell Programming and Scripting

Help with checking that 2 variables contain matching characters

hi i am writing a hangman script and am having trouble checking the correct letters against the word i need the script to compare the word against the letters guessed that are correct so once all the letters within the word have been guessed it will alow me to create a wining senario eg ... (3 Replies)
Discussion started by: lsecer
3 Replies

8. Programming

How to convert byteArray variables to HexaString variables for Linux?

Hello everybody, I am having problem in converting byte array variables to Hexa String variables for Linux. I have done, converting byte array variables to Hexa String variables for Windows but same function doesn't work for linux. Is there any difference in OS ? The code for Windows is given... (2 Replies)
Discussion started by: ritesh_163
2 Replies

9. Shell Programming and Scripting

naming variables with variables

Hello, FIRST QUESTION: I am writing a script in which a query is taken at the beginning of the script to be later used at the end. In the query, variables are generated from a loop, and I would like to assign the variable NAME (not value) with an appended 1, 2, 3, 4.....n. The number of... (2 Replies)
Discussion started by: Allasso
2 Replies

10. UNIX for Dummies Questions & Answers

Using Variables to Set Other Variables

I have a script that I'm trying to shorten (below) by removing repetitive code. if ] then commodity_ndm_done=Y fi if ] then customer_ndm_done=Y fi if ] then department_ndm_done=Y fi if ] then division_ndm_done=Y fi (3 Replies)
Discussion started by: superdelic
3 Replies
Login or Register to Ask a Question