sub-if in shell script

hi all
i have script like this
#!/bin/bash
if [[ $1 -eq 1 ]] ; then
echo "printing report employee "
lp -d [name of printer] sp_employee
exit
fi
if [[ $1 -eq 2 ]] ; then
echo "printing report manager"
lp -d [name of printer] sp_manager
exit
fi
i want to an if after the lp -d command , if it doesnot work to echo "error in printing"
i've try this :
if [[ $1 -eq 1 ]] ; then
echo "printing report employee "
lp -d [name of printer] sp_employee
if [[$? -eq 1 ]]; then
echo "error in printing" //suppose that this is the way to check an error in printing
fi
exit
fi
now i've try to run this it give me an error:
syntax error in conditional expression: unexpected token'
syntax error near `;'
`if [[ $? -eq 1]] ; then '
can some one tell me what is wrong with my syntax ?
thanks in advance

Hi,

if [[ $1 -eq 1 ]] ; then 
  echo "printing report employee "
  lp -d [name of printer] sp_employee || echo "error in printing" 
fi

Should do the trick. The part after "||" is only executed if the
command preceding it failed. "&&" is only executed if the command
preceding it succeeded.

HTH Chris

thank you for your quick response
i've been thinking is there a way to improve the if
like
if [[ $1 -eq 1 ]] ; then
lp -d [name of printer] sp_employee || echo "error in printing"
"printing report employee "
fi
if it does not success so write echo "error in printing" otherwise to write echo "printing report employee "
can i complex both && and ||?
according to my first example it show printing report employee "
even if there is an error in printing.

Take a look at the following snippet and experiment with it.
There is no better way to learn than trying this out for yourself.

case $1 in
    1) echo "Printing report employee"
    lpd ... && echo succesful || echo "no way"
    ;;
    2) echo "Printing report manager"
    lpd ... && echo succesful || echo "no way"
    exit
    ;;
esac

Hi, Yes You can combine them, as in

lakris@home:~$ [ 1 -eq 1 ] && echo true || echo false
true
lakris@home:~$ [ 1 -eq 2 ] && echo true || echo false
false

so for You I guess this would work;

lp -d [name of printer] sp_employee && echo "printing report employee " || echo "error in printing"

Read it as a if-then-else construct, if the first statement is true (zero exit code) do the second statement, otherwise do the third.

/Lakris

thank you all it works great !