From charlesreid1

Syntax

Defining functions

function dostuff() {
    if [[ -f "${DIR}" ]]; then
        echo "${DIR} exists!"
    fi
}

dostuff

Usage function

function usage() {
    echo ""
    echo "Title: A Brief Description"
    echo ""
    echo "Tell the user how to use the script."
    echo "Give them a simple example to copy and paste."
    echo ""
    exit 1;
}

if (( $# == 0 )); then
    echo "Use the default"


elif (( $# == 1 )); then
    echo "Use $1"


else
    usage

fi

DevOps

Creating a new user

The quick and dirty guide to creating a new user:

Use -s to specify shell

Use -d to specify home directory

Use -G to specify groups

useradd creates the user, usermod modifies the user

USERN="florence"

# create new user with specified shell and home dir
useradd -s /bin/bash -d /home/$USERN $USERN

# add to group1 and group2
usermod -a -G group1,group2 $USERN

# add to sudo group
usermod -a -G sudo $USERN

Now the user will need to log out and log back in to be able to use sudo command.

To make it passwordless (as on AWS nodes), you can add the following line to a new file in the directory /etc/sudoers.d/, which is sourced by the sudoers file:

echo "${USERN} ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/${USERN}

Finally, if you're on AWS, you are using a public key associated with that AWS node that allows you to connect to it via SSH. You need to include a copy of the private key in the SSH folder of the new user, so that you can use the private key to SSH in as that user.

Copy the contents of ~/.ssh/authorized_keys from the pre-configured ubuntu user into the file ~/.ssh/authorized_keys for the new user.

Once you're able to log in as the new user, associate it with a Github account by adding the public SSH key for the new account to your Github account's list of trusted SSH keys.

cat ~/.ssh/id_rsa.pub

the contents of this file should be copied and pasted into a new key in your Github account's settings.

Patterns

Check if root user

Use $(id -u) to get the UID. the UID will be 0 if the user is root.

if [ "$(id -u)" != "0" ]; then
    echo ""
    echo ""
    echo "This script should be run as root."
    echo ""
    echo ""
    exit 1;
fi



Flags