To verify whether the year has all the digits


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting To verify whether the year has all the digits
# 1  
Old 06-26-2009
To verify whether the year has all the digits

I have a code unit based on which i have this code snippet

Code:
year='2009'
echo year is $year
if [[ $year != @([0-9])@([0-9])@([0-9])@([0-9]) ]]
then
echo 'year in error'
fi
echo end of program

Here i want to find whether all the characters in the variable year are numbers. I am not sure whether this works or not because when i make a small shell out of the above snippet i get the following error

Code:
year is 2009
t1.sh: syntax error at line 3: `(' unexpected

Please let me know what is wrong Smilie

PS: I am not very good at Unix

Last edited by rak007; 06-26-2009 at 03:35 AM..
# 2  
Old 06-26-2009
You can do this way also...
Code:
if [[ ! -z `echo $year | tr -d '[0-9]'` ]]
then
   echo "Error"
fi

# 3  
Old 06-26-2009
^^
This is what i did
Code:
year='2009'
echo year is $year
if [[ ! -z `echo $year | tr -d '[0-9]'` ]]
then
echo 'year in error'
fi
echo end of program

But i got this error

Code:
t1.sh: [[: not found

# 4  
Old 06-26-2009
Which shell you are using ?..

Try with single "[" instead of two.
# 5  
Old 06-26-2009
^ Thanks a million Buddy...its working

Just one more question, if i repace the original IF condition with this revised one then still the meaning of the whole thing remains same right ?

---------- Post updated at 02:42 AM ---------- Previous update was at 02:26 AM ----------

Guys the earlier problem is rectified but a new one has come now

Here s the offending code

Code:
if [[ $myline = [a-zA-Z0-9]*_ACCRUAL_@([0-9])@([0-9])@([0-9])@([0-9])@([0-9])@([0-9])@([0-9])@([0-9]).csv ]]

Here the variable myline contains some file name and i suppose that is being checked for format and all. :0

---------- Post updated at 04:23 AM ---------- Previous update was at 02:42 AM ----------

The above code unit is giving me following errors

Code:
syntax error in conditional expression: unexpected token `('
syntax error near `[a-zA-Z0-9]*_ACCRUAL_@(['

# 6  
Old 06-26-2009
Quote:
Originally Posted by rak007
Here i want to find whether all the characters in the variable year are numbers.
Code:
# echo $year | awk '{print ($0+0==$0)?"ok":"not ok"}'
ok

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

sed / awk script to delete the two digits from first 3 digits

Hi All , I am having an input file as stated below 5728 U_TOP_LOGIC/U_CM0P/core/u_cortexm0plus/u_top/u_sys/u_core/r03_q_reg_20_/Q 011 611 U_TOP_LOGIC/U_CM0P/core/u_cortexm0plus/u_top/u_sys/u_core/r04_q_reg_20_/Q 011 3486... (4 Replies)
Discussion started by: kshitij
4 Replies

2. Red Hat

Verify multipathing

I have a couple of questions regarding multipath. If I do vgdisplay vg01, I see it is using 1 PV: /dev/dm-13 If I type multipath -ll I see dm-9, dm-10, dm-11, dm-12, but do not see dm-13. Is my vg01 multipathed? How can I actually know for sure? Secondly, let's say this time vg01 says... (1 Reply)
Discussion started by: keelba
1 Replies

3. Hardware

Verify patching

I patched a linux kernel 2.6.28 with lttng patch. Now I have two folder of linux patched and unpatched. How can I verify whether the linux is patched or unptahced(original version)? (0 Replies)
Discussion started by: rupeshkp728
0 Replies

4. Shell Programming and Scripting

Find filenames with three digits and add zeros to make five digits

Hello all! I've looked all over the internet and this site and have come up a loss with an easy way to make a bash script to do what I want to do. I have a file with a naming convention as follows: 2012-01-18 string of words here 123.jpg 2012-01-18 string of words here 1234.jpg 2012-01-18... (2 Replies)
Discussion started by: Buzzman25
2 Replies

5. AIX

verify command

Guy's I have script doing many steps as the below ... ############# ## step1# mount all Files system mount all ## step2# Start the application /app/appsh ############# but some time mount points will not be mounted completely so that will give an error if the next step started... (1 Reply)
Discussion started by: Mr.AIX
1 Replies

6. Shell Programming and Scripting

Verify the input

I run the script with one parameter : myscript abc002 But I need my script to check the parameter in txt array first: txt="abc001 abc002 abc004" What's the best way to do it? I am using ksh. #! /usr/bin/ksh txt="abc001 abc002 abc004" if ; then echo " Your input is wrong,... (9 Replies)
Discussion started by: newoz
9 Replies

7. Shell Programming and Scripting

help: single digits inflated to 2 digits

Hi Folks Probably an easy one here but how do I get a sequence to get used as mentioned. For example in the following I want to automatically create files that have a 2 digit number at the end of their names: m@pyhead:~$ for x in $(seq 00 10); do touch file_$x; done m@pyhead:~$ ls file*... (2 Replies)
Discussion started by: amadain
2 Replies

8. Shell Programming and Scripting

total last digits

hi group, How can I count total number of 5's which are continuous in the end. i.e. in the below string, the o/p should be 4 I just know to calculate total number of 5's $ echo "95952325555" | awk -F "5" '{print NF-1}' 6 (3 Replies)
Discussion started by: uwork72
3 Replies

9. Shell Programming and Scripting

verify arguments

thanks for your help, im gonna skip this one and try a different question. thanks for your help though. (2 Replies)
Discussion started by: bebop1111116
2 Replies

10. Shell Programming and Scripting

Verify Parameters

I have a unix script written in the korn shell. At the top of the script I call a script that exports the values of the variables I use in my script. I know that when you execute the script using ksh -x it shows you the script running. I was wondering if there was a way you could run the script... (2 Replies)
Discussion started by: lesstjm
2 Replies
Login or Register to Ask a Question

Featured Tech Videos