Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am wondering how to modify this code.

ls
while [ 1 > 0 ]
do
fc -s
sleep 1
done

For fc -s, you can refer to this question for details.

My question are as follows:

  1. How will it run? If you name it as test.sh.
  2. If I want fc -s represent the first line ls, how will I modify it? That is to say, I want to Run ls then sleep one second, then ls, then sleep one second,... Of course ls may be placed by some other command.
share|improve this question
2  
Welcome to Unix & Linux! Could you try elaborating your explanations a bit of what you're asking? I wasn't able to follow what your questions are? Specifically with #2. –  slm Jul 18 '13 at 11:44
    
@slm Is it more clearly? –  eccstartup Jul 18 '13 at 11:49
    
What does it imply: "ls may be placed by some other command." –  unxnut Jul 18 '13 at 12:00
    
@unxnut AVERYVERYVERYLONGCOMMAND for example. –  eccstartup Jul 18 '13 at 12:05
    
"I want to Run ls then sleep one second, then ls, then sleep one second,..." The answer to this is watch. No need to make your own. –  Michael Kjörling Jul 18 '13 at 12:45

2 Answers 2

up vote 3 down vote accepted

You can try something along the lines of this:

 $ cat script.sh
 while sleep 1
 do
     eval "$@"
 done
 $ ls
 $ sh script.sh !!

The script will run the command passed to it over and over again. !! evaluates to the last command.


If you want the command to be part of the script and don't want to type it in multiple locations, try this:

 $ cat script.sh
 cmd=ls
 while sleep 1
 do
     eval "$cmd"
 done
 $ sh script.sh
share|improve this answer
    
This can quite easily break -- reparsing with eval is almost always a bad idea. sprunge.us/TMij –  Chris Down Jul 18 '13 at 13:24
    
@ChrisDown: It's definitely not ideal. That particular problem can be fixed by removing the eval statement. (s/eval "$@"/"$@"/). –  paraxor Jul 18 '13 at 13:31

Why don't you use the watch command, which does exactly what you want? This is what it says in the manual:

watch - execute a program periodically, showing output fullscreen

To run the command every second:

watch -n 1 command
share|improve this answer
    
Yes, but how to get the command before the while loop? –  eccstartup Jul 18 '13 at 12:39
    
You need to be more specific here. What is this script good for? –  user2516278 Jul 18 '13 at 12:55
    
I need to a command like watch. But I don't want it presented fullscreen. I want all output shows again and again in the terminal. And knowing how to get the command before the while loop is another problem which is not as important. –  eccstartup Jul 18 '13 at 13:02
    
#/!bin/bash while true; do $1 sleep 1 done save this as test.sh, make it executable and execute ist e.g. test.sh ls –  user2516278 Jul 18 '13 at 13:12
    
#/!bin/bash <br/> while true <br/> do <br/> $1 <br/> sleep 1<br/> done –  user2516278 Jul 18 '13 at 13:20

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.