Pipe into if statement?

10,737

Solution 1

To copy standard input to standard output, use cat, not echo.

git ls-remote "$1" 'refs/heads/*' |
sed 's~.*/~~' |
if [ -z "$2" ]
  then
    cat
  else
    cat > "$2"
fi

Notice also the proper use of quotes and the placement of the pipes so you can avoid the backslashes. The use of sed is a very minor optimization but I also find it clearer than the double rev around a cut (provided you grok regex). You could also use awk -F/ '{ print $NF }' (but then that requires you to grok Awk).

You could avoid the cat by doing this instead;

${2+exec >"$2"}
git ls-remote "$1" | sed 's~.*/~~'

(The failure if you pass in an empty string as the second argument should at least be more explicit, if not necessarily more helpful, than with [ -z, which fails to distinguish between an unset and an empty value.)

Solution 2

You can just replace {} syntax you're trying to use with cat since you're trying to read from stdin:

git ls-remote $1 'refs/heads/*' \
  | rev \
  | cut -d'/' -f1 \
  | rev \
  | if [ -z $2 ]
      then
        cat
      else
        cat > $2
    fi

Update: no subshell is needed, thanks @tripleee.

Share:
10,737

Related videos on Youtube

Philip Kirkbride
Author by

Philip Kirkbride

Updated on September 18, 2022

Comments

  • Philip Kirkbride
    Philip Kirkbride over 1 year

    I'm writing a bash script. I have a series of pipes working to get all the branches on a git repository:

    git ls-remote $1 'refs/heads/*' \
      | rev \
      | cut -d'/' -f1 \
      | rev \
      | if [ -z $2 ]
          then
            echo {}
          else
            echo {} > $2
        fi
    

    Currently the if statement part of this doesn't work properly. What do I replace {} with to make this work?

    • tripleee
      tripleee almost 7 years
      Make it work how exactly? To copy standard input to standard output, use cat, not echo.
    • tripleee
      tripleee almost 7 years
      And quote your variables, "$1" and "$2".
    • Philip Kirkbride
      Philip Kirkbride almost 7 years
      @tripleee currently it prints {} not the result from previous pipe.
    • tripleee
      tripleee almost 7 years
      If you mean the standard input received by the if statement, ... Sorry, I can't think of any simpler way to say it. I think you want cat.
    • tripleee
      tripleee almost 7 years
      You might also want to replace the rev | cut | rev with sed 's~.*/~~'
    • Stéphane Chazelas
      Stéphane Chazelas almost 7 years
  • tripleee
    tripleee almost 7 years
    Yes, it also protects any wildcard characters in the values from being expanded by the shell; and newline could still be included in the values, although that woud be a rather pathological corner case here.