Unix Bootcamp

Welcome to our beginner-friendly Unix training page. Whether you're a developer, system admin, or just curious about Unix-based systems, this guide will walk you through essential concepts and commands.

What is Unix?

Unix is a powerful, multiuser, multitasking operating system used widely in servers, desktops, and mobile devices. Many modern operating systems like Linux and macOS are Unix-based.

Basic Unix Commands

Basic vi Editor Tutorial

The vi editor is a powerful text editor available on most Unix systems. Here's a quick guide to help you get started:

Tip: If you're new, practice in a test file before editing critical scripts.

Unix Shell Scripting

Shell scripts are text files containing Unix commands. They help automate tasks. Start a script with #!/bin/bash and write commands line by line.

Shell Script Tutorial

Let's write a simple script that greets the user and shows the current date.

#!/bin/bash

echo "Welcome to the Unix Shell Script Tutorial"
echo "Today is: $(date)"
echo "Your current directory is: $(pwd)"

Steps to run the script:

  1. Open a terminal and create a file:
    vi myscript.sh
  2. Press i to enter insert mode and paste the script
  3. Save and exit: Press Esc, then type :wq and press Enter
  4. Make it executable:
    chmod +x myscript.sh
  5. Run it:
    ./myscript.sh

This is just the beginning. You can use variables, conditionals, loops, and functions in shell scripts to build more powerful tools.

Using if Statements in Bash

The if statement allows your shell script to make decisions based on conditions. Here's the basic syntax:

if [ condition ]
then
    # commands to execute if condition is true
fi

Example 1: Check if a Number is Positive

#!/bin/bash

echo "Enter a number:"
read num

if [ $num -gt 0 ]
then
    echo "The number is positive."
fi

Example 2: Check if a File Exists

#!/bin/bash

echo "Enter a filename:"
read file

if [ -f "$file" ]
then
    echo "File '$file' exists."
else
    echo "File '$file' does not exist."
fi

Common Conditions

Pro Tips

Practice Exercise

Create a script that checks whether a user is root:

#!/bin/bash

if [ "$USER" == "root" ]
then
    echo "You are root!"
else
    echo "You are not root."
fi

Using while Loops in Bash

The while loop is used to execute a block of code repeatedly as long as a given condition is true.

Basic Syntax

while [ condition ]
do
    # commands to execute
done

Example 1: Countdown from 5

#!/bin/bash

count=5

while [ $count -gt 0 ]
do
    echo "Countdown: $count"
    count=$((count - 1))
done

echo "Blast off!"

Notes

Reading User Input in Bash

In Bash, the read command is used to take input from the user during script execution.

Basic Syntax

read variable_name

The user's input is stored in variable_name.

Example 1: Greeting the User

#!/bin/bash

echo "What is your name?"
read name
echo "Hello, $name! Welcome to Unix Training."

Example 2: Multiple Inputs

#!/bin/bash

echo "Enter your first and last name:"
read first last
echo "Hello, $first $last!"

Example 3: Prompt on the Same Line

#!/bin/bash

read -p "Enter your age: " age
echo "You are $age years old."

Example 4: Silent Input (e.g. passwords)

#!/bin/bash

read -sp "Enter your password: " password
echo
echo "Password received (but not shown for security)."

Tips

Practice Task

Create a script that asks for a favorite programming language and responds accordingly:

#!/bin/bash

read -p "What is your favorite programming language? " lang

if [ "$lang" == "bash" ]
then
    echo "Nice! You're already using it!"
else
    echo "$lang is cool too!"
fi

SSH Tutorial: Connecting to a Remote Unix Server

SSH (Secure Shell) is a protocol that allows you to securely connect to a remote Unix or Linux system over a network. It's commonly used by system administrators, developers, and IT professionals.

What You Need

Connect to a Remote Server

Use this command in your terminal:

ssh username@remote_host

Example:

ssh john@example.com

If it’s your first time connecting, you’ll be asked to confirm the server’s authenticity. Then enter your password when prompted.

Common SSH Options

Useful Tips

Exit the SSH Session

exit

This will close the remote session and return you to your local terminal.