LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-13-2014, 11:07 PM   #1
Bobbyyyy
LQ Newbie
 
Registered: Apr 2014
Posts: 4

Rep: Reputation: Disabled
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.
 
Old 04-13-2014, 11:11 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
What part are you getting hung up on? What language?
 
Old 04-13-2014, 11:16 PM   #3
Bobbyyyy
LQ Newbie
 
Registered: Apr 2014
Posts: 4

Original Poster
Rep: Reputation: Disabled
I am just not sure on how to write the whole thing in Linux to create into a batch file.
 
Old 04-13-2014, 11:26 PM   #4
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
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.
 
Old 04-14-2014, 01:48 AM   #5
kooru
Senior Member
 
Registered: Sep 2012
Posts: 1,385

Rep: Reputation: 273Reputation: 273Reputation: 273
Basically:

Code:
while true # start loop
do
  first_command # run 1st command
  sleep 1 # sleep for 1 second
  second_command # run second command
done
 
Old 04-14-2014, 06:16 AM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
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
 
Old 04-14-2014, 06:55 AM   #7
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 2,321
Blog Entries: 1

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
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.
 
Old 04-14-2014, 08:42 AM   #8
onebuck
Moderator
 
Registered: Jan 2005
Location: Midwest USA, Central Illinois
Distribution: Slackware®
Posts: 12,541
Blog Entries: 23

Rep: Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943
Member Response

Hi,

Welcome to LQ!
Quote:
Originally Posted by Bobbyyyy View Post
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.
 
Old 04-14-2014, 10:48 AM   #9
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 4,087
Blog Entries: 10

Rep: Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520
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.
 
1 members found this post helpful.
Old 04-14-2014, 03:13 PM   #10
Bobbyyyy
LQ Newbie
 
Registered: Apr 2014
Posts: 4

Original Poster
Rep: Reputation: Disabled
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?
 
Old 04-14-2014, 03:19 PM   #11
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
Quote:
Originally Posted by Bobbyyyy View Post
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).
 
Old 04-14-2014, 03:25 PM   #12
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 4,087
Blog Entries: 10

Rep: Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520
Quote:
Originally Posted by Bobbyyyy View Post
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.
 
Old 04-14-2014, 03:29 PM   #13
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 4,087
Blog Entries: 10

Rep: Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Timing a Command in a Loop: ali2011 Programming 2 01-01-2012 12:26 PM
[SOLVED] While loop in SSH command gburton Linux - Newbie 3 06-15-2011 03:29 PM
[SOLVED] ssh command in a loop ? gadikota Linux - Newbie 3 01-04-2011 11:06 PM
for loop in awk command jssidhu4 Programming 7 03-31-2010 05:19 AM
how to loop a command? adamrosspayne Linux - Newbie 15 06-23-2006 07:33 PM


All times are GMT -5. The time now is 02:51 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration