0

Pardon me as I am relatively new to this.

[ -f /etc/example/txt] && echo "True" || echo "False" 

I have the above code, I am trying to print out the hostname as well.

I have modified it to the following,

[ -f /etc/example/txt] && hostname = uname -n &&echo "$hostname:True" || echo "$hostname:False" 

Unfortunately the above doesn't work. Is there a way to have the hostname printed out along with True or False?

  • `hostname="$(uname -n)"; if [ -f /etc/example/txt ]; then echo "$hostname:True"; else echo "$hostname:False"; fi` – melpomene Jun 10 '16 at 15:09

1 Answers1

0

The following works for me:

 [ -f /tmp/txt ] && [ $hostname = $(uname -n) ] && echo "$hostname:True" || echo "$hostname:False"
GMichael
  • 2,726
  • 1
  • 20
  • 30
  • This won't work the way you think it will... When you define hostname don't preceed it with a "$". No spaces around the "=" sign. If /tmp/txt isn't a file it will skip to "echo "$hostname:False" which won't have the variable $hostname defined. – jgshawkey Jun 10 '16 at 17:19
  • `sHostName=$(uname -n) && [[ -f /tmp/txt ]] && echo "$sHostName:True" || echo "$sHostName:False"` – jgshawkey Jun 10 '16 at 17:20