LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-27-2015, 08:58 PM   #1
gat3
LQ Newbie
 
Registered: May 2010
Distribution: OBsd, Slackware
Posts: 11

Rep: Reputation: 0
To simplify my bash loop script


Hi folks.
I'd like to make a simplified loop script,like

#!/bin/sh
while true
do
command1;command2;command3
sleep $somevariabl
done

in that case I can only type like


#./loop.sh command1;command2;command3 somevariable.

how could I achieve this?

Last edited by gat3; 05-28-2015 at 01:29 PM.
 
Old 05-27-2015, 09:09 PM   #2
veerain
Senior Member
 
Registered: Mar 2005
Location: Earth bound to Helios
Distribution: Custom
Posts: 2,524

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
The code:

Code:
#!/bin/sh
while true; do
$1; $2; $3
sleep $4
done
You run the script like this:
Code:
./loop.sh "command1" "command2" "command3" "sleep_time"
And this script will loop indefinitely. So would need to terminate it with 'Ctrl-C' or using kill command.

Do read man page of bash. Also there are basic as well as advanced BASH guides available from TLDP.

Last edited by veerain; 05-27-2015 at 09:25 PM.
 
Old 05-27-2015, 09:12 PM   #3
gat3
LQ Newbie
 
Registered: May 2010
Distribution: OBsd, Slackware
Posts: 11

Original Poster
Rep: Reputation: 0
thanks for your response.
But in the first place I don't know how many commands I would pass to the script.
So it looks not like a perfect form.
Quote:
Originally Posted by veerain View Post
The code:

Code:
#!/bin/sh
while true; do
$1; $2; $3
sleep $4
done
You run the script like this:
Code:
./loop.sh "command1" "command2" "command3" "sleep_time"
Do read man page of bash. Also there are basicas well as advanced BASH guides available from TLDP.

Last edited by gat3; 05-27-2015 at 09:15 PM.
 
Old 05-27-2015, 09:27 PM   #4
veerain
Senior Member
 
Registered: Mar 2005
Location: Earth bound to Helios
Distribution: Custom
Posts: 2,524

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
So you can pass sleep_time as first parameter and the commands as next. And use a loop to execute the commands one by one.
 
Old 05-27-2015, 09:41 PM   #5
gat3
LQ Newbie
 
Registered: May 2010
Distribution: OBsd, Slackware
Posts: 11

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by veerain View Post
So you can pass sleep_time as first parameter and the commands as next. And use a loop to execute the commands one by one.
can you explain more?
if I use a loop to execute the commands one by one,in what way I could pass those commands to the script?
since I don't know how many commands I would pass to.
 
Old 05-27-2015, 11:14 PM   #6
veerain
Senior Member
 
Registered: Mar 2005
Location: Earth bound to Helios
Distribution: Custom
Posts: 2,524

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
You use a shell feature which gives user a count of how many positional parameters (arguments) were passed.
 
Old 05-28-2015, 03:05 AM   #7
gat3
LQ Newbie
 
Registered: May 2010
Distribution: OBsd, Slackware
Posts: 11

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by veerain View Post
You use a shell feature which gives user a count of how many positional parameters (arguments) were passed.
many thanks



for var in "$@"
do
echo "$var"
done
 
Old 05-28-2015, 01:22 PM   #8
gat3
LQ Newbie
 
Registered: May 2010
Distribution: OBsd, Slackware
Posts: 11

Original Poster
Rep: Reputation: 0
I found some problem in those things.

Code:
> bash -x ./2.sh ls "ls |wc" "ls ~" "seq 1 3" "seq 1 3|wc"                          
+ for var in '"$@"'
+ ls
1.sh  2.sh  3.sh  4.sh  5.sh
+ for var in '"$@"'
+ ls '|wc'
ls: cannot access |wc: No such file or directory
+ for var in '"$@"'
+ ls '~'
ls: cannot access ~: No such file or directory
+ for var in '"$@"'
+ seq 1 3
1
2
3
+ for var in '"$@"'
+ seq 1 '3|wc'
seq: invalid floating point argument: 3|wc
Try `seq --help' for more information.

> cat 2.sh                                                                           
#!/bin/bash
for var in "$@"
do
$var
done

Can someone help me?

Last edited by gat3; 05-28-2015 at 01:23 PM.
 
Old 05-28-2015, 01:37 PM   #9
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
for i in ls madness.


Code:
ls: cannot access ~: No such file or directory
and the use of "~"
 
Old 05-29-2015, 08:03 AM   #10
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,774

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
If you actually want to execute arbitrary shell commands (not just single programs), you should use eval:

Code:
for var in "$@"
do
    eval "$var"
done
 
1 members found this post helpful.
  


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
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 03:49 PM
Script to simplify remote installation of Slackware saulgoode Slackware 1 05-11-2015 11:49 PM
script while loop s_linux Linux - Newbie 2 01-21-2015 03:10 AM
Bash script issue (for loop inside a loop) Mperonen Programming 3 08-08-2013 02:14 AM
Simplify this shell script vinayakm Programming 5 03-12-2009 11:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:01 PM.

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
Open Source Consulting | Domain Registration