#!/bin/bash
limite=3
while [ "$a=1" -le $limite ];
do
if [ "$a" -eq 1 ];
then
pru=/usr/bin/mei.txt
cat $pru | mail -s "IMEIS" account@domain.com
fi
if [ "$a" -eq 2 ];
then
pru=/usr/bin/mei2.txt
cat $pru | mail -s "IMEIS" account@domain.com
fi
a=`expr $a + 1`
done
if [ "$a" -eq 2 ];
then
a=1
fi
First, remove the single quotes around the occurrences of $a.
Second, what shell are you using? Depending on this, you may be able to use shell arithmetic expressions, like a=$((a+1)) in bash, otherwise you will need to take a different approach, sth. like expr that donadarsh mentioned.
#!/bin/bash
limite=3
a=0
while ["$a" -le "3"];
do
if ["$a" -eq "1"];
then
pru=/usr/bin/mei.txt
cat $pru | mail -s "IMEIS" mathias@wirtel.com.ar
fi
if ["$a" -eq "2"];
then
pru=/usr/bin/mei2.txt
cat $pru | mail -s "IMEIS" mathias@wirtel.com.ar
fi
a=$a+1
done
if ["$a" -eq "2"];
then
a=1
fi
the code with " but return me this
/usr/bin/imei: line 6: [0: command not found
/usr/bin/imei: line 35: [0: command not found
#!/bin/bash
limite=3
a=0
while [ "$a" -le 3 ]; do
if [ "$a" -eq 1 ]; then
pru=/usr/bin/mei.txt
cat $pru | mail -s "IMEIS" mathias@wirtel.com.ar
fi
if [ "$a" -eq 2 ]; then
pru=/usr/bin/mei2.txt
cat $pru | mail -s "IMEIS" mathias@wirtel.com.ar
fi
#a=$a+1
a=`expr $a + 1`
done
if [ "$a" -eq 2 ]; then
a=1
fi
[ is command and need arguments and argument delimiters, it is not bracket like in the programming languages.
if variable is empty, then you get error if value is empty. In that case double quotes => you have always argumen even it's empty.
$a is not same as "$a" in the command line if value of the a is empty.
#!/bin/bash
limite=3
a=1
while [ "$a" -le "$limite" ] # this is okay, no problem
# or while (( a <= limite ))
do
if [ "$a" -eq 1 ] # delimiters after command [ and between arguments
# or if (( a == 1 ))
then
pru=/usr/bin/mei.txt
cat $pru | mail -s "IMEIS" account@domain.com
fi
if [ "$a" -eq 2 ]
# or if (( a == 2 ))
then
pru=/usr/bin/mei2.txt
cat $pru | mail -s "IMEIS" account@domain.com
fi
#$a=$a+1
((a=a+1)) # works fine in ksh and bash, but not in dash
a=$((a+1)) # posix compatible version
# or use let command, it's also builtin command, expr does not
done
if [ "$a" -eq 2 ]
# or if (( a == 2 ))
then
a=1
fi
#!/bin/bash
limite=2
a=1
while [ "$a" -le "$limite" ]; # this is okay, no problem
do
if [ "$a" -eq "1" ];
then
pru=/usr/bin/mei.txt
cat $pru | mail -s "IMEIS" account@domain.com
fi
if [ "$a" -eq "2" ];
then
pru=/usr/bin/mei2.txt
cat $pru | mail -s "IMEIS" account@domain.com
fi
a=$((a+1)) # posix compatible version
done
if [ "$a" -eq "2" ];
then
a=1
fi
they not have sintax error but , the send me 2 mails , the if not found , i dont know really why.
You have a loop that executes 3 times, a has the values 1, 2 and 3.
So, lap 1 it will do what's in the first condition; send mail with /usr/bin/mei.txt
lap 2 it will do what's in the second condition; send mail with /usr/bin/mei2.txt
lap 3 it will do nothing.
The last condition, after the while-loop will never be met.
And it's a good idea to use indenting, see some of the answers you got earlier.
but , they no repeat the loop while de condition its true ?
in this case a <= limite , so
if a = 1 do the first condition.
not do the second condition.
a++
out of while ,
the condition if final , its a = 2 ? no. so they not do nothing.
im wrong ?
the original idea its that if the script run one time , send the first condition , if they have a second run , they do the second condition if the script execute again the value "$a" return to 0