Shell script

I have one script.

#!/bin/ksh
[[ $# -lt 1 ]] && echo "Usage: $0 pattern (pattern can be gsx name or trunkgroup or point code)" && exit 1
egrep -h "$1" /appl/voip/voipfiles/trunkgroups/tglastrun.mostrecent

Which works like this.

./abc.sh <input>

I have one requirement.

  1. if input is Point code like 203-53-27

do this

else

that.

means. I want to control function of script based on input.

tcmg6so 209-68-71   MLWDWAAAGT0BIM1I MLWDWAAAGT0BIM1I 209-65-23            CIRCUITLO2HI   NOCICS     INSERVICE    ENABLED      528
tcmg6so 209-68-71   PTLDOR62GT2BIM1I PTLDOR62GT2BIM1I 209-68-66            CIRCUITLO2HI   NOCICS     INSERVICE    ENABLED     1080
tcmg6so 209-68-71   PTLDOR62GT2BSS1I PTLDOR62GT2BSS1I 209-68-66            CIRCUITLO2HI   NOCICS     INSERVICE    ENABLED       48

How to identify if input is any Point code or else any word like tcm or CIRCU.

bash-3.00$ abc TACNWADNCM6BWW1O
tcmg1so 209-68-71   TACNWADNCM6BWW1O TACNWADNCM6BWW1O 209-65-23            CIRCUITHI2LO   NOCICS     INSERVICE    ENABLED      792
tcmg3so 209-68-71   TACNWADNCM6BWW1O TACNWADNCM6BWW1O 209-65-23            CIRCUITHI2LO   NOCICS     INSERVICE    ENABLED     1152
tcmg6so 209-68-71   TACNWADNCM6BWW1O TACNWADNCM6BWW1O 209-65-23            CIRCUITHI2LO   NOCICS     INSERVICE    ENABLED     1056
bash-3.00$
bash-3.00$ abc 209-65-23
dnvg1so 209-68-36   TACNWADNCM6AE92I TACNWADNCM6AE92I 209-65-23            CIRCUITLO2HI   NOCICS     INSERVICE    ENABLED       24
tacg1so 227-125-20  TACNWADNCM6ASS1I TACNWADNCM68SS1B 209-65-23            CIRCUITLO2HI   NOCICS     INSERVICE    ENABLED       48
tacg1so 227-125-20  TACNWADNCM6AWW1O TACNWADNCM6AWW1O 209-65-23            CIRCUITLO2HI   ALLCICS    INSERVICE    ENABLED      528
tacg2so 227-125-20  TACNWADNCM6AIM1I TACNWADNCM68IM1B 209-65-23            CIRCUITLO2HI   NOCICS     INSERVICE    ENABLED     1512
tacg2so 227-125-20  TACNWADNCM6ASS1I TACNWADNCM68SS1B 209-65-23            CIRCUITLO2HI   NOCICS     INSERVICE    ENABLED       24
tacg3so 227-125-20  TACNWADNCM6AIM1I TACNWADNCM68IM1B 209-65-23            CIRCUITLO2HI   NOCICS     INSERVICE    ENABLED     1536
tacg4so 227-125-20  TACNWADNCM6AIM1I TACNWADNCM68IM1B 209-65-23            CIRCUITLO2HI   NOCICS     INSERVICE    ENABLED     2232
tacg5so 227-125-20  MLWDWAAAGT0AIM1I MLWDWAAAGT08IM1I 209-65-23            CIRCUITHI2LO   ALLCICS    INSERVICE    ENABLED      552
tacg5so 227-125-20  TACNWADNCM6AE91I TACNWADNCM6AE91I 209-65-23            CIRCUITLO2HI   NOCICS     INSERVICE    ENABLED       24
tacg5so 227-125-20  TACNWADNCM6AWW1O TACNWADNCM68WW1O 209-65-23            CIRCUITLO2HI   ALLCICS    INSERVICE    ENABLED     1248
tacg6so 227-125-20  MLWDWAAAGT0AIM1I MLWDWAAAGT08IM1I 209-65-23            CIRCUITHI2LO   ALLCICS    INSERVICE    ENABLED      840
tacg6so 227-125-20  TACNWADNCM6AWW1O TACNWADNCM68WW1O 209-65-23            CIRCUITLO2HI   ALLCICS    INSERVICE    ENABLED     1224
tcmg1so 209-68-71   TACNWADNCM6BSS1I TACNWADNCM6BSS1I 209-65-23            CIRCUITLO2HI   NOCICS     INSERVICE    ENABLED       72
tcmg1so 209-68-71   TACNWADNCM6BWW1O TACNWADNCM6BWW1O 209-65-23            CIRCUITHI2LO   NOCICS     INSERVICE    ENABLED      792
tcmg2so 209-68-71   TACNWADNCM6BIM1I TACNWADNCM6BIM1I 209-65-23            CIRCUITHI2LO   NOCICS     INSERVICE    ENABLED     1872
tcmg3so 209-68-71   TACNWADNCM6BIM1I TACNWADNCM6BIM1I 209-65-23            CIRCUITHI2LO   NOCICS     INSERVICE    ENABLED     1320
tcmg3so 209-68-71   TACNWADNCM6BWW1O TACNWADNCM6BWW1O 209-65-23            CIRCUITHI2LO   NOCICS     INSERVICE    ENABLED     1152
tcmg4so 209-68-71   TACNWADNCM6BIM1I TACNWADNCM6BIM1I 209-65-23            CIRCUITHI2LO   NOCICS     INSERVICE    ENABLED     2088
tcmg5so 209-68-71   MLWDWAAAGT0BIM1I MLWDWAAAGT0BIM1I 209-65-23            CIRCUITLO2HI   NOCICS     INSERVICE    ENABLED      864
tcmg6so 209-68-71   MLWDWAAAGT0BIM1I MLWDWAAAGT0BIM1I 209-65-23            CIRCUITLO2HI   NOCICS     INSERVICE    ENABLED      528
tcmg6so 209-68-71   TACNWADNCM6BWW1O TACNWADNCM6BWW1O 209-65-23            CIRCUITHI2LO   NOCICS     INSERVICE    ENABLED     1056
bash-3.00$

I want script to identify input whether it's its point code or not. if it's point code do something, else in any other case of input do that.

---------- Post updated at 06:38 PM ---------- Previous update was at 06:35 PM ----------

in case of point code i want exact match.

Like

egrep -h "$1" /appl/voip/voipfiles/trunkgroups/tglastrun.mostrecent|grep -w "$1"

using -w it matched exact.

in case of other input (not point code ) I want this to function like normal grep.

Like

egrep -h "$1" /appl/voip/voipfiles/trunkgroups/tglastrun.mostrecent

What do you exactly mean by "Point code"? Is it always XXX-XX-XX where all the X's are integers? (3 X's-2 X's-2 X's)

Hi gacanepa,

Input to the script can be PointCode or word.

./abc 209-57-73

./abc tcm

./abc SHOKCA0

I want script to identify $1 that command line input and do operation based on that.

If script identifies $1 is Point-code then it will do certain function, else it will do other function.

I just want one one if - else script.

I don't know how script will identify if it's PointCode or some thing else.

---------- Post updated at 07:28 PM ---------- Previous update was at 07:25 PM ----------

one logic to the find the input will be like this, if Input starts with an integer or not.

If starts with integer
do this,

else
do this.

This does the job:

#!/bin/bash
glob="[[:digit:]][[:digit:]][[:digit:]]-[[:digit:]][[:digit:]]-[[:digit:]][[:digit:]]"
if [[ ${1} == $glob ]]; then
    echo "yes"
else
    echo "no"
fi

This is called "Character classes" and you can find a good tutorial here.
Let me know if you have further questions.

Hi Gacanepa.

Thanks for reply, little improvement required numbers can be 1 to 3 it's not 3x-2y-2z

I tried something, but not able to match correctly.

bash-3.2$ cat raza
#!/usr/bin/bash
Input="$1"
echo "$Input"
if [[ "$Input"="[0-9]+-[0-9]+-[0-9]+" ]];
then
echo "HELLO"
PC=`echo "$Input" |sed -e 's/^[0]*//g'|sed -e's/-[0]*/-/g'`
grep -hw "$PC" /appl/voip/voipfiles/trunkgroups/tglastrun.mostrecent
else
echo "Hi"
grep -h "$Input" /appl/voip/voipfiles/trunkgroups/tglastrun.mostrecent
fi

bash-3.2$

Point code can be 209-78-125 , 27-67-85, 209-127-56 like this.
So please improve it.

Please use code tags. It makes your code more understandable - I'll be more than glad to help :).

 bash-3.2$ cat raza
#!/usr/bin/bash
Input="$1"
echo "$Input"
if [[ "$Input"="[0-9]+-[0-9]+-[0-9]+" ]];
then
echo "HELLO"
PC=`echo "$Input" |sed -e 's/^[0]*//g'|sed -e's/-[0]*/-/g'`
grep -hw "$PC" /appl/voip/voipfiles/trunkgroups/tglastrun.mostrecent
else
echo "Hi"
grep -h "$Input" /appl/voip/voipfiles/trunkgroups/tglastrun.mostrecent
fi
bash-3.2$

don't know how to use code tag. Just tried this, hope it is fine.

---------- Post updated at 09:23 PM ---------- Previous update was at 09:22 PM ----------

bash-3.2$ cat raza
#!/usr/bin/bash
Input="$1"
echo "$Input"
if [[ "$Input"="[0-9]+-[0-9]+-[0-9]+" ]];
then
echo "HELLO"
PC=`echo "$Input" |sed -e 's/^[0]*//g'|sed -e's/-[0]*/-/g'`
grep -hw "$PC" /appl/voip/voipfiles/trunkgroups/tglastrun.mostrecent
else
echo "Hi"
grep -h "$Input" /appl/voip/voipfiles/trunkgroups/tglastrun.mostrecent
fi
bash-3.2$

---------- Post updated at 09:24 PM ---------- Previous update was at 09:23 PM ----------

My test criteria in If statement is not working well.

#!/bin/bash
echo "You provided the following argument: $1"
if [[ $1 =~ ^[0-9]{1,3}-[0-9]{1,3}-[0-9]{1,3}$ ]]
then
    echo "yes"
else
    echo "no"
fi

This script uses regular expressions:

[0-9]{1,3}

means: "allow a minimum of one and a maximum of three digits between 0 and 9".

If you consider that your question has been answered, please mark this thread as SOLVED for the future reference of other users. (Click on "Thread tools" and choose "Mark this thread as solved"). If someone helps you, click the "Thanks" link on the bottom-right corner of the post.