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 would like to trace a shell script for debugging purposes without having to modify its source code (e.g. change the shebang or insert a set -x at the beginning). It is a dpkg postinst script, so not actually called by myself on the shell.

In hope of finding an environment variable for this I searched the man page for dash, but could not find anything.

Is there some way to do this?

share|improve this question
1  
What I would recommend for this specific use case is to edit the deb file. It's easy in Emacs (install debian-el); or manually: extract the archive with ar x, extract the control file with tar x, edit the postinst script, and repack with tar cz and ar r. –  Gilles Jan 12 '12 at 18:05

2 Answers 2

up vote 4 down vote accepted

Since you have super user access, you can just change /bin/sh. Of course you'll be affecting anything that wants to use the default shell (for example, cron scripts), so try to restore it as soon as possible.

First, create the wrapper. Create in your home directory a file named mysh with this content:

#!/bin/dash
exec /bin/dash -x "$@"

Make it executable.

$ chmod +x ~/mysh

Then change /bin/sh. First, make sure to note where it is pointing

$ ls -l /bin/sh
lrwxrwxrwx 1 root root 9 Jan 12 17:42 /bin/sh -> /bin/dash

Then, recklessly change it. (Warning: there will be a fraction of a microsecond when your system doesn't have /bin/sh.)

$ sudo ln -sf ~/mysh /bin/sh

As soon as you finish your thing, restore it.

$ sudo ln -sf /bin/dash /bin/sh

Good luck!

share|improve this answer
    
you perhaps intended to use mysh symlinked by /bin/sh or /bin/dash. and use dash.real in mysh +1 for the idea. –  Nikhil Mulley Jan 12 '12 at 16:59
    
Yes, that's what it does. /bin/sh is a link to ~/mysh, and ~/mysh calls /bin/dash. Or I don't understand your comment? –  angus Jan 12 '12 at 17:11
    
Oh, I think I get it. You mean, change /bin/dash, too? Yes, you are right. I thought postinst scripts always used /bin/sh, that's why I didn't bother with that. –  angus Jan 12 '12 at 17:17

You could use the -x flag in the invoking command itself:

sh -x ./script
share|improve this answer
    
Yes, that should work. Unfortunately, in my specific use case (script is a dpkg postinst script), I am not the one invoking it. Is there a way to enable this globally? –  Legate Jan 12 '12 at 9:06
    
@Legate: could you please update your question with this information? –  enzotib Jan 12 '12 at 9:14
    
Sure, done. <15 char requirement on comments> –  Legate Jan 12 '12 at 9:46

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.