I'm trying to return a boolean if a curl response has a status of 200.curl https://www.example.com -I | grep ' 200 ' ? echo '1' : echo '0';This however brings back:grep: ?: No such file or directorygrep: echo: No such file or directorygrep: 1: No such file or directorygrep: :: No such file or directorygrep: echo: No such file or directorygrep: 0: No such file or directory  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current                                 Dload  Upload   Total   Spent    Left  Speed  0     0    0     0    0     0      0      0 --:--:--  0:00:01 --:--:--     0So I'm thinking (and from reading other threads) that grep doesn't support the ternary operator?Is there a simple one liner to do this without creating a shell script? (also my grep ' 200 ' is loose but I figure I can make that more specific later)
Grep Ternary Curl Reponse
shell;command line;grep
Like a quick study of the grep manual page should reveal, it allows for options, a search pattern (multiple patterns if you specify multiple -e options), and an optional list of file names, falling back to reading standard input like many other Unix filters if no file names are specified.  There is nothing about a ternary operator.If it were supported, it would arguably need to be a feature of the shell, but no shell I know of supports anything like this.  It would also clash with the syntax for ? as a wildcard metacharacter which (in isolation) matches any single-character file name.The usual shorthand idiom in Bourne-compatible shells looks likecurl https://www.example.com/ -I | grep -q ' 200 ' && echo 1 || echo 0This could accidentally match on a 200 somewhere else in the output.  For improved accuracy, try curl https://www.example.com/ -I | head -1 | grep ' 200 ' or perhaps curl https://www.example.com/ -I | awk 'NR==1 { print ($2==200 ? 1 : 0); exit }' where the precision of the matching operation is significantly improved.As an afterthought, if you want to print 0 in the case of invalid URLs and other unexpected errors too, you could set the default output to 0 and change it to 1 when you see 200, then print whatever it ended up as at the end.curl https://www.example.com/ -I |awk 'BEGIN { s=0 } NR==1 { s=($2==200); exit } END { print s }'