Community Tutorials Linux Basics أساسيات برمجة Bash على Linux
أساسيات برمجة Bash على Linux
LINUX BASICS

أساسيات برمجة Bash على Linux

SKYLINE Knowledge Base
Photo by Sigmund on Unsplash

دليل عملي خطوة بخطوة لـ أساسيات برمجة Bash على Linux. أوامر مختبرة في الإنتاج، متطلبات مسبقة، تحقق نهائي وروابط لمواضيع ذات صلة.

كل صندوق Linux يأتي مع bash. معرفة كتابة سكربت 50 سطرًا بشكل صحيح تفصل "المشغّل" عن "مدير النظام".

المتطلبات المسبقة

  • shell على Linux/macOS.

الخطوة 1: shebang آمن والوضع الصارم

#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'

ضع هذه الأسطر أعلى كل سكربت وتجنّب 80% من فخاخ bash.

الخطوة 2: المتغيرات والتنصيص

name="World"
echo "Hello, ${name}"

file="my report.txt"
rm "$file"

: "${PORT:=8080}"

اقتبس دائمًا.

الخطوة 3: الشروط

if [[ "$1" == "deploy" ]]; then
    echo "Deploying..."
elif [[ -z "$1" ]]; then
    echo "No arg given" >&2
    exit 2
fi

if [[ -f "/etc/passwd" ]]; then echo "exists"; fi
if [[ -x "$(command -v jq)" ]]; then echo "jq installed"; fi
if [[ "$EUID" -ne 0 ]]; then exit 1; fi

استخدم [[ ]] وليس [ ].

الخطوة 4: الحلقات

for f in /var/log/*.log; do echo "$f"; done
for i in {1..5}; do echo "$i"; done

while IFS= read -r line; do echo "got: $line"; done < hosts.txt

while IFS= read -r host; do
    ping -c1 -W2 "$host" >/dev/null && echo "$host UP"
done < <(awk '{print $1}' /etc/hosts)

الخطوة 5: الدوال

log() {
    local level="$1"; shift
    echo "$(date +'%F %T') [$level] $*"
}

backup() {
    local src="$1"
    local dest="$2"
    log INFO "Backing up $src to $dest"
    rsync -a --delete "$src" "$dest"
}

[[ $# -lt 2 ]] && { log ERROR "Usage: $0 SRC DEST"; exit 2; }
backup "$1" "$2"

استخدم local دائمًا في الدوال.

الخطوة 6: trap للتنظيف

tmpdir=$(mktemp -d)
trap 'rm -rf "$tmpdir"' EXIT

curl -sfL https://example.sa/data.tar.gz -o "$tmpdir/data.tar.gz"
tar xzf "$tmpdir/data.tar.gz" -C "$tmpdir"

الخطوة 7: قالب سكربت شائع

#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'

log()  { echo "$(date +'%F %T') [$1] ${*:2}" >&2; }
die()  { log ERROR "$*"; exit 1; }
need() { command -v "$1" >/dev/null 2>&1 || die "Missing: $1"; }

input=""; output=""
while getopts "hi:o:" opt; do
    case "$opt" in
        i) input="$OPTARG" ;;
        o) output="$OPTARG" ;;
    esac
done

[[ -z "$input"  ]] && die "Missing -i"
need rsync
log INFO "Copying $input -> $output"
rsync -a "$input" "$output"

الخطوة 8: lint السكربتات

sudo apt install -y shellcheck
shellcheck deploy.sh

التحقق

bash --version | head -1
shellcheck --version

الخاتمة

set -euo pipefail، اقتباس المتغيرات دائمًا، [[ ]]، trap '...' EXIT، و shellcheck في CI.

الخطوات التالية

SKYLINE Engineering

@skyline

The engineering team at SKYLINE Industrial Solutions. We publish field-tested guides drawn from real KSA and GCC deployments.

See author profile
SKYLINE engineering services

Need this implemented for you?

Reading is free — building it right takes a team. SKYLINE engineers ship Linux Basics for Aramco vendors, banks, hospitals and government agencies across Saudi Arabia. Talk to us before you start.

Aramco Approved Contractor ISO 9001 · ISO 27001 SAMA CSF aligned NCA ECC ready 247+ KSA clients

Comments

0 total · 0 threads
Be the first to leave a comment.