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 >> /var/log/ntpdate.log ||  ntpdate >> /var/log/ntpdate.log || ntpdate >> /var/log/ntpdate.log|| ntpdate  >> /var/log/ntpdate.log

I have tried two method,none of them succeed.

ntpdate  ||  ntpdate  || ntpdate || ntpdate  >> /var/log/ntpdate.log
ntpdate  ||  ntpdate  || ntpdate || ntpdate  &&>> /var/log/ntpdate.log

How to simplify the command?

share|improve this question
up vote 2 down vote accepted

Use a simple for construct:

for ip in; do
    out="$(ntpdate "$ip" 2>/dev/null)" && echo "$out" >>/var/log/ntpdate.log && break

Or saving IP addresses in an array:

ips=( )
for ip in "${ips[@]}"; do
    out="$(ntpdate "$ip" 2>/dev/null)" && echo "$out" >>/var/log/ntpdate.log && break

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

ips=( )
for ip in "${ips[@]}"; do
    ntpdate "$ip" 2>/dev/null >>/var/log/ntpdate.log && break
share|improve this answer
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 >> /var/log/ntpdate.log || ntpdate >> /var/log/ntpdate.log || ntpdate >> /var/log/ntpdate.log || ntpdate >> /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 || ntpdate >> /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 || ntpdate ) >> /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
    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 >> /var/log/ntpdate.log
share|improve this answer

Your Answer


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.