简体   繁体   中英

Changing a users shell with a script?

Is there a proper way to change a user's shell variable with a script depending on what shell they use? Such as

GITSHELL=/bin/bash
if [ GITSHELL = $SHELL ]
then 
chsh git -s /usr/bin/git-shell
else
chsh git -s /bin/bash

Obviously there is some problems with this. I'm not sure how to word this so i can A: Run it as root and still call the user git's shell instead of root's using su in the statement somewhere or B: Run it as git and su -c root to change git's shell after the initial if?

The goal is to have a script i can run to change the user git's shell to bash when i need that user to temporarily have to ability to create folders and run git init --bare. And then i would run the script again to change the shell back to git-bash for security reasons. Is this possible or should i go about this a different way?

Let the user make that decision, controlled via an environment variable. Just write your script like this:

: ${GITSHELL:=/usr/bin/git-shell}

git -s "$GITSHELL"

Now, your script will use /usr/bin/git-shell unless explicitly overriden:

$ ./script.sh                     # use /usr/bin/git-shell
$ GITSHELL=/bin/bash ./script.sh  # use /bin/bash

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM