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
  • 34,080
  • 14
  • 96
  • 122
ArrchanaMohan
  • 1,867
  • 2
  • 29
  • 63

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,090
  • 3
  • 36
  • 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
  • 31,737
  • 7
  • 30
  • 50
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,220
  • 3
  • 11
  • 35