Skip to content
IRC-Coding IRC-Coding
Linux Bash Shell Scripting Tutorial Automation Command Line

Linux Bash Coding Tutorial: Quick Start Guide

Master Bash scripting fundamentals in under 5 minutes. Learn essential Linux shell programming techniques.

S

schutzgeist

2 min read
Linux Bash Coding Tutorial: Quick Start Guide

Quick Introduction in under 5 minutes

If you want to learn Bash programming, we recommend the following book on Amazon: https://amzn.to/3WQnERX ( this is an external affiliate link and will take you to Amazon)

Linux BASH Coding Programming Tutorial Workshop English

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

As usual, 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 “Verbindung zu $server auf Port $port herstellen…“

3. Execute Commands

Use backticks or $() to execute commands.

> hostname=$(hostname) > echo "Mein Hostname ist $hostname"

4. If-Else

Check conditions with if.

if [ "$port" -eq 6667 ]; then echo "Standard-IRC-Port erkannt." else echo "Nicht-Standard-Port." fi

5. Loops

For Loop

Iterate over a list of servers.

> for server in "irc.example.com" "irc.freenode.net"; do > echo "Verbinde zu $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 "Verbindung hergestellt!"

6. Functions

Define reusable blocks.

> verbinde_zu_server() { > echo "Versuche Verbindung zu $1..." > nc -z $1 $port && echo "Verbindung zu $1 erfolgreich!" > }

verbinde_zu_server “irc.irc-mania.com”

7. Case

Branch based on inputs.

> command="PRIVMSG" > > case $command in > "JOIN") echo "Ein Benutzer ist beigetreten." ;; > "PART") echo "Ein Benutzer hat den Kanal verlassen." ;; > "PRIVMSG") echo "Eine Nachricht wurde empfangen." ;; > *) echo "Unbekanntes Kommando." ;; > esac

8. Arrays

Store multiple values in an array.

channels=("general" "help" "random")
echo "Ich trete ${channels[0]} bei."

9. Read and Write Files

Read

**Read a file line by line. **

> while IFS= read -r line; do > echo "Empfangen: $line" > done < irc_messages.log > Write > Write to a file.
> echo "Nachricht gesendet an $server" >> irc_log.txt

10. Input and Output

Redirect Outputs Save the output of a command.

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

Pipes

Connect commands.

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

11. Process Management

Background Processes Run a command in the background.

> ping $server **&**

Kill Processes

Terminate a process.

kill %1

12. Debugging

Use set -x for debugging.

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

13. Command Line Arguments

Use input arguments in your script.

if [ "$1" = "start" ]; then echo "Starte den IRC-Client..." fi

14. Cron Jobs

Automate scripts with Cron.

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

Now we know the most important pillars of Linux Bash programming: Let’s start generating meaningful and practical examples so we can get productive right away. As a Linux and IRC user, I have tailored the examples accordingly

Linux IRC Coding Practical Examples

  1. Automatic 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="MeinNick"
channel="#meinchannel"
message="Hallo, dies ist eine automatische Nachricht!"

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 automatically respond

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

#!/bin/bash

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

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 :Hallo! Wie kann ich helfen?" >&3
    fi
done

3. Collect IRC channel statistics

Collects information about activity in an IRC channel (e.g. number of messages per user) and saves it to a file.

#!/bin/bash

server="irc.example.com"
port=6667
nickname="StatBot"
channel="#meinchannel"
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 the following book on Amazon:

https://amzn.to/3WQnERX ( this is an external affiliate link and will take you to Amazon)**

Back to Blog
Share:

Related Posts