0

I have two string like this...

a=[2018:08:22-15:26:18:26182619]
b=[2018:08:22-15:26:18:26182619]

And I want to convert
a= 20180911183706376476
b= 2018082215261826182619

Can anyone please help me out?

  • 1
    You must include a space around both sides of the `]` and `[`. And when writing shell scripts, it's good to use `shellcheck`. – Halfgaar Sep 11 '18 at 06:39
  • Now I want to convert `[2018:08:22-15:26:18:26182619]` to `2018082215261826182619`. Can you please help me out? – justAnAnotherCoder Sep 14 '18 at 05:20
  • Welcome on the ServerFault. The command can be done with `a="${a//[\[\]]/}"`, this removes all `[` and `]` from the variable `$a`. But it is not really a sysadm task, it is more like a little scripting trick. – peterh Sep 14 '18 at 06:20
  • Thanks.Getting error as `ksh: a="${a//[\[\]]/}": bad substitution` – justAnAnotherCoder Sep 14 '18 at 06:23

1 Answers1

3

From the "test, [ -- condition evaluation utility" manpage (accessible using the command man [ or man test), the '-ne' option is used for comparing integers. Use the '=' operator for strings.

n1 -eq n2 True if the integers n1 and n2 are algebraically equal.

s1 = s2 True if the strings s1 and s2 are identical.

It's probably not a bad idea to quote the strings as well. Make sure to use double (or soft) quotes with variables.

if [ "$a" = "$b" ]
  then
    #do something
fi