Skip to content
IRC-CodingIRC-Coding
LinuxBashShell ScriptingTutorialAutomationCommand Line

Linux Bash Coding Tutorial for Beginners

Quick start to Bash programming. Learn Bash scripting fundamentals for Linux in under 5 minutes.

S

schutzgeist

3 min read
Linux Bash Coding Tutorial for Beginners

Quick Introduction in Under 5 Minutes

If you want to learn Bash programming, we recommend this book from Amazon: https://amzn.to/3WQnERX (this is an external affiliate link that takes you to Amazon)

Linux BASH Coding Programming Tutorial Workshop

This is a quick Linux Bash coding tutorial. Within a few minutes, you’ll be able to write simple scripts and programs. This guide also helps you get up to speed faster with programming books.

As always, everything “in a nutshell.”

1. Variables

Variables store data that you can use in your script.

server="irc.IRC-Mania.com"
port=6667

2. Echo

Use echo to output text.

echo "Connecting to $server on port $port..."

3. Running Commands

Use backticks or $() to execute commands.

> hostname=$(hostname) > echo "My hostname is $hostname"

4. If-Else

Check conditions with if.

if [ "$port" -eq 6667 ]; then echo "Standard IRC port detected." else echo "Non-standard port." fi

5. Loops

For Loop

Iterate over a list of servers.

> for server in "irc.example.com" "irc.freenode.net"; do > echo "Connecting to $server..." > done

While Loop

Repeat until a condition is met.

connected=0 while [ $connected -eq 0 ]; do nc -z $server $port && connected=1 sleep 5 done echo "Connection established!"

6. Functions

Define reusable blocks of code.

> connect_to_server() { > echo "Attempting connection to $1..." > nc -z $1 $port && echo "Connection to $1 successful!" > }

connect_to_server “irc.irc-mania.com”

7. Case

Branch based on input values.

> command="PRIVMSG" > > case $command in > "JOIN") echo "A user joined." ;; > "PART") echo "A user left the channel." ;; > "PRIVMSG") echo "A message was received." ;; > *) echo "Unknown command." ;; > esac

8. Arrays

Store multiple values in an array.

channels=("general" "help" "random")
echo "I'm joining ${channels[0]}."

9. Reading and Writing Files

Reading

Read a file line by line.

> while IFS= read -r line; do > echo "Received: $line" > done < irc_messages.log

Writing

Write to a file.

> echo "Message sent to $server" >> irc_log.txt

10. Input and Output

Redirecting Output

Save the output of a command.

nc -z $server $port > /dev/null 2>&1

Pipes

Connect commands together.

echo "Hello IRCWorld" | tr 'a-z' 'A-Z'

11. Process Management

Background Processes

Run a command in the background.

> ping $server **&**

Terminating Processes

Kill a process.

kill %1

12. Debugging

Use set -x for debugging.

set -x
connect_to_server "irc.example.com"
set +x

13. Command-Line Arguments

Use input arguments in your script.

if [ "$1" = "start" ]; then echo "Starting the IRC client..." fi

14. Cron Jobs

Automate scripts with Cron.

0 0 * * * /path/to/irc_script.sh

Now you know the essential foundations of Linux Bash programming. Let’s move on to practical, real-world examples so you can start getting productive right away. As a Linux and IRC user, I’ve tailored these examples accordingly.

Linux IRC Coding Practical Examples

1. Auto-login and Send Messages to an IRC Channel

This script automatically logs into an IRC server, joins a channel, and sends a message.

#!/bin/bash

server="irc.example.com"
port=6667
nickname="MyNick"
channel="#mychannel"
message="Hello, this is an automatic message!"

exec 3<>/dev/tcp/$server/$port

echo "NICK $nickname" >&3
echo "User $nickname 0 * :$nickname" >&3
sleep 5
echo "JOIN $channel" >&3
sleep 2
echo "PRIVMSG $channel :$message" >&3

2. Monitor an IRC Channel and Auto-Reply

This script monitors messages in an IRC channel and responds automatically to specific keywords.

#!/bin/bash

server="irc.example.com"
port=6667
nickname="BotNick"
channel="#mychannel"

exec 3<>/dev/tcp/$server/$port

echo "NICK $nickname" >&3
echo "User $nickname 0 * :$nickname" >&3
sleep 5
echo "JOIN $channel" >&3

while IFS= read -r line <&3; do
    echo "$line"
    if [[ "$line" == *"hello"* ]]; then
        echo "PRIVMSG $channel :Hi! How can I help?" >&3
    fi
done

3. Collect IRC Channel Statistics

Gathers activity information from an IRC channel (such as message count per user) and saves it to a file.

#!/bin/bash

server="irc.example.com"
port=6667
nickname="StatBot"
channel="#mychannel"
statsfile="channel_stats.txt"

exec 3<>/dev/tcp/$server/$port

echo "NICK $nickname" >&3
echo "User $nickname 0 * :$nickname" >&3
sleep 5
echo "JOIN $channel" >&3

declare -A user_stats

while IFS= read -r line <&3; do
    if [[ "$line" =~ ^:([^!]+)! ]]; then
        user="${BASH_REMATCH[1]}"
        ((user_stats["$user"]++))
    fi
done

If you want to learn Bash programming, we recommend this book from Amazon:

https://amzn.to/3WQnERX (this is an external affiliate link that takes you to Amazon)

Back to Blog
Share:

Nächster Artikel in Tutorials

Weiterlesen
Building GUI Desktop Apps on Linux

Related Posts