Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

How to run dpkg without shell?

Here is my problem:

I made and autorun.sh script in my USB key.

To run this script, I created rules in /etc/udev/rules/49-usb-rules:

SUBSYSTEM=="block", KERNEL=="sd*1", ACTION=="add", RUN+="/bin/sh /opt/local/usb_autorun.sh %E{ID_FS_LABEL} %E{ID_FS_UUID}"

/opt/local/usb_autorun.sh calls my autorun.sh located on my USB key.

It works well, but in that script, I try to run dpkg -i to install a Debian archive and it fails.

After some research, I tried to run:

at -f /media/usb-key/autorun.sh -v 12:00

And i had the same problem.

So I saw that dpkg seems to need a shell to be run.

I tried `dpkg-reconfigure --frontend=dialog debconf`` and set up non-interactive, but it didn't seems to work neither.

How can I run dpkg -i from my autorun.sh script?

Here is my autorun.sh (on the usb key):

#!/bin/bash

touch /tmp/fic

echo "Installation du firmware" > /tmp/fic

DIR=$( readlink -f "$( dirname "$0" )")

export PATH=$PATH

/usr/bin/dpkg -i --force-all $DIR/radiolive.deb >> /tmp/fic 2>&1

echo "FIN" >> /tmp/fic

Here is my usb_autorun.sh (on my hard-drive):

 #!/bin/bash

 SCRIPT=autorun.sh

 autorun() {
 if [ -z ${1} ]; then
    FILE=/media/${2}/${SCRIPT}
 else
    FILE=/media/${1}/${SCRIPT}
 fi

 sleep 10

  if [ -f ${FILE} ]; then
    /bin/bash ${FILE}
  fi
 }

 autorun $@ &
share|improve this question
    
(1) As far as I know, you need administrator privileges to run dpkg -i and install a package. You should use gksu. (2) Have you made the file executable (chmod +x)? (3) Have you added the #!/bin/sh or #!/bin/bash on top of the file? (4) It would help if you post the contents of the script. –  medigeek Jul 5 '12 at 11:54
2  
@medigeek gksu is graphical, and will fail if run in the background like this. Since dpkg is a non-graphical program, it can simply be run with sudo. (But if the script is itself running as root, then of course neither is necessary.) –  Eliah Kagan Jul 5 '12 at 11:56
    
udev is run as root. In my autorun.sh i create the file: "-rw-r--r-- 1 root root 317 5 juil. 14:02 /tmp/fic". So this script is well executed and with the good privileges. But thank's for your answer. My script has the shebang #!/bin/sh –  Korigan Jul 5 '12 at 12:09
    
${BASH_SOURCE[0]} is not supported in sh, but then again you run the file autorun.sh with /bin/bash in the usb_autorun script so that should work in theory... Try using #!/bin/bash shebang on both autorun and usb_autorun -- is there any output in /tmp/fic ? –  medigeek Jul 5 '12 at 18:04

1 Answer 1

up vote 1 down vote accepted

I think your problem is this line:

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

BASH_SOURCE is a "bad substitution" for sh.

On the other hand, I found this:

http://www.reactivated.net/writing_udev_rules.html#external-run

udev does not run these programs on any active terminal, and it does not execute them under the context of a shell. Be sure to ensure your program is marked executable, if it is a shell script ensure it starts with an appropriate shebang (e.g. #!/bin/sh), and do not expect any standard output to appear on your terminal.

So basically the problems could be:

a) You're not using a shebang for the usb_autorun.sh and you need to add #!/bin/bash to both autorun.sh and usb_autorun.sh

b) try this line instead and use #!/bin/sh on both autorun.sh and usb_autorun.sh:

DIR=$( readlink -f "$( dirname "$0" )")

Note: This solution is a bit unsafe (Anyone can run anything in autorun.sh and get it executed), but on a local basis I believe it's harmless. Nevertheless, I would use some other name instead of "autorun.sh", such as "install_deb.sh" -- just to avoid any malware scripts.

share|improve this answer
    
Thank's a lot! You were right! All is working well now! For the malware you are right, i will rename my autorun.sh. Thank's medigeek! I also had to add "export PATH" before dpkg instruction for this to work but now it's ok! –  Korigan Jul 6 '12 at 12:51
    
I also had to edit my rules and give him a numer greater than 49 to be sure to be execute. –  Korigan Jul 6 '12 at 13:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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