0

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.

2 Answers2

4

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
unxnut
  • 6,008
3

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
  • This can quite easily break -- reparsing with eval is almost always a bad idea. http://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 "$@"/"$@"/). –  Jul 18 '13 at 13:31