开发者

bash/ shell script while statement

开发者 https://www.devze.com 2023-02-20 13:50 出处:网络
I\'m new in shell programming ... basically I\'m novice at all but I need a simple script to do while loop and execute a php script . I\'ve tried the following :

I'm new in shell programming ... basically I'm novice at all but I need a simple script to do while loop and execute a php script . I've tried the following :

!/bin/bash
i=0
 while[ i < 13 ]
  do
php /var/www/html/pos.php &
(( i++ ))
done

but for some reasons the syntax is not good ... I'm gett开发者_如何学JAVAing error line 4: syntax error near unexpected token `do'


You need to have a space between while and the left bracket [, and you need to put the do on a separate line or use a semicolon (both of those are fairly common mistakes when writing loops). Additionally, the left bracket [ is equivalent to man test which supports -lt but not <:

function doStuff() {
  local counter=0
  while [ $counter -lt 10 ]
  do
     echo $counter
     let counter=$counter+1
  done
}
doStuff

OR

function doStuff() {
  local counter=0
  while [ $counter -lt 10 ] ; do
     echo $counter
     let counter=$counter+1
  done
}
doStuff


!/bin/bash
i=0
while (( i < 13 ))
do
    php /var/www/html/pos.php &
    (( i++ ))
done


alternatively, you can use a for loop

for((i=1;i<=13;i++))
do
  php /var/www/html/pos.php &
done

since the for loop already creates the counter you, you don't have to declare a counter manually.


can't see your code, but it should be like this

while [ $i -ne 3 ]
do
   echo "on number $i of 3"
   i=`expr $i + 1`
done


I suppose that you want to do something like:

i=0; while (($i<10)); do i=$((i+1)); echo $i; done
0

精彩评论

暂无评论...
验证码 换一张
取 消