running if statement on command line

Hi, is it possible to run an if statement from the command line?

I am doing this

[root@test]# service postgresql status; if [[ $? -eq 1 ]] ; then awk '{print ""$2""}' /root/file.txt
>

But it throws me into a different mode ie the > then I have to do a ctrl+c. I want to do it this way because I will be executing the command from ssh.

if always requires fi to terminate.
so at this point, your command is incomplete and hence you are getting PS2 prompt.

You just didn't finish your command.
Every "if" has to have a "fi"
That "mode" with the prompt ">" is telling you that you have not yet finished what you started.

service postgresql status; if [[ $? -eq 1 ]] ; then awk '{print ""$2""}' /root/file.txt ; fi

But you may have too many double quotes :

if service postgresql status ; then awk '{print "$2"}' /root/file.txt ; fi

Doing this over ssh will require different quoting because the local shell will process one
"layer" of quoting and the remote system will process the remaining one(s).
You have to make sure that the quotes and metacharacters that need to be seen by the remote shell
get all the way to the remote system.
Fortunately, the only thing going on here is the $2 that the remote awk needs.

I think this will work:

ssh hostname "if service postgresql status ; then awk '{print \$2}' /root/file.txt ; fi "