Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top
ntpdate 110.75.186.247 >> /var/log/ntpdate.log ||  ntpdate 110.75.186.247 >> /var/log/ntpdate.log || ntpdate 203.123.48.218 >> /var/log/ntpdate.log|| ntpdate 103.16.199.21  >> /var/log/ntpdate.log

I have tried two method,none of them succeed.

ntpdate 110.75.186.247  ||  ntpdate 110.75.186.247  || ntpdate 203.123.48.218 || ntpdate 103.16.199.21  >> /var/log/ntpdate.log
ntpdate 110.75.186.247  ||  ntpdate 110.75.186.247  || ntpdate 203.123.48.218 || ntpdate 103.16.199.21  &&>> /var/log/ntpdate.log

How to simplify the command?

share|improve this question
up vote 2 down vote accepted

Use a simple for construct:

#!/bin/bash
for ip in 110.75.186.247 110.75.186.247 203.123.48.218 103.16.199.21; do
    out="$(ntpdate "$ip" 2>/dev/null)" && echo "$out" >>/var/log/ntpdate.log && break
done

Or saving IP addresses in an array:

#!/bin/bash
ips=( 110.75.186.247 110.75.186.247 203.123.48.218 103.16.199.21 )
for ip in "${ips[@]}"; do
    out="$(ntpdate "$ip" 2>/dev/null)" && echo "$out" >>/var/log/ntpdate.log && break
done

Even simpler, get rid of the intermediate variable (thanks @kos):

#!/bin/bash
ips=( 110.75.186.247 110.75.186.247 203.123.48.218 103.16.199.21 )
for ip in "${ips[@]}"; do
    ntpdate "$ip" 2>/dev/null >>/var/log/ntpdate.log && break
done
share|improve this answer
4  
Also simply ntpdate "$ip" >>/var/log/ntpdate.log 2>/dev/null && break – kos Mar 27 at 11:17
    
kos' structure is more simple and more readable,@heemayl what does out= mean? – it_is_a_literature Mar 27 at 13:17
    
@it_is_a_literature out is a variable that stores the STDOUT of the ntpdate command..if you want you can replace that with what kos has written..thats simpler.. – heemayl Mar 27 at 13:20
    
@kos distracted enough to miss that :| – heemayl Mar 27 at 13:37
  1. Check if the file .bash_aliases exists in your home directory with ls -a ~/.bash_aliases (if not run the text editor of your choice, which is in my case nano ~/.bash_aliases)

  2. Add the following line into that file: alias update-time='ntpdate 110.75.186.247 >> /var/log/ntpdate.log || ntpdate 110.75.186.247 >> /var/log/ntpdate.log || ntpdate 203.123.48.218 >> /var/log/ntpdate.log || ntpdate 103.16.199.21 >> /var/log/ntpdate.log'

  3. Save and exit the editor

  4. Now you simply can run the command update-time and rest is magic :)

share|improve this answer

The problem with the construction

ntpdate 192.0.2.1 || ntpdate 203.0.113.2 >> /var/log/ntpdate.log

is that >> only applies to the last command. You can address that by using a parenthesis around the commands like this:

( ntpdate 192.0.2.1 || ntpdate 203.0.113.2 ) >> /var/log/ntpdate.log

You may think that typing ntpdate multiple times is redundant, and it should be possible to eliminate that. You can eliminate the repetition of ntpdate by using a loop. Though in your specific case the command is so simple, that the use of a loop introduce more complexity than it eliminates:

for IP in 192.0.2.1 203.0.113.2
do
    ntpdate "$IP" && break
done >> /var/log/ntpdate.log

Alternatively it may be that your original problem is better solved by invoking ntpdate only once with multiple server addresses:

ntpdate 192.0.2.1 203.0.113.2 >> /var/log/ntpdate.log
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.