3

How do I get an one liner for this condition?

if [ -f ~/.ssh/config ]
then
    echo -e "\xE2\x9C\x94 Config file existing"
fi

My attempt:

if [ ! -f ~/.ssh/config ] || echo -e "\xE2\x9C\x94 Config file existing"
user3142695
  • 1,599
  • so this is correct? if [ ! -f ~/.ssh/config ] || echo -e "\xE2\x9C\x94 Config file existing" I have to check for the opposite, right? – user3142695 Feb 11 '17 at 11:25
  • 1
    You don't want the if keyword. Normally you use && to connect the 2 commands and do not reverse the condition but using || with a reversed condition is valid. so [ -f ~/.ssh/config ] && echo ... – icarus Feb 11 '17 at 11:32
  • Don't forget that the first word after if is a command (if command; then A; else B; fi) -- that means that [ and [[ are commands. – glenn jackman Feb 11 '17 at 12:53
  • 1
    Please don't edit a question to include its answer — especially not a wrong answer. If your "Update" is not meant to be a proposed answer, explain what it is. – G-Man Says 'Reinstate Monica' Feb 11 '17 at 22:17

2 Answers2

8

Try this,

if [ -f ~/.ssh/config ]; then echo -e "\xE2\x9C\x94 Config file existing"; fi

or

[ ! -f ~/.ssh/config ] || echo -e "\xE2\x9C\x94 Config file existing"

or

[ -f ~/.ssh/config ] && echo -e "\xE2\x9C\x94 Config file existing"

and the else has to come last

[ -f ~/.ssh/config ] && echo -e "\xE2\x9C\x94 Config file existing" || echo -e "\xE2\x9E\x97 No config file"
hschou
  • 2,910
  • 13
  • 15
prado
  • 930
  • 1
  • 11
  • 33
4

The most readable way (IMHO) is to use the test utility explicitly:

test -f ~/.ssh/config && echo -e "\xE2\x9C\x94 Config file existing"
Kusalananda
  • 333,661