Link to home
Create AccountLog in
Avatar of raaj4354
raaj4354

asked on

String matching in shell

I have the below shell script which checks for strings in file. The strings in the file are of the format  string:string2
Avatar of raaj4354
raaj4354

ASKER

Below s the script I have in place in CVS version control to check users against tag. The man purpose of this script is to check for  username and tag in the control list and if they match the tag that is used by user then it should be exit normally or error out .
the control list is of the form

user1:tag1
user2:tag1
user3:tag1

if other than these three users try to use the tag1 then the script should error.

below is the script I have
#!/bin/ksh

PROGRAM=cvs_tag.sh
VALIDUSER=0                # defaults to not a valid user
CONTROLFILE=cvs_tag_users  # file that contains list of tags and users
CURRENT_DIR=/cvs/test/CVSROOT
CHAR_SLASH=/
LOG_FILE=/tmp/tag123.log
TAGFOUND=0

  # sub to ensure all capital string characters
  toUpper() {
    echo $1 | tr "[:lower:]" "[:upper:]"
  }


  # incoming tag information from CVS
  if  ! [ -z "$1" ]
  then
    TAG=$1
  else
    echo "$(date): user $USER, tag " >> $LOG_FILE
    echo "$*" >> $LOG_FILE
    TAG=tag
  fi

  # ensure the tag is lowercase
  TAG=`toUpper $TAG`
  USER=`toUpper $USER`

  echo "\n\n\t***************************************************************"  >> $LOG_FILE
  echo "\t***  program: $PROGRAM -- version: $VERSION    ***" >> $LOG_FILE
  echo "\t***  `date`                           ***" >> $LOG_FILE
  echo "\t***  Control File: $CONTROLFILE                  ***" >> $LOG_FILE
  echo "\t***  Current Dir: $CURRENT_DIR                  ***" >> $LOG_FILE
  echo "\t***  Tag(uppercased): $TAG                  ***" >> $LOG_FILE
  echo "\t***  User(uppercased): $USER                  ***" >> $LOG_FILE
  echo "\t***************************************************************\n"  >> $LOG_FILE

  # first ensure that we have the control file
  if [ `find $CURRENT_DIR -type f -perm -o+rx -name $CONTROLFILE 2>/dev/null` ]; then
    # found the control file
    echo "Using control file: $CONTROLFILE, in $CURRENT_DIR, for a tag of $TAG\n" >> $LOG_FILE
  else
    # error condition - file not found
    echo "Error occurred - unable to locate a valid control file! "
    echo "Error occurred - unable to locate a valid control file! " >> $LOG_FILE
    exit 1;
  fi

  # look through the file to see if this user is authorized to use this tag
  while read inputline
  do
    # control file is colon seperated - first position is login, then tag
    login="$(echo $inputline | cut -d: -f1)";
    tag="$(echo $inputline | cut -d: -f2)";

    # uppercase the strings
    tag=`toUpper $tag`
    login=`toUpper $login`

    if [ "$tag" = "$TAG" ]
    then
      TAGFOUND=1;
    fi  

    # if valid user, exit normally  
    if [ "$login" = "$USER" -a TAGFOUND -eq 1 ]
    then
      echo "match - $login::$USER and $tag::$TAG \n" >> $LOG_FILE
      TAGFOUND=1;
      VALIDUSER=1;
      break;
    fi
  done < $CURRENT_DIR$CHAR_SLASH$CONTROLFILE

  # exit with error if user not on list
  if [ $VALIDUSER -eq 0 -a $TAGFOUND -eq 1 ]; then
    echo "You are not authorized to use this tag: '$TAG'. " 
    echo "You are not authorized to use this tag: '$TAG'. " >> $LOG_FILE
    exit 1;
  else
    echo "tag is not in the control list, or you are authorized to use this special tag. You are all set" >> $LOG_FILE
    exit 0;
  fi
Below is the log file which shows the match section .The user name is matched but not the tag .But still the tag shows as matched .
DEBUG -- login:~user1~ and tag:~tag1~ and TAG:~sometag~ and USER:~user1~

match - user1::user1 and tag1::sometag (the usernames are matching but the tags are not...!!!)

Either your tag is not in the control list, or you are authorized to use this special tag. You are all set

Can anybody help in the string comparison in the match line in the script ..?
OK ..the only problem in the script is here :

# uppercase the strings
    tag=`toUpper $tag`
    login=`toUpper $login`

    if [ "$tag" = "$TAG" ]
    then
      TAGFOUND=1;
    fi  

    # if valid user, exit normally  
    if [ "$login" = "$USER" -a TAGFOUND -eq 1 ]
    then
      echo "match - $login::$USER and $tag::$TAG \n" >> $LOG_FILE
      TAGFOUND=1;
      VALIDUSER=1;
      break;
    fi


Even thought the two strings doent match it still says they match .. !!
Avatar of simon3270
You don't clear TAGFOUND a thte top of the while loop, so once it is set to 1, it stays there.

Add TAGFOUND=0 just after the "while read inputline; do" bit.
ASKER CERTIFIED SOLUTION
Avatar of simon3270
simon3270
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account