Lewati ke konten
Kembali ke Blog

Cara Membuat Bash Script untuk Automation

Β· Β· 3 menit baca

Bash scripting adalah skill penting untuk automating repetitive tasks di Linux. Mari pelajari dasar-dasarnya.

Creating Your First Script

Basic Script

#!/bin/bash
# This is a comment

echo "Hello, World!"

Make Executable

# Create script file
touch myscript.sh

nano myscript.sh

Make executable

chmod +x myscript.sh

Run script

./myscript.sh

Variables

Defining Variables

#!/bin/bash

String variable

name="John" echo "Hello, $name"

Number variable

age=25 echo "Age: $age"

Command output

current_date=$(date) echo "Today: $current_date"

User Input

#!/bin/bash

echo "What is your name?" read name

echo "Hello, $name!"

Conditional Statements

If-Else

#!/bin/bash

age=18

if [ $age -ge 18 ]; then echo "You are an adult" else echo "You are a minor" fi

Comparison Operators

# Numeric comparisons
-eq  # equal
-ne  # not equal
-gt  # greater than
-lt  # less than
-ge  # greater or equal
-le  # less or equal

String comparisons

= # equal != # not equal -z # empty string -n # non-empty string

File Tests

#!/bin/bash

file="test.txt"

if [ -f "$file" ]; then echo "File exists" fi

if [ -d "folder" ]; then echo "Directory exists" fi

Other tests:

-e exists

-r readable

-w writable

-x executable

Loops

For Loop

#!/bin/bash

Loop through list

for item in apple banana orange; do echo "Fruit: $item" done

Loop through numbers

for i in {1..5}; do echo "Number: $i" done

Loop through files

for file in *.txt; do echo "Processing: $file" done

While Loop

#!/bin/bash

counter=1

while [ $counter -le 5 ]; do echo "Count: $counter" ((counter++)) done

Functions

Defining Functions

#!/bin/bash

Function definition

greet() { echo "Hello, $1!" }

Call function

greet "World" greet "John"

Return Values

#!/bin/bash

add_numbers() { local result=$(($1 + $2)) echo $result }

sum=$(add_numbers 5 3) echo "Sum: $sum"

Practical Examples

Backup Script

#!/bin/bash

Backup script

backup_dir="/backup/$(date +%Y%m%d)" source_dir="/home/user/documents"

mkdir -p "$backup_dir" cp -r "$source_dir" "$backup_dir"

echo "Backup completed: $backup_dir"

System Monitor

#!/bin/bash

echo "=== System Monitor ===" echo "Date: $(date)" echo "" echo "Disk Usage:" df -h / | tail -1 echo "" echo "Memory Usage:" free -h | grep Mem echo "" echo "CPU Load:" uptime | awk '{print $10 $11 $12}'

File Organizer

#!/bin/bash

Organize files by extension

for file in ~/Downloads/ ; do if [ -f "$file" ]; then ext="${file##.}" mkdir -p ~/Downloads/$ext mv "$file" ~/Downloads/$ext/ fi done

echo "Files organized!"

Error Handling

Exit Codes

#!/bin/bash

if ! command -v git &> /dev/null; then echo "Git not installed" exit 1 fi

echo "Git is installed" exit 0

Set Options

#!/bin/bash

set -e # Exit on error set -u # Error on undefined variables set -o pipefail # Fail on pipe errors

Kesimpulan

Bash scripting adalah powerful tool untuk automation. Start dengan simple scripts dan gradually build complexity.

Ditulis oleh

Hendra Wijaya

Hanya hamba Allah Ta'ala yang berusaha berbuat baik..

Tinggalkan Komentar

Email tidak akan ditampilkan.