0

I have tested it with double braces for the "&&" and have tried it against "" for null values however every time it does not move down to the else statement.

#!/bin/bash
#find | grep
#find -a {} | grep {search}
function fg { 
        if [ -z $1 ] && [ -z $2 ]; then
        echo "Help: find {path} | grep {search} "

        else
        find $1 | grep $2

        fi 
}

fg
2
  • Have you tried with quotes arround the $1 and $2? Commented Jul 13, 2017 at 12:47
  • Yes, I have tried every variation I can think of I've assigned the arguments to variables and applied them in the if statement. Commented Jul 14, 2017 at 8:24

1 Answer 1

1

Always quote your parameter expansions.

fg () { 
  if [ -z "$1" ] && [ -z "$2" ]; then
    echo "Help: must specify path and regular expression"
  else
    find "$1" -regex "$2"
  fi
}

fg

A cleaner way to check for unset or empty parameters, though, is via the :? operator.

fg () {
    : ${1:?Missing path}
    : ${2:?Missing regex}
    find "$1" -regex "$2"
}
Sign up to request clarification or add additional context in comments.

2 Comments

I couldn't use the top block of code but tried the bottom one and hasnt flagged any errors, however I cant verify that it works for me as I'm getting an unexpected token for the closing brace on the function.
Sorry for the late reply, tried the second block of code and still didn't work?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.