Some Advice please. Almost Any thoughts welcome

So I got bored of every time I make a new server having to go in and make a new user for myself and his home directory and adding him to the /etc/sudoers file and such so I have made a script for it.

# Start of Script #

#!/bin/bash

echo "Hello Daniel"

useradd dspillman

echo "Now prepare a password for the user dspillman"

passwd dspillman

mkdir /home/dspillman

cd /home/cloud_user

echo "alias fred='echo "fred ate my last piece of pizza"'" >> .bashrc

cp -ar . /home/dspillman

cd ..

chown -R dspillman:dspillman dspillman

usermod -s /bin/bash dspillman

mv /etc/sudoers /etc/sudoerss

sed '21idspillman ALL=(ALL:ALL) ALL' /etc/sudoerss > /etc/sudoers

mv /etc/sudoers /etc/sudoerss

sed '26i dspillman ALL=(ALL:ALL) ALL' /etc/sudoerss > /etc/sudoers

mv /etc/sudoers /etc/sudoerss

sed '30i dspillman ALL=(ALL:ALL) ALL' /etc/sudoerss > /etc/sudoers

apt-get update

apt-get upgrade

echo "If you wish to reboot, now is the chance.

yes or no (Default is yes)"

read answer

if [ $answer = no ]

            then echo "You are on your own now"

             else reboot

fi

# End of Script #

 

And now LA has changed the servers, no offense taken, and I have had to change some things. I want to put into this scripts something that changes my prompt to a bright blue colored prompt or maybe red and changes it so instead of the hostname it says what number of server this is.(For example the first server would say,

dspillman@First:~$

But I want it changed in such a way that it will always be changed. So I must change the /home/dspillman/.bashrc file. OK. I have looked online and found some things. This next command will change it so that my prompt is color.

dspillman@First:~$ sed -i '137s/.*/force_color_prompt=yes/' .bashrc

The above line is NOT in the previously pasted script.

Ok now it is color, but a yellow one. But now to change the color. In the bashrc file in line 151 is what I need to change I think. (Why is there such a big gap in the bashrc file LA?  A few lines is understandable but 91?!!?) This line is pretty long but I understand where I need to change it. Here is the line that I find on a fresh server.

PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '

So what I want is a bright blue prompt. I looked on a bunch of pages to try and figure this out on my own and found a chart that said 34 is a blued one. Lets make that a little more clear. In the string above where it says [01;32m\] 32 stands for yellow and the 01 stands for bold. So I want to change that to say [01;34m/] which would make the ugly yellow part become an acceptable blue.Ok I got my blue. Now to change the hostname thing. Then I would put something like this in my script.

# Start#

read number

# and change it so that whatever number I put there will be part of the # prompt.

(Like "dspillman@'$number':~$. but I am not sure how to get  that in yet.)

So in the PS1 string above I would have it in stead of this.

 [01;32m\]\u@\h\

I would have...
[01;32m\]\u@$number\      (from my script)
But I have not figured out how to get that $number to be what I want.
So that is what I have come up with.

If this is kind of confusing I am sorry.
What I hope to accomplish with this...
1. Anybody can use the long script and customize it how you want but mine will remain the same, with a little adjustment  after this is solved. (In case you want it i can post a script I made for CentOS7 that is almost identical to this one but works for Red Hat machines.)
2. Get it so I don't have to manually change the .bashrc file.

If you have made it this far. CONGRATULATIONS! No you did not win a prize but i will explain the fifteenth line on the big script. I was trying to understand how the alias command worked and that was what I was practicing with. I was doing that because on LA's CentOS7 servers it did not have the alias for "ll". That was two lower case L's. It basically is "ls -al" but easier.


  • post-author-pic
    Keith T
    11-16-2018

    I think something like this should work for you to handle the dynamic word values:


    serverNumber() {
    local digits=("" one two three four five six seven eight nine)
    local -i number=$(hostname | grep -Po '\d+')
    if ((number < 10)); then
    echo "${digits[number]}"
    else
    echo $number
    fi
    }

    PS1="${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@$(serverNumber)\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ "

  • post-author-pic
    Daniel S
    11-21-2018

    Could you please explain what this script is doing?
    Thanks for the help.

  • post-author-pic
    Daniel S
    11-21-2018

    I tried to run this as a script after adding the shebang and this is not working. Please explain what this script is supposed to do.
    Thanks agian.

Looking For Team Training?

Learn More