Kerenin Bash Script! Mastering curl
and wget
dengan Gaya 😎
Sebagai developer atau sysadmin, pasti sering banget ngebut dengan command line. Dua tools keren yang bisa bikin hidup lo lebih mudah adalah curl
dan wget
. Nih gue kasih tahu cara pake keduanya di Bash script biar kerja lo lebih cepat dan keren!
1. curl
: The Cool Way to Transfer Data 🚀
curl
tuh keren banget buat komunikasi sama web servers. Lo bisa nge-download file, ngirim request HTTP, dan masih banyak lagi.
Contoh Gampang:
#!/bin/bash
# Nge-download file pake curl
curl -O https://example.com/file.zip
Tuh, tinggal pake -O
, terus ikutin URL, file langsung didownload ke direktori sekarang.
Nge-download dengan Nama Kustom:
Kalo mau namanya beda, pake -o
.
#!/bin/bash
curl -o myfile.zip https://example.com/file.zip
Nambahin Headers:
Biar lebih tajam, lo bisa tambahin headers pake -H
.
#!/bin/bash
curl -H "Authorization: Bearer YOUR_TOKEN" https://api.example.com/data
2. wget
: The Wajib Pake Tool buat Downloader 🔥
wget
lebih gampang buat nge-download file sama mirroring website. Enaknya, wget
bisa nge-resume download kalo tiba-tiba putus.
Contoh Dasar:
#!/bin/bash
# Download file pake wget
wget https://example.com/file.zip
Download dan Save dengan Nama Berbeda:
#!/bin/bash
wget -O myfile.zip https://example.com/file.zip
Download dalam Mode Silent:
Ga mau keliatan ribet? Coba -q
biar ga keliatan verbose.
#!/bin/bash
wget -q https://example.com/file.zip
3. Contoh Lebih Kompleks: Script utk Download Banyak File 🚀
Ini nih script bash yang lebih tajam, bisa nge-download banyak file sekaligus!
#!/bin/bash
# Array of file URLs
files=(
"https://example.com/file1.zip"
"https://example.com/file2.zip"
"https://example.com/file3.zip"
)
# Loop untuk download semua file
for file in "${files[@]}"; do
wget "$file" || echo "Failed to download $file"
done
Script di atas bakal nge-download semua file dalam array. Kalo ada yang gagal, bakal ada pesan error.
4. Integrasi dengan curl
untuk Cek HTTP Status 🕵️♂️
Kadang kita perlu ngecek status halaman web. Pake curl
, gampang!
#!/bin/bash
status_code=$(curl -s -o /dev/null -w "%{http_code}" https://example.com)
if [[ "$status_code" -eq 200 ]]; then
echo "Website is OK!"
else
echo "Website returned HTTP $status_code"
fi
Nah, itu dia cara keren pake curl
dan wget
di Bash script. Dengan dua tools jagoan ini, lo bisa otomatisasi banyak banget tugas. Jadi, jangan ragu buat eksplor! Semoga artikel ini membantu lo jadi lebih jago di command line. Stay cool! 😎🚀