LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   2 command loop help (https://www.linuxquestions.org/questions/linux-newbie-8/2-command-loop-help-4175501602/)

Bobbyyyy 04-13-2014 10:07 PM

2 command loop help
 
I am not sure how to create a 2 command loop script. I want it to run one command, sleep for 1 then run the other command then loop this continuously. If anyone can help me with this, thanks.

suicidaleggroll 04-13-2014 10:11 PM

What part are you getting hung up on? What language?

Bobbyyyy 04-13-2014 10:16 PM

I am just not sure on how to write the whole thing in Linux to create into a batch file.

suicidaleggroll 04-13-2014 10:26 PM

Here's a basic tutorial on BASH scripting

http://www.tldp.org/LDP/Bash-Beginne...ers-Guide.html

Beyond that, you just need to open up a text editor of your choice.

kooru 04-14-2014 12:48 AM

Basically:

Code:

while true # start loop
do
  first_command # run 1st command
  sleep 1 # sleep for 1 second
  second_command # run second command
done


chrism01 04-14-2014 05:16 AM

More bash tutorials
http://rute.2038bug.com/index.html.gz
http://www.tldp.org/LDP/abs/html/

Have a crack and come back if you get stuck. We prefer to help rather than do it for you :)

Shadow_7 04-14-2014 05:55 AM

It depends on the language you want to use. For bash an example has already been given. Although calling sleep and looping is not a good practice for anything long term. Good enough for proof of concepts, but you'll burn through a lot pid's on the long haul.

onebuck 04-14-2014 07:42 AM

Member Response
 
Hi,

Welcome to LQ!
Quote:

Originally Posted by Bobbyyyy (Post 5152052)
I am not sure how to create a 2 command loop script. I want it to run one command, sleep for 1 then run the other command then loop this continuously. If anyone can help me with this, thanks.

Quote:

FYI: I suggest that you look at 'How to Ask Questions the Smart Way' so in the future your queries provide information that will aid us in diagnosis of the problem or query.
If you want Shell Scripting then from RUTE;
Quote:

  • Introduction
  • Looping -- while statement
  • Looping -- for statement
  • Looping over glob expressions
  • The case statement

More links;
Quote:

Just a few links to aid you to gaining some understanding;



1 Linux Documentation Project
2 Rute Tutorial & Exposition
3 Linux Command Guide
4 Bash Beginners Guide
5 Bash Reference Manual
6 Advanced Bash-Scripting Guide
7 Linux Newbie Admin Guide
8 LinuxSelfHelp
9 Utimate Linux Newbie Guide
10 Linux Home Networking
11 Virtualization- Top 10

The above links and others can be found at 'Slackware-Links'. More than just SlackwareŽ links!
Quote:

"Knowledge is of two kinds. We Know a subject ourselves, or we know where we can find information upon it."- Samuel Johnson
Quote:

"You must look into people as well as at them."-Chesterfield
Hope this helps.

rtmistler 04-14-2014 09:48 AM

Welcome to LQ.

Kooru has pretty much steered you in the correct direction, so I won't copy/paste my own variation.

However a good "kudo" as well as minor warning. The kudo is good that you are thinking to have the "sleep", the warning is that if you don't have the sleep, I've found that it's sometimes difficult to exit, CTRL-C or other, a bash script. This would be on a limited distribution where things aren't always the standard. Sometimes it's minor, you merely have to attach with another command prompt and kill that other task, but if you happen to be at a point where you have no network access yet, and solely have that one console, you may find that it locks you out and you have to reboot.

Bobbyyyy 04-14-2014 02:13 PM

while true # start loop
do
first_command # run 1st command
sleep 1 # sleep for 1 second
second_command # run second command
done

So about this code, two things. Could I put a sleep after the second command or is that unnecessary? Also is everything after the # not apart of the code and it can be deleted?

suicidaleggroll 04-14-2014 02:19 PM

Quote:

Originally Posted by Bobbyyyy (Post 5152485)
So about this code, two things. Could I put a sleep after the second command or is that unnecessary? Also is everything after the # not apart of the code and it can be deleted?

It depends on what you want the code to do. Do you want it to sleep for a second before going back to first_command, or do you want it to be immediate?

# in BASH denotes a comment, everything after it is ignored by the interpreter (unless it's on the first line and is part of a shebang, but that's another topic).

rtmistler 04-14-2014 02:25 PM

Quote:

Originally Posted by Bobbyyyy (Post 5152485)
while true # start loop
do
first_command # run 1st command
sleep 1 # sleep for 1 second
second_command # run second command
done

So about this code, two things. Could I put a sleep after the second command or is that unnecessary? Also is everything after the # not apart of the code and it can be deleted?

  1. Anywhere you put sleep, it will pause for that time, in this case 1 second. So if you don't care that between second_command and the next loop iteration where it will run first_command that there will be no delay, then you don't need sleep. Or you can put the sleep solely after the second command. Or you can put one after the first and after the second one too, that's purely your choice, no real functional problem however you choose there.
  2. The helpful thing to do is to place your code within [code][/code] tags. That will make it appear in a code block, such as this:
    Code:

    while true # start loop
    do
      first_command # run 1st command
      sleep 1 # sleep for 1 second
      second_command # run second command
    done

  3. Any thing after the # will be considered a comment. In fact, sometimes people use the # to comment out a line of debug which they use, perform their debug, and then leave in their script, but remove by making it a comment. The most common one which I use is near the top of my scripts
    Code:

    #set -xv
    which when included not as a comment, enables script debug statements.

rtmistler 04-14-2014 02:29 PM

Another thing to do is the absolute first line of the script should contain this, for bash
Code:

#!/bin/bash
or this for sh
Code:

#!/bin/sh
to indicate that it is a shell script.

I'd recommend you peruse this link if you have more specific questions about the first line Starting Off With a She-Bang if you go down about a page, it explains the purpose more better than I could, or should:
Quote:

The sha-bang ( #!) [1] at the head of a script tells your system that this file is a set of commands to be fed to the command interpreter indicated. The #! is actually a two-byte [2] magic number, a special marker that designates a file type, or in this case an executable shell script (type man magic for more details on this fascinating topic). Immediately following the sha-bang is a path name. This is the path to the program that interprets the commands in the script, whether it be a shell, a programming language, or a utility. This command interpreter then executes the commands in the script, starting at the top (the line following the sha-bang line), and ignoring comments.


All times are GMT -5. The time now is 02:28 PM.