Visit Our UNIX and Linux User Community


Shell script to check numbers!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script to check numbers!
# 8  
Old 11-01-2009
The files which i have, is named like P12345 or D12345 or M12345....
The numbers with P D and M may changes.....


File name is the input...

So if i give the file name first it should check the file name.. If it is not started with P or D or M then it should give error...

If it is with the proper name then depending on whether it P D M I should do the operation....

You ppl here may tell like It is better to use CASE statement for this.. Yes it is better to use CASE but the problem is if I use case then I should write everything about that file under once statement..

But i should be able to use this manier times in my script..

So please tell me how to check equality with shell script ..

the file name can start with P or p.. that means it should not be case sensitive...


Please tell me how to give equality sign..

For example..

the input I gave is p12345

i want you people to tell

how to check this in shell script...

like

Code:
if $input = p or P
then 
do something
fi

Please give me the exact syntax for coded part...
# 9  
Old 11-01-2009
For a simple if statement
Code:
# example for M or m :
if [[ "${FILENAME:0:1}" = [Mm] ]]
then what_you_have_to_do_with_M*_files
fi

# 10  
Old 11-01-2009
Quote:
Originally Posted by smarty86
[..]
So if i give the file name first it should check the file name.. If it is not started with P or D or M then it should give error...
If it is with the proper name then depending on whether it P D M I should do the operation....
This can easily be arranged with an extra line in the case statement.
Code:
case $FILENAME in
  I*|i*) i-operation ;;
  M*|m*) m-operation ;;
  P*|p*) p-operation ;;
  *)     echo "Wrong inputfile. The first character should be i, m or p. Exiting"; exit 1 ;;
esac

# 11  
Old 11-01-2009
Quote:
Originally Posted by frans
For a simple if statement
Code:
# example for M or m :
if [[ "${FILENAME:0:1}" = [Mm] ]]
then what_you_have_to_do_with_M*_files
fi

guys I dont want case statement.. please give me If statement...

unfortunately above code is giving error..

The error I'm getting is:

{FILENAME:0:1} bad substitution...

Please help me
# 12  
Old 11-02-2009
Quote:
Originally Posted by smarty86
guys I dont want case statement.. please give me If statement...
Pardon me for asking, but ... why Smilie
# 13  
Old 11-02-2009
brother I need to use it manier times in my script.. so I want to check it with if statement..
Please tell me how can i use equality sign with if statement in shell script if the file name is not case sensitive..

i used the below one too but didnt work out..

Code:
if [[ "REQ2" -eq P* ]] && [[ "REQ2 -eq p* ]]
then
----------
fi

and also the below one
Code:
if [[ "REQ2" == P* ]] && [[ "REQ2 == p* ]]
then
----------
fi


please help me

---------- Post updated at 09:02 PM ---------- Previous update was at 08:58 PM ----------

and somebody please tell me what is the meaning of ${FILENAME:0:1}
# 14  
Old 11-02-2009
Bash/ksh:

Code:
if [[ "$REQ2" == P* ]] || [[ "$REQ2" == p* ]]
then
----------
fi

-or-
Code:
if [[ "$REQ2" == P* || "$REQ2" == p* ]]
then
----------
fi

-or-
Code:
if [[ "$REQ2" == [pP]* ]]
then
----------
fi


Previous Thread | Next Thread
Test Your Knowledge in Computers #87
Difficulty: Easy
Windows 7 added PowerShell, a more powerful command-line shell and scripting language than the Command Prompt.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shell script to tail a file with unknown numbers

Hello, I would like to write script to tail a file for different environment But the number of lines are keep changing How can I write a script For example: env could : A, B or C and log files could be a.log, b.log and c.log with the number of lines can change say sometimes it 100 last... (9 Replies)
Discussion started by: encrypt_decrypt
9 Replies

2. Shell Programming and Scripting

Script Shell: Count The sum of numbers in a file

Hi all; Here is my file: V1.3=4 V1.4=5 V1.1=3 V1.2=6 V1.3=6 Please, can you help me to write a script shell that counts the sum of values in my file (4+5+3+6+6) ? Thank you so much for help. Kind regards. (3 Replies)
Discussion started by: chercheur111
3 Replies

3. Shell Programming and Scripting

calculation using awk or shell script in between the numbers

file A E969K D223L E400L E34L file B predicted 3 1 250 251 500 501 1000 The output should be E969K 501 1000 D223L 1 250 E400L 251 500 E34L 1 250 I tried in this way (1 Reply)
Discussion started by: cdfd123
1 Replies

4. Shell Programming and Scripting

Shell script to find the sum of first n Fibonacci numbers

pls give me the solution for this i need it for my exam pls pls pls Shell script to find the sum of first n Fibonacci numbers (1 Reply)
Discussion started by: Kshitija
1 Replies

5. Shell Programming and Scripting

How to compare floating point numbers in shell script?

How can we compare 2 floating point numbers in SHELL script? (11 Replies)
Discussion started by: dearanik
11 Replies

6. Shell Programming and Scripting

reverse ':' separated numbers in a shell script

I want to reverse a the following: 00:11:22:33:44:55 I currently use something like below to pass it as is. But now I want the same script to reverse the above and pass it to ethtool. // psuedo code i=0 skip=0 for m in $@ do if then skip=1 ... (1 Reply)
Discussion started by: bhanu.nani
1 Replies

7. Shell Programming and Scripting

Shell script to search through numbers and print the output

Suppose u have a file like 1 30 ABCSAAHSNJQJALBALMKAANKAMLAMALK 4562676268836826826868268468368282972982 2863923792102370179372012792701739729291 31 60... (8 Replies)
Discussion started by: cdfd123
8 Replies

8. Shell Programming and Scripting

Shell script to check the unique numbers in huge data

Friends, I have to write a shell script,the description is---- i Have to check the uniqueness of the numbers in a file. A file is containing 200thousand tickets and a ticket have 15 numbers in asecending order.And there is a strip that is having 6 tickets that means 90 numbers.I... (7 Replies)
Discussion started by: namishtiwari
7 Replies

9. Shell Programming and Scripting

add numbers in shell script

cat dailyreports | grep "Important list" | awk -F":" '{print $2}' | awk -F" " '{print $1}' hey guys, after running the above combination of cat and awk, i get the below output: 3 4 2 9 now, i need to add these numbers up all in one line. i dont know what to add to that cat and awk one... (2 Replies)
Discussion started by: Terrible
2 Replies

10. Shell Programming and Scripting

retain Line numbers.. in Vi .. OR .. A SHELL SCRIPT

Hello everybody ! GOT SOMETHING INTERESTING... I am trying to retain line number for a text document.. usually we get line numbers in VI using :set nu , but I want to permanently store them. It's a 4000 lines of text and I want grep/search it for a list of words/fields stored in a different... (2 Replies)
Discussion started by: sdlayeeq
2 Replies

Featured Tech Videos