0

I'm using below if condition in shell script

   if [! -z "$failedTC"] || [-n "$failedTC"];
   then
   echo "ITS not empaty AND NOT NULL"
   else
   echo "ITS empty or NULL"
   fi

The above code I'm checking the $failedTC shouldn't be null and empty. But the if condition throws below error message in jenkins. I used the above script in jenkins Execute Shell window.

/tmp/jenkins371454092166709762.sh: line 23: [!: command not found /tmp/jenkins371454092166709762.sh: line 23: [-n: command not found

Any leads....

codeforester
  • 39,467
  • 16
  • 112
  • 140
ArrchanaMohan
  • 2,314
  • 4
  • 36
  • 84

3 Answers3

4

You need to add a space after the opening brackets and before the closing brackets in your conditionals:

Change

if [! -z "$failedTC"] || [-n "$failedTC"];

to

if [ ! -z "$failedTC" ] || [ -n "$failedTC" ];
EJK
  • 12,332
  • 3
  • 38
  • 55
2

The error is that, it needs a space before and after every square bracket[].

Try this:

if [ ! -z "$failedTC" ] || [ -n "$failedTC" ];
   then
   echo "ITS not empaty AND NOT NULL"
   else
   echo "ITS empty or NULL"
fi
Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58
2

"[" and "]" are commands, it should be wrapped around spaces " [ ". that is the best practice. try this

  if [ ! -z "$failedTC" ] || [ -n "$failedTC" ];
   then
   echo "ITS not empaty AND NOT NULL"
   else
   echo "ITS empty or NULL"
   fi
stack0114106
  • 8,534
  • 3
  • 13
  • 38