0

If tag_len is greater than 18 or tag_len is 19 and the tag is not ends with "A", I want to cut the tag to length 18.

I tried several options but that is not working correctly. Could you let me now how I can do this?


#officail daily release TAG check

official_tag_len=18

export tag_len=`expr length $TAG`

if (($tag_len > $official_tag_len))

then

    echo "$TAG length is greater than $official_tag_len"
    if (($tag_len == ($official_tag_len+1))) && (($TAG == *A))
    then
        echo $TAG is an AM daily tag
    else
        echo $TAG is a temporary tag. reset to daily tag
        export TAG=$($TAG:0:$official_tag_len)
    fi
fi

UPDATE

the final error message is

e7e10_preqe2:0:18: command not found

I edit the code "export TAG=$($TAG:0:$official_tag_len)" referring to Extract substring in Bash

and one more thing, at first I wrote [[ instead of (( in if condition but command not found error occurs in [[ line.

Usually, I used [ ] expression in if condition. Are there any exceptional cases?

Community
  • 1
  • 1
500004dolkong
  • 725
  • 3
  • 12
  • 19

1 Answers1

1

The question setup is a bit difficult to understand, but I think this runs the way you want on bash. You didn't specify interpreter for the question..

I have taken liberty to take the "TAG" as input parameter (TAG=$1, and calculate it's length using command wc).

I have replaced all the if statements to use square brackets, and also use keyword -gt (greater than) for comparison (use lt for <, and eq for ==). These are meant to be used for numerical comparison.

#!/bin/bash

#officail daily release TAG check

TAG=$1
tag_len=`echo $TAG | wc -c`

official_tag_len=18

echo "input $tag, length: $tag_len"

if [[ $tag_len -gt $official_tag_len ]];
then

  echo "$TAG length is greater than $official_tag_len"
  if [[ $tag_len -eq $(($official_tag_len+1)) ]] && [[ $TAG == *A ]];
  then
    echo $TAG is an AM daily tag
  else
    echo $TAG is a temporary tag. reset to daily tag
    export TAG=${TAG:0:official_tag_len}
    echo "new tag: [$TAG]"
  fi
fi
julumme
  • 2,326
  • 2
  • 25
  • 38
  • I'm sorry for my uncertain codes and questions. But your answer includes all information exactly I wonder. Thanks alot. – 500004dolkong Feb 28 '14 at 01:58