0

I am trying to run commands as root and capture the output into a variable. However the variable "loopdev" is returning as empty.

sudo bash << "EOF"
whoami
loopdev=`losetup -f --show "${image}"`
echo "####" $loopdev "####"
EOF

The normal output for this command would be like below

$ image=sdimage.img
$ sudo losetup -f --show "${image}"
/dev/loop0
Bob R
  • 605
  • 1
  • 13
  • 25
  • "<< 'EOF' -> losetup: : failed to set up loop device: Success" (no loop device was created, not sure why it says success) – Bob R Oct 13 '18 at 21:17
  • Can u try to set "image" variable value inside the bash script ? As like below sudo bash << "EOF" whoami image=sdimage.img loopdev=`losetup -f --show "${image}"` echo "####" $loopdev "####" EOF – vijayalakshmi d Oct 13 '18 at 22:15

2 Answers2

0

I tried your problem as like below

test=12781278
sudo bash << "EOF"
whoami
loopdev=`echo Hi $test`
echo "####" $loopdev "####"
EOF

And the output is

#### Hi  ####

For your problem, the image variable value is not being passed to the bash shell that you are starting.

vijayalakshmi d
  • 686
  • 5
  • 8
0

The solution was to pass in the image variable like this:

image=rpi_2.img
sudo image="${image}" bash << 'EOF'
whoami
loopdev=`losetup -f --show "${image}"`
echo "####" $loopdev "####"
EOF

Output:

root
#### /dev/loop0 ####
Bob R
  • 605
  • 1
  • 13
  • 25