bash/ shell script while statement - bash

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 getting 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

Related

conditional execution of steps after called function in bash

Requirement: Based upon IF condition in the called function: myfunc, echo hello in the for loop should not get executed and control should go to the next iteration.
In the below script, when the value of k becomes 2 and 3, echo hello should not get executed.
This is the script that I am trying to develop but no success.
#!/usr/bin/env bash
myfunc() {
if [[ $k -gt 1 ]]; then
echo "in the loop"
return
else
echo continue
fi
}
for (( k=1; k<=3; k++ ))
do
myfunc
echo hello
done
Please help.
Your loop is all wrong and I don't know why you have an if / else if you're just interested in one output:
#!/usr/bin/env bash
myfunc() {
if [[ $k -lt 2 ]]; then
echo "hello my value is $k"
fi
}
for (( k=1; k<=3; k++ ))
do
myfunc
echo "$k just to prove it is looping" # this is always run regardless of what's in the function
done
output:
hello my value is 1
1 just to prove it is looping
2 just to prove it is looping
3 just to prove it is looping
#!/usr/bin/env bash
myfunc() {
if [[ $k -gt 1 ]]; then
echo "in the loop"
x=1
else
echo Welcome
fi
}
for (( k=1; k<=3; k++ ))
do
myfunc
if [[ $x -eq 1 ]];then
continue
fi
echo hello
done

Writing a do while loop in bash with multiple conditions

I'm having some trouble writing a do-while loop in bash with multiple conditions.
My code currently works when it is like this:
while
count=$((count+1))
( MyFunction $arg1 $arg2 -eq 1 )
do
:
done
But I want to add a second condition to the "do-while" loop like so:
while
count=$((count+1))
( MyFunction $arg1 $arg2 -eq 1 ) || ( $count -lt 20 )
do
:
done
When I do this I get an "command not found error".
I've been trying some of the while loop examples from this post but had no luck and the do-while example I use is from here. In particular the answer with 137 likes.
The ( is part of syntax and $count is not a valid command. The test or [ is a valid command that is used to "test" expressions.
while
count=$((count+1))
[ "$(MyFunction "$arg1" "$arg2")" -eq 1 ] || [ "$count" -lt 20 ]
do
:
done
The answer you mention uses arithmetic expressions with (( (not a single (, but double (( without anything between). You could also do:
while
count=$((count+1))
(( "$(MyFunction "$arg1" "$arg2")" == 1 || count < 20 ))
do
:
done
You can use for loop:
for ((count=0; i<20 && $(MyFunction $arg1 $arg2) == 1; count++)); do
echo $count
done

What is the difference between -lt and < in shell?

I'm studying shell scripting and don't understand the difference between -eq and ==, -lt and <, -gt and >, so on.
I'm trying to write a while loop printing out from 0 to 9
num=0
while [ $num -lt 10 ]; do
echo "$num"
((num++))
done
This code works but when I change -lt to <, it says No such file or directory.
num=0
while [ $num < 10 ]; do
echo "$num"
((num++))
done
What is the issue with < here? Do I always have to go for -lt in while loops? Is there a general way to do while loops? Appreciate if you can help.
Shell scripting has been always different when it comes to syntax.
so when you say -lt it means less than (<).so when you write your code it works totally fine
while [ $num -lt 10 ]; do
echo "$num"
((num++))
done
But when you use < this in the shell script it is used to read input from file or directory. So here in your case, it will search for the name of the file which is inside the $num variable
In simple words
-lt is Less than which is used for condition checking
< is used for Reading input from the files.
In commandline
< means read input from file
for example
grep "myname" < data.txt
also,
> redirect output to a file
for example ls > lists.txt
when executing $num < 10
it checking for a file named 10
The command [ specifies that -lt should be used to compare two integers. Expecting < to do anything useful is simply wishful.
Coincidentally, the character < is a metacharacter in bash used for input redirection. The error you get is due to the file 10 not existing in your cwd.
You can use '<' with double parentheses (integers) or curly brace (strings)
num=0
while (( $num < 10 )); do
echo "$num"
((num++))
done
and for strings
str="a"
while [[ $str < "aaaaa" ]]; do
echo "$str"
str+="a"
done

Why does math, like "while ($i < $MAX)", give "no such file or directory"?

I wrote a shell script into a file named test.sh, and the code as following:
echo "start"
declare -i i=1
declare -i MAX=99999999;
while ($i < $MAX)
do
# do something
let ++i;
done
echo "done"
The result is:
start
test.sh: line 3: 99999999: No such file or directory
done
I run it on Max os Yosemite 10.10.2 and the terminal is bash.
Where am i wrong?
Okay the problem is with ($i < $MAX) it will not work in bash. In your case it taking < as redirect operator so it is treating < $MAX as you are giving $MAX file redirection to any loop. SO it is treating $MAX as file. That's why the error.
use
[ "$i" -lt "$MAX" ] ### generic POSIX number comparison syntax
or
[[ $i -lt $MAX ]] ### extended (bash/ksh/zsh) number comparison syntax
or
[[ $i < $MAX ]] ### extended string comparison syntax (may give wrong result for numbers)
or
(( i < MAX )) ### extended number comparison syntax

Shell script - No such file error

#!/bin/bash
local dept=0
while [ $n < 5 ]
do
echo $n
$n++
done
this code returns error 7: cannot open 5: No such file
Where should I change?
You should use $n -lt 5. Bash reads the < there as redirection, so it tries to open a file named 5 and feed its contents to a command named $n
This works for me:
#!/bin/bash
n=0
while [ $n -lt 5 ]
do
echo $n
let n=$n+1
done
#!/bin/bash
n=0
while [[ "$n" < 5 ]]
do
echo $n
((n++))
done
~
Most portable (POSIX sh-compliant) way is:
#!/bin/sh -ef
n=0
while [ "$n" -lt 5 ]; do
echo "$n"
n=$(($n + 1))
done
Note:
"$n" - quotes around $n help against crashing with missing operand error, if n is not initialized.
[ (AKA test) and -lt - is a safe and fairly portable way to check for simple arithmetic clauses.
$((...)) is a safe and portable way to do arithmetic expansion (i.e. running calculations); note $n inside this expansion - while bash would allow you to use just n, the standard and portable way is to use $n.

Resources