Can someone verify the code of two function

Input=D123,S1234,D345 | kESTREL PRODUCTION SUPPORT

echo -en "Enter the logmsg="
read logmsg
logmsg1=${logmsg%%|*};
echo "$logmsg1"|tr ',' '\n' | sed 's/[ \t]*$ | sed '/^$/d'//'>pre-commit.config

Char()
{
while read line
do
if [[ ${line:0:1 = D ]] || [[ ${line:0:1 = S ]];then
echo "Success"
else
exit 1;
done<pre-commit.config
}

length()
{
while read LINE
do
len=`expr length "$LINE"`
if [ "$LINE" -le "6" ];then
echo "Success"
else
exit 2;
# and using the variable len for some purpose
done < pre-commit.config
}

length
Char

In the above script I want the two things

1) Each variable in the left of pipe should start with D and S
2) Each variable is of 6 characters

You are missing the closing fi

Since you are using bash or ksh, use parameter expansion for the length:

len=${#LINE}

You are comparing $LINE istead of $len.

Again, you are missing the closing fi

I'd write the functions like this:

input='D123,S1234,D345 | kESTREL PRODUCTION SUPPORT'

Char()
{
  for var
  do
    case $var in
      [DS]*) echo Success ;;
      *) exit 1 ;;
    esac
  done
}

length()
{
  for var
  do
    if [ ${#var} -le 6 ]
    then
      echo Success
    else
      exit 2
    fi
  done
}

input=${input%% | *}

IFS=, read -a vars <<< "$input"

length "${vars[@]}"
Char "${vars[@]}"
1 Like