0

I was able to extract a substring from a string; but no matter what syntax changes I try (and I've tried many), I am unable to actually enter the if block in this snippet even though the print out suggests that the CLIENT_NAME string matches the expected one (attached the output from the first echo). Only the first echo prints anything. What am I doing wrong here? Any ideas are really appreciated!

The idea is if the client is named say aa_NNNN, then I need to extract the aa and the NNNN and check if the aa matches a known string (say "xx") and if it does, only then, calculate the version NNNN and do something if version NNNN exceeds a known version MMMM.

#! /bin/sh
CLIENT=$1
...
CLIENT_NAME="${CLIENT:0:2}"
CLIENT_VERSION=2015
echo "Before compare; client: $CLIENT_NAME; version: $CLIENT_VERSION"
if [ "$CLIENT_NAME" == "xx" ]; then
   CLIENT_VERSION="${CLIENT:3:4}"
   echo "Inside compare; client: $CLIENT_NAME; version: $CLIENT_VERSION"
   if [ $CLIENT_VERSION -ge 2016 ]; then
      ...
   fi
fi

First echo output:

Before compare; client: xx; version: 2015

/bin/sh --version returns:

GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
squashed.bugaboo
  • 1,338
  • 2
  • 20
  • 36

1 Answers1

1

I got the same result running your code, but removing the quotes from the substring extraction (and replacing the , with : in the second one) fixed it...

CLIENT_NAME=${CLIENT:0:2}

...and...

CLIENT_VERSION=${CLIENT:3:4}
Patrick Lee
  • 1,990
  • 1
  • 19
  • 24
  • Thanks @Patrick: Did you have /bin/bash just like @tiagohngl? I tried with your changes and still the second echo doesn't print for me. – squashed.bugaboo Jan 07 '16 at 19:31
  • I'm on a Mac with GNU Bash 3.2.57. The edited script works for me with `/bin/bash` and `/bin/sh`. I also tested successfully on Ubuntu 14.04 with Bash (`/bin/bash`). – Patrick Lee Jan 07 '16 at 19:36
  • Have you tried changing the "shebang" line to Bash at the top of your script? `#!/bin/bash` – Patrick Lee Jan 07 '16 at 19:42
  • Yes, did that too.. still don't see the inside echo. No error message, nothing. – squashed.bugaboo Jan 07 '16 at 19:43
  • Is there a value for `$CLIENT_NAME` in the first echo? And how are you invoking the script? What's the actual command you're using? And what is the output of `which bash`? – Patrick Lee Jan 07 '16 at 19:45
  • Yes, the first echo prints out as expected (I displayed the output in my question) - it shows as `xx`. I am invoking it by executing from command line with argument as follows: `scriptname xx_2016`, so I am expecting to see the version displayed as `2016` in the inner echo, but I don't even see the inner echo printed. `which bash` returns: `/bin/bash` – squashed.bugaboo Jan 07 '16 at 19:47
  • When I read: http://stackoverflow.com/questions/10849297/compare-a-string-in-unix, it seems to suggest using `=` instead of `==`. Could that be the problem? Does using `=` even make any sense? Even the responses in that link are quite confusing. – squashed.bugaboo Jan 07 '16 at 19:54
  • The Bash scripting guide for Linux says that `==` is a synonym for `=`, but you could try it. Ref: http://www.tldp.org/LDP/abs/html/comparison-ops.html – Patrick Lee Jan 07 '16 at 20:01
  • Tried that too.. still doesn't print the inner echo for me. – squashed.bugaboo Jan 07 '16 at 20:22
  • Apparently I had another typo that I finally caught. I am marking this as the answer. Apologies, and thanks all for your help! – squashed.bugaboo Jan 07 '16 at 20:32
  • No problem. Typos go with the territory in Bash. :) – Patrick Lee Jan 07 '16 at 20:34