how to ignore error in shell script?

Im writing a script, but it is throwing some errors when something is not found, i need ignore those error.i mean when i run the script it should display positive results if any errors should not display.

#!/bin/sh
boot=$(ls /bootpool | grep boot | awk 'NR==1{print $1}')
data=$(ls /datapool | grep boot | awk 'NR==1{print $1}')
echo "boot"
if [ "$boot" == "boot" ] 
then
    echo "boot"
    pass=$(grep rootpw /bootpool/boot/loader.conf| grep -o '".*"' | sed 's/"//g' | awk 'BEGIN { ORS = " " } { print }')
elif [ "$data" == "boot" ]
then
    pass=$(grep rootpw /datapool/boot/loader.conf| grep -o '".*"' | sed 's/"//g' | awk 'BEGIN { ORS = " " } { print }')
fi
if [ $pass == edjos ]
then
   echo  "You are at default password. kindly change the password"
   oldpass=$(grep root /etc/master.passwd | awk 'NR==1 { print $1 }' | cut -d ':' -f 2 | sed 's/\$/\%/g')
   passwd
   newpass=$(grep root /etc/master.passwd | awk 'NR==1 { print $1 }' | cut -d ':' -f 2 | sed 's/\$/\%/g')
    if [ "$newpass" != "$oldpass" ]
     then              
          if [ "$boot" == "boot" ]
          then 
              sed -i.bak '/mfsbsd.rootpw/s/edjos//' /bootpool/boot/loader.conf
              sed -i.bak '/mfsbsd.rootpwhash/d' /bootpool/boot/loader.conf
              echo mfsbsd.rootpwhash=\"$newpass\"  >> /bootpool/boot/loader.conf
            echo "Great! password updated successfully"
          elif [ "$data" == "boot" ]
          then
             sed -i.bak '/mfsbsd.rootpw/s/edjos//' /datapool/boot/loader.conf
             sed -i.bak '/mfsbsd.rootpwhash/d' /datapool/boot/loader.conf
             echo mfsbsd.rootpwhash=\"$newpass\"  >> /datapool/boot/loader.conf
            echo "Great! password updated successfully"
         fi
    fi
    else
     echo "Great! you are having authorised password"
fi

You can put 2>/dev/null behind a command to suppress errors:

ls /home/cas/thisfolderdoesntexist -> error

ls /home/cas/thisfolderdoesntexist 2>/dev/null -> no output because error is suppressed.

You can also put 2>/dev/null behind a script to run the complete script with errors suppressed:

./script.sh 2>/dev/null

What your doing is redirecting (>) errors (2) to /dev/null. Every piece of data (in this case the output of your command(s)/script) that is redirected to /dev/null will be completely ignored. See it as a trash can for data.


Edit: 2>/dev/null suppresses the output of the command, not the complete pipe. In the example that you gave, you're supressing errors from the awk command. If the error is comming from the ls command, do the following (this will suppress errors from the ls command):

ls /bootpool 2>/dev/null | grep boot | awk 'NR==1{print $1}'

If the error is comming from the grep command:

ls /bootpool | grep boot 2>/dev/null | awk 'NR==1{print $1}'

I think you get it now.

A good thing to remember:

1 = stdout = normal output of a command

2 = stderr = error output of a command

0 = stdin = input to a command (this isn't usefull for redirecting, more for logging)


I also improved your script (using shellcheck, you can install it or use their online tool link):

#!/bin/sh
boot=$(find /bootpool/*boot* 2>/dev/null | sed "s|/.*/||")
data=$(find /datapool/*boot* 2>/dev/null | sed "s|/.*/||")
echo "boot"
if [ "$boot" = "boot" ] 
then
        echo "boot"
        pass=$(grep rootpw /bootpool/boot/loader.conf | grep -o '".*"' | sed 's|"||g' | awk 'BEGIN { ORS = " " } { print }')

elif [ "$data" = "boot" ]
then
        pass=$(grep rootpw /datapool/boot/loader.conf | grep -o '".*"' | sed 's|"||g' | awk 'BEGIN { ORS = " " } { print }')

else
        echo "Couldn't find boot in bootpool nor datapool"
        exit
fi

if [ "$pass" = edjos ]
then
        echo "You are at default password. kindly change the password"
        oldpass=$(grep root /etc/master.passwd | awk 'NR==1 { print $1 }' | cut -d ':' -f 2 | sed 's/\$/\%/g')
        passwd
        newpass=$(grep root /etc/master.passwd | awk 'NR==1 { print $1 }' | cut -d ':' -f 2 | sed 's/\$/\%/g')
        if [ "$newpass" != "$oldpass" ]
        then              
                if [ "$boot" = "boot" ]
                then 
                        sed -i.bak '/mfsbsd.rootpw/s/edjos//' /bootpool/boot/loader.conf
                        sed -i.bak '/mfsbsd.rootpwhash/d' /bootpool/boot/loader.conf
                        echo "mfsbsd.rootpwhash=\"$newpass\""  >> /bootpool/boot/loader.conf
                        echo "Great! password updated successfully"

                elif [ "$data" = "boot" ]
                then
                        sed -i.bak '/mfsbsd.rootpw/s/edjos//' /datapool/boot/loader.conf
                        sed -i.bak '/mfsbsd.rootpwhash/d' /datapool/boot/loader.conf
                        echo "mfsbsd.rootpwhash=\"$newpass\""  >> /datapool/boot/loader.conf
                        echo "Great! password updated successfully"
                fi
        fi
else
        echo "Great! you are having authorised password"
fi
  1. You were using == but /bin/sh doesn't make use of that. Only =. When you use /bin/bash, == will actually become usefull. But as you don't, you need to use =.
  2. I changed the way you set the boot and data variables. The way you did it was inefficient.
  3. When both $boot and $data are empty, the script will catch it instead of letting you continue. This is handy because in your second if statement, when $oldpass and $newpass aren't equal, it depends on either $boot or $data to contain "boot". But what if they don't? That's what the else is for in the first if-statement.
  4. Putting "" around variables. echo $var -> echo "$var"