How to execute chsh using a bash script I put up i

2019-07-21 19:51发布

问题:

I have a gist that I always use to install the packages I need on a fresh server.

http://gist.github.com/4372049

All I need to do is to type the following in the fresh server via ssh

bash -c "$(curl -fsSL https://raw.github.com/gist/4372049)" <mysqlPassword>

I will be good to go.

Now I have this series of steps I always need to perform on a fresh installation of ubuntu.

first at root i did a

echo $SHELL
I saw that I have /bin/bash

then i switch to www-data

sudo su www-data
then i do a

echo $SHELL
I saw that I had

/bin/sh
instead.

So I did a

chsh -s /bin/bash
I was prompted for my www-data password so I gave it.

Password:
after that I switch back to root

exit
then i log back into www-data

sudo su www-data
I checked the $SHELL again

echo $SHELL
I saw that now it is

/bin/bash

listed here in https://askubuntu.com/a/232663/10591

Is there a way to write a bash script I can put up in gist.github.com to use in a similar way to execute?

if so, how do I write the bash script?

UPDATE:

I realized that I was given a vote to close this question because it was deemed too localized.

Let me rephrase this to

how do I write a bash script that I can put up in gist and use it in my linux console such that it can take in arguments for username and password and therefore execute the command

chsh -s /bin/bash 

and supplying the password correctly?

This is my attempt: https://gist.github.com/simkimsia/5126919

the su worked, but not the chsh command

Update 2:

I have changed the script to be

EXPECTEDARGS=1
if [ $# -ne $EXPECTEDARGS -o "x$0" == "x"  -o $0 == "bash" ]; then
  echo "Usage:"
  echo "   Parameter 1: your username"
  echo "   Parameter 2: your password"
  exit 1
fi

CHANGESHELL_FOR_USER=$0
PASSWORD_OF_USER=$1

########################################
## switch to another user
## read this https://stackoverflow.com/a/1988255/80353
########################################
sudo -u $CHANGESHELL_FOR_USER -H sh -c "chsh -s /bin/bash" 

expect "*?assword:*" {send -- "$PASSWORD_OF_USER\r";}
expect eof

after reading how to use a shell script to supply a password when the interface asks for it

and

https://stackoverflow.com/a/1988255/80353

Now the problem is somehow sending the password when prompted for

Password: 

回答1:

As long as you are running the below command as root, you will be fine.

chsh -s /bin/bash <username>

in this case, it is

chsh -s /bin/bash www-data

See https://gist.github.com/simkimsia/4372049#file-installation-12-10-ubuntu-sh-L373



标签: bash shell gist