I am wondering how to modify this code.

while [ 1 > 0 ]
fc -s
sleep 1

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
    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. – a CVn Jul 18 '13 at 12:45

You can try something along the lines of this:

 $ cat script.sh
 while sleep 1
     eval "$@"
 $ 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
 while sleep 1
     eval "$cmd"
 $ sh script.sh
  • 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 "$@"/"$@"/). – user26112 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
  • 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

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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