Jump to content

Run bash script, filter if/else on output


Recommended Posts

Posted

I have a simple workflow, that checks if a file exists, and shows a notification with the content if it does, and a Large type ERROR, if it doesn't. I think I misunderstand something with how filter works.

The script looks like this:

FILE="/Users/niclas/test.txt"

if [ -f "$FILE" ];
then
   cat $FILE
else
   echo "";
fi

 

Top filter: Only continue if {query} is not equal to (empty)

Bottom filter: Only continue if {query} is equal to (empty)

 

The notification looks like:

Read OK {query}

 

The problem is, it always shows the first one, even if the file doesn't exist.

I also tried make it output 0, and "Only continue if {query} is equal to 0", but with the same results.

I also tried a regex (0)+, it didn't work either.

 

What am I doing wrong? I've attached the workflow here: Workflow

 

Thanks! :)

 

5a0c3d580512d_Skarmavbild2017-11-15kl_14_12_23.thumb.png.b3c08d7bbe20dacbb0b7a17904a4c979.png

Posted

Hmm, it seems to work when I didn't echo anything at all, by just removing the else part. So echo probably outputs a newline or something. But why didn't it work with echo "0" and setting the filter to match on "0"? Is it something with a new line in the script?

Posted
7 hours ago, Niclas said:

Hmm, it seems to work when I didn't echo anything at all, by just removing the else part

 

That's the right solution.

 

7 hours ago, Niclas said:

So echo probably outputs a newline or something.

 

It does. Use echo -n to suppress the trailing space.

 

7 hours ago, Niclas said:

Is it something with a new line in the script?

 

Alfred tries to remove extraneous whitespace, so it probably removes the trailing newline unless the input is whitespace-only.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...