Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I have the following bash script:

#!/bin/bash -e

SUDO=sudo

$SUDO apt-get -y update || true
$SUDO apt-get -y install lxc bridge-utils || true

# remove the default bridge, if it exists
DEFAUT_BRIDGE=$(brctl show | grep "lxcbr0")
if ! [ -z "${DEFAULT_BRIDGE}" ]
then
      $SUDO ip link set dev lxcbr0 down
      $SUDO ip link del dev lxcbr0
fi

# add the WiFi and Mobile bridges, if they don't exist yet
WIFI_BRIDGE=$(brctl show | grep "lxcbr_wifi")
MOBILE_BRIDGE=$(brctl show | grep "lxcbr_wifi")

echo "${MOBILE_BRIDGE}"
echo "${WIFI_BRIDGE}"
if [ -z "${WIFI_BRIDGE}" ]
then
      echo "adding wifi bridge"
      $SUDO brctl addbr lxcbr_wifi
      $SUDO brctl addif lxcbr_wifi $(./network identify wifi)
      $SUDO ip link set dev lxcbr_wifi up
fi

if [ -z "${MOBILE_BRIDGE}" ]
then
    echo "adding mobile bridge"
    $SUDO brctl addbr lxcbr_mobile
    $SUDO brctl addif lxcbr_mobile $(./network identify mobile)
    $SUDO ip link set dev lxcbr_mobile up
 fi

This script fails at the second apt-get command, unless I run the script in debug mode, with the option set -x. I cannot understand why the script is crashing there, since apt-get returns 0 (I checked the value of $? ) and even if it returns an error, I have "|| true".

Weirder, if I run the script in debug mode, everything works. What I am missing ? Is it apt-get fault, or something in the script ?

share|improve this question

closed as off-topic by jasonwryan, Michael Homer, taliezin, lcd047, slm Jul 10 at 12:39

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – jasonwryan, Michael Homer, taliezin, lcd047, slm
If this question can be reworded to fit the rules in the help center, please edit the question.

    
what is the point of SUDO=sudo? –  mikeserv Jul 10 at 9:05
    
This script is a modified version of another one, where it was checked if the user running it was root or not. I know it is not useful here, but I did not want to place only a snippet where some variables where not defined –  ngoncalves Jul 11 at 18:02

1 Answer 1

It turns out I was looking in the wrong place. The script was failing at

    DEFAUT_BRIDGE=$(brctl show | grep "lxcbr0")

if the network bridge "lxcbr0" does not exits, grep fails with an error and stops the script. I modified this line to

    DEFAUT_BRIDGE=$(brctl show | grep "lxcbr0" || true)

and now everything works.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.