LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   To simplify my bash loop script (https://www.linuxquestions.org/questions/programming-9/to-simplify-my-bash-loop-script-4175543794/)

gat3 05-27-2015 08:58 PM

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?

veerain 05-27-2015 09:09 PM

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.

gat3 05-27-2015 09:12 PM

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 (Post 5368475)
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.


veerain 05-27-2015 09:27 PM

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.

gat3 05-27-2015 09:41 PM

Quote:

Originally Posted by veerain (Post 5368481)
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.

veerain 05-27-2015 11:14 PM

You use a shell feature which gives user a count of how many positional parameters (arguments) were passed.

gat3 05-28-2015 03:05 AM

Quote:

Originally Posted by veerain (Post 5368511)
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

gat3 05-28-2015 01:22 PM

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?

Habitual 05-28-2015 01:37 PM

for i in ls madness.


Code:

ls: cannot access ~: No such file or directory
and the use of "~"

ntubski 05-29-2015 08:03 AM

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



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