I have a bash script that mounts a usb drive, reads a text file on the usb drive, and echos that file to the php program that called it. The mount and unmount does not work.

If I mount the usb from command line the php works. Evidence points to the umount and mount not working. Any feedback would be appreciated.

Bash Script: DisplayTextFile.sh

#! /bin/bash
umount /dev/sdc1
mount -t vfat /dev/sdc1 /media/usbdisc -o uid=1000,gid=1000,utf8,dmask=000,fmask=000
$filetoecho=$(</media/usbdisc/textfile.txt)
echo "File Content: $filetoecho"
umount /dev/sdc1
exit $?

PHP that Calls Bash Script:

ob_start();
$command="/bin/bash /path/DisplayTextFile.sh"
passthru($command, &$result);
$filetoecho=ob_get_contents();
ob_end_clean();
2

There are 2 best solutions below

0
On

You probably aren't running as root, try to add your user to the sudoers group (install sudo if you don't have it) and then change your user privileges to use sudo without password.

It's not really safe, so you must know what you're doing.

0
On

As suggested above, I added DisplayTextFile.sh to sudoers at /etc/sudoers and it worked. I used nano to make the following entry:

www-data ALL=(ALL) NPPASSWD: /path/DisplayTextFile.sh

Once that entry was made, the problem was solved. Thanks a bunch.