script with mail

hello , what is wrong in this script ?

#!/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

thanks

I don't know if I am correct, try this -

a=$a+1

should be

a=`expr $a + 1`

no , unix return me that errors

/usr/bin/imei: line 5: [: =1: integer expression expected
/usr/bin/imei: line 34: [: : integer expression expected

where you are initializing the variable 'a'? if it has no value assigned to it, how can you put expression over it?

#!/bin/bash

limite=3

while [ "$a=1" -le $limite ];
do

in the first while

but im not very sure of that

Just check if you can initialize 'a' before while loop and instead of '$a=1' just use '$a'. Hope it solves your problem.

the systems return me

/usr/bin/imei: line 6: [: $a: integer expression expected
/usr/bin/imei: line 35: [: $a: integer expression expected

thanks and sorry , im newbie with this

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.

my mistake - '$a' shouldn't be in single quote. It will read it as one string. better use "$a".

#!/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

Hi,

  1. remove all semi-colons they are not required in shell scripts
  2. remove the single quotes around the varaibles in if conditon
    i.e.
 
while [ $a -le $limite ]

the single quotes disable varaible interpolation, to use the value of the variable either use

""

double quotes or don't use quotes at all
3.

$a=$a+1

is wrong
you can write this section as

let a=a+1

(notice no spaces between

a=a+1

)
or

a=`expr $a + 1`

(notice the spaces and backticks this time)

[ 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

thank you very much but , i have a problem

the code now is

#!/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