0

I am trying to create a "Lambda" style WHERE script.

I want lambdaWHERE to take piped input and pass it through if condition after given as arguments is met. Like xargs I use {} to represent what comes down the pipe.

I call command like:

ls -d EqAAL* | lambdaWHERE.sh -f {}/INFO_ACTIVETICK

I want the folder names passed through if they contain a file called INFO_ACTIVETICK

Here is the script:

#!/bin/sh
#set -x

ARGS=$*

while read i
do
        CMD=`echo $ARGS | sed 's/{}/'$i'/g'`

        if [[ $CMD  ]]
        then
                echo $i
        fi
done

But when I run it a mysterious "-n" appears...

$ ls -d EqAAL* | /q/lambdaWHERE.sh -f {}/INFO_ACTIVETICK
+ ARGS='-f {}/INFO_ACTIVETICK'
+ read i
++ echo -f '{}/INFO_ACTIVETICK'
++ sed 's/{}/EqAAL-1m/g'
+ CMD='-f EqAAL-1m/INFO_ACTIVETICK'
+ [[ -n -f EqAAL-1m/INFO_ACTIVETICK ]]
+ echo EqAAL-1m
EqAAL-1m
+ read i

How can I get the bit in the [[ ]] correct?

ManInMoon
  • 6,795
  • 15
  • 70
  • 133

1 Answers1

1

You were quite close. you only need to switch to the standard POSIX [ $CMD ] and it will work.

The main difference between using [[ $CMD ]] and [ $CMD ] is that the first has fewer surprises and you need not quote variables. That also means that a variable is though of as one token and cannot have a whole expression in it like you are trying. [ $CMD ] however works the same way as the original shell where [ was just a command an thus need explicit quotations in order to interpret something with spaces as one argument.

There is a relevant question about the differences between [[ ...]] and [ ..]

Community
  • 1
  • 1
Sylwester
  • 47,942
  • 4
  • 47
  • 79