running if statement on command line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting running if statement on command line
# 1  
Old 04-09-2009
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.
# 2  
Old 04-09-2009
if always requires fi to terminate.
so at this point, your command is incomplete and hence you are getting PS2 prompt.
# 3  
Old 04-09-2009
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.


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

But you may have too many double quotes :

Code:
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:

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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Another one line command where I'd like to determine if Ubuntu or Red Hat when running command

Hello Forum, I'm making very good progress on my report thanks to the very helpful people on this forum. I've been able to successfully create my report for my Red Hat servers. But I do have a few ubuntu servers in the mix and I'd like to capture some data from them when an ssh connection is... (8 Replies)
Discussion started by: greavette
8 Replies

2. Shell Programming and Scripting

Issue in running a command line utility in CRON

Hi Everyone! I am facing an issue in running a command line utility from the CRON. This utility displays IPC statistics on UNIX message queues: The "queue name" and the "count" of messages in the queue. When running this utility from prompt, it will provide an output on the screen, like the... (4 Replies)
Discussion started by: vai_sh
4 Replies

3. Programming

Passing arguments from command line to switch case statement in C

Hi Am pretty new to C.. Am trying to pass the arguments from command line and use them in switch case statement.. i have tried the following #include <stdlib.h> main(int argc, char* argv) { int num=0; if ( argc == 2 ) num = argv; printf("%d is the num value",num); switch ( num ) ... (2 Replies)
Discussion started by: Priya Amaresh
2 Replies

4. Shell Programming and Scripting

Running Two scripts in a single command line

Hi All, I have two scripts which i want to run in a single command line(or in a single script). And if both the scripts in the main script fail then i want to pass a value to a Application specific utility which will trigger some alert.The value can be anything. Somebody please help.It is... (1 Reply)
Discussion started by: shroh
1 Replies

5. Shell Programming and Scripting

running multiple command in a single line

Hi Can we run the linux command and per script in a single command $ cd /usr/local/adm/ ;ctsv scmtest_qabuild ;cspec.pl scmtest This is a combination of linux and clearcase command and last one is perl script with argument. I can see the first and 2nd coomand is executing but last... (6 Replies)
Discussion started by: anuragpgtgerman
6 Replies

6. Shell Programming and Scripting

running variable on command line

Hi, I have a unix script, and I have problem, where I work out the variable I need. But it will not excute the variable from the script ? See my script below for details : #!/bin/sh ENVGRP=`hostname |cut -c1-3 | tr '' ''` ENVINST=${ENVGRP}`hostname -i |cut -d\. -f 4` echo... (4 Replies)
Discussion started by: fettie
4 Replies

7. UNIX for Dummies Questions & Answers

Running set options from the command line and bash command

I'm reading about debugging aids in bash and have come across the set command. It says in my little book that an addition to typing set you can also use them "on the command line when running a script..." and it lists this in a small table: set -o option Command Line... (5 Replies)
Discussion started by: Straitsfan
5 Replies

8. UNIX for Advanced & Expert Users

Running ksh script from command line

Hi. I have a ksh script I want to run, but I'm connecting through a telnet and I don't want to FTP the scripts itself. is there a way of running the script from command line ? like ksh "The script itself..." Thanks, Ramon (4 Replies)
Discussion started by: mellowcandle
4 Replies

9. UNIX and Linux Applications

Running oracle commands Directly from Command Line

In mysql, I can get what I want from a database without having to log into mysql. works great for me and it is perfect. now, i want to be able to do the same in oracle but I dont know how to. For example, if i want to retrieve information from the mysql database from the command line, i issue... (4 Replies)
Discussion started by: SkySmart
4 Replies

10. Shell Programming and Scripting

running multiple command in same line

I have 5 hosts and each host as 3 java process .I have one machine which has ssh keys so it can login without any passwords etc to all the machines. How can I find out say jstack or some command so it goes to each machine and run the command . For example machine 1 has 3 java process and they... (2 Replies)
Discussion started by: gubbu
2 Replies
Login or Register to Ask a Question