![]() local sudo -k # invalidates my current timestamp. Only when I then attempt to cd to a directory with sudo is a new shell created, and I am reminded explicitly of what is going on. ![]() Here's an example of how that shell function automatically "does the right thing." Notice that sudo ls -A /root behaves normally. If that succeeds, it replaces that bash shell with a new, interactive one that you can use. When you run sudo cd directory, first it forks off a separate bash shell from the one you're running the sudo function in and changes directory. It actually runs bash twice when you pass cd and a directory argument to it (and zero times otherwise). The code does not actually run sudo -s, but uses sudo bash, so the -c option works properly. The shell that it creates is similar to what you get with sudo -s. Running sudo -u user cd directory in particular is not supported, though you could extend the shell function to support that case. Note also that the shell function shown above does still let you pass other arguments to sudo, but that will prevent it from treating cd specially. That's why you can still use it in the normal ways people use sudo. Note, however, that you don't have to do that, because this sudo function actually does that itself whenever there are more or fewer than two arguments passed to it or the first argument passed to it is anything but cd. To manually run the regular sudo command directly even if the shell function is defined, run command sudo. To disable (i.e., undefine) the function once it's defined, run unset -f sudo. To check and see if sudo is currently defined as a shell function, and to see its current definition if it is one, run type sudo. (Of course, you can write a script for this, that just doesn't happen to be the approach I've taken here.) but only in the shell that ran the script, not for you as the caller. If you did run it as a shell script, it would define the function. This is really a library, rather than a standalone shell script. bashrc are not executable, don't mark sudo.bash executable with chmod. That takes effect in the current shell and its child shells, but not others. If you create a file called sudo.bash in your home directory with those contents, then you can make the sudo function available-so that it will run instead of the regular sudo command-by running. In that case, it's better to put it in its own file. ![]() ' bash _ "$2" # Use $2 as the dir in the intermediate shell, too.Ĭommand sudo could put this in your ~/.bashrc, though this is a weird enough way to use sudo that you may only want to enable it occasionally. Printf "%s: Type \"exit\" once you are done!\n" "$0" >&2Įxec bash # Replace this bash shell with an interactive one. If cd - "$2" then # When cd fails, its own message is enough. (That reminder is likely to be useful for users of any skill level, because one is not generally accustomed to being in a new shell when one runs sudo without -s, -i, or the name of an actual shell as an argument.) # Make sudo treat "sudo cd " as a special case and start a shell. Here's one way to write such a shell function, which also reminds you that you're in a new shell and that you should exit out of it when you are finished. Please do not use this as an alternative to learning why sudo cd does not otherwise succeed, because if you don't understand what is going on, then you will likely be very confused by being in a new shell (and you may not understand any error messages that occur). So, if you want to, then you can write a shell function (or a script) that performs the first two of those actions when sudo is followed by cd, and just runs sudo normally otherwise. ![]() It's important not to forget this, because you don't want to perform more actions as root than you intend!
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |