Shell script to check numbers!

Hello All,

I have 3 types of files. The names of which starts with P,I,M

like P*********
D*********
M*********

now I need to do some operations witht hese files..

so

if file name starts with P or p
then
 
do the operation for P file...
 
fi
else
if file name starts with I or i
then
 
do the operation for I file...
 
fi
else
if file name starts with M or m
then
 
do the operation for M file...
 
fi

please help me in doing so..

input is file name... which is case sensitive.. and after P or I or M everything is numbers..like P12345 etc...

waiting for ur reply

case "${FILENAME:0:1}" in
"I"|"i")
  # do something
;;
"M"|"m")
  # do something
;;
"P"|"p")
  # do something
;;
esac

if i use case then i have to write whole thing inside that only.. but i need to use it manier times...

so please tell me how can i check with if statement!

With the solution above and the link below you should be able to try it out for yourself.

Test Constructs

Regards

if [[ "${FILENAME:0:1}" = [Ii] ]]
then do something
elif [[ "${FILENAME:0:1}" = [Mm] ]]
then do something
elif [[ "${FILENAME:0:1}" = [Pp] ]]
then do something
fi

but using case is cleaner.
Or explain more precisely what you want to do, maybe one can find the optimal code for doing so.

HI,

Please check this code , this may help you,

!#/us/bin/ksh

ls -1 P* >Pfile.lst
ls -1 D* >Dfile.lst
ls -1 R* >Rfile.lst

while read P D R
do
if [ -s "$P" ] then
--
fi
if [ -s  "$D" ] then
-----
fi
if [-s "$R" ] then
-----
fi
done < Pfile.lst Dfile.lst Rfile.lst

There is no need for the non-posix substring operation, nor the quoting. Simply:

case $FILENAME in
  I*|i*) i-operation ;;
  M*|m*) m-operation ;;
  P*|p*) p-operation ;;
esac

-or-

case $FILENAME in
  [Ii]*) i-operation ;;
  [Mm]*) m-operation ;;
  [Pp]*) p-operation ;;
esac

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

if $input = p or P
then 
do something
fi

Please give me the exact syntax for coded part...

For a simple if statement

# example for M or m :
if [[ "${FILENAME:0:1}" = [Mm] ]]
then what_you_have_to_do_with_M*_files
fi

This can easily be arranged with an extra line in the case statement.

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

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

Pardon me for asking, but ... why :confused:

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..


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

and also the below one

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}

Bash/ksh:

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

-or-

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

-or-

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

no brother it ll give "== unexpected" error :frowning:

---------- Post updated at 10:10 PM ---------- Previous update was at 10:08 PM ----------

this will not execute only :frowning: giving the above said error :frowning: :frowning:

What shell are you using?

how to check that? I think it is ksh

---------- Post updated at 10:13 PM ---------- Previous update was at 10:12 PM ----------

when i just write ./ and enter it shows /bin/sh.... so what shell you think it is brother?

---------- Post updated at 10:16 PM ---------- Previous update was at 10:13 PM ----------

below is the error i got

logmdr.txt[2]: syntax error at line 90 : `==' unexpected

Either Bourne or posix shell. So the [[ .. ]] constructs would not work anyway.

Oops is it! then how can i do that? please help me.. Badly i need of this script

if [ "$(echo $REQ2|cut -c1)" = "p" ] || [ "$(echo $REQ2|cut -c1)" = "P" ]
then
----------
fi

-or-

case $REQ2 in [pP]*) 
----------
esac

---------- Post updated at 10:50 PM ---------- Previous update was at 10:37 PM ----------

If /bin/ksh exists on your system you can put this on line 1 of your script:

#!/bin/ksh

Then all of the previous examples may work too...