Skip to content

Instantly share code, notes, and snippets.

@x-yuri
Last active July 1, 2019 07:47
Show Gist options
  • Select an option

  • Save x-yuri/949d1aac088283a1bfec903174608c46 to your computer and use it in GitHub Desktop.

Select an option

Save x-yuri/949d1aac088283a1bfec903174608c46 to your computer and use it in GitHub Desktop.
[ command

There exists a way in the shell to do different kinds of checks, like if a file exists:

$ if [ -e some-file ]; then echo exists; fi
exists

It looks like [ is a part of the syntax. But it's not. You can find the executable at /usr/bin/[ (although it may be implemented as a built-in). The long name is test. So, [ -e some-file ] is a command with 3 arguments. And there can be any other command in place of the square brackets:

$ if grep ^root: /etc/passwd &> /dev/null; then echo why wouldn\'t it be there?; fi

Next, here's how you can execute commands on a remote machine:

$ ssh user@host echo hello from the other side
hello from the other side

Now then, let's put it all together:

$ if ssh user@host [ -e some-file ]; then echo exists; fi
exists

Or:

$ if ssh user@host /usr/bin/[ -e some-file ]; then echo exists; fi
exists
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment