Backbox Linux
  • Backbox Indonesia
  • Get's Started
    • System Requirements
    • Download Images
    • Installation
    • 5 Things To Do Right After Installing BackBox
  • Basic Linux
    • Mengenal GNU/Linux
    • Kenapa Linux?
    • Linux Desktop vs Linux Server
    • Distro Linux
    • Struktur Direktori GNU/Linux
    • Linux File System
  • Basic Command
    • Introduction
      • Terminal
      • Accessing Linux
      • Network Command
      • Important to remember of Linux
      • Change Password
    • Fundamental
      • File System Navigation Commands
      • TAB Completion and Up Arrow
      • Linux Types File
      • Linux Command Syntax
      • Help Commands
      • Creating Files & Directories
      • Files and Directories Permission
      • Files Ownership
      • Access Control List
      • Soft and Hard Links
      • Find Files and Directories
      • Files Maintenance
      • Wildcards
      • File Display Commands
      • Adding Text to Files
      • Pipeline (|)
      • Standard Output to a File (tee)
      • Filters / Text Processors Commands
      • Compare Files
      • Compress and Uncompress File
      • Truncate File Size
      • Combining and Splitting Files
      • Linux vs Windows Commands
    • System Administration
      • Linux File Editor (vi)
      • sed - Command
      • User Account Management
      • Switch Users and Sudo Access
      • Monitor Users
      • Talking to Users
      • System Utility Commands
      • Process and Jobs
      • Addtional cronjobs
      • Process Management
      • System Monitoring Commands
      • System Logs Monitor
      • System Maintenance Commands
      • Changing System Hostname
      • Finding System Information
      • Finding System Architecture
      • Terminal Commands
      • Terminal Control Keys
      • Recover Root Password
      • SOS Report
  • Bash Scripting
    • Environment Variables
    • Apa itu Shell?
    • Basic Shell Scripting
    • Input/Output
    • Percabangan (If)
    • Perulangan (For)
    • Perulangan (While)
    • Percabangan (Case)
    • Fungsi
    • Alias
    • Shell History
  • Networking
    • Untitled
  • Disk Management
    • Untitled
Powered by GitBook
On this page
  • ./Contoh-1
  • ./Contoh-2
  • ./Contoh-3
  • ./Contoh-4
  • ./Contoh-5

Was this helpful?

  1. Bash Scripting

Perulangan (For)

PreviousPercabangan (If)NextPerulangan (While)

Last updated 4 years ago

Was this helpful?

Perulangan (looping) adalah suatu bentuk kegiatan mengulang suatu statement sampai batas yang diinginkan.

./Contoh-1

Program berikut akan melakukan perulangan sebanyak 5 kali dan menampilkan data 1, 2, 3, 4 dan 5 yang disimpan ke dalam variable i secara berurut.

perulangan-for.sh
#!/bin/bash

for i in 1 2 3 4 5
do
    echo "Tepuk $i kali."
done

./Contoh-2

Program berikut akan melakukan perulangan sebanyak 4 kali dan menampilkan data makan, tidur, main dan belajar yang disimpan ke dalam variable i secara berurut.

perulangan-for.sh
#!/bin/bash

for i in makan tidur main belajar
do
    echo "Siti sedang $i"
done

./Contoh-3

Program berikut akan membuat file bernama file-1, file-2, file-3, ... , hingga file-5.

perulangan-for.sh
#!/bin/bash

for i in {1..5}
do
    touch file-$i
done

./Contoh-4

Program berikut akan menghapus file bernama file-1, file-2, file-3, ... , hingga file-5.

perulangan-for.sh
#!/bin/bash

for i in {1..5}
do
    rm file-$i
done

./Contoh-5

Program berikut akan menampilkan user yang tersedia pada file /etc/passwd.

perulangan-for.sh
#!/bin/bash

i=1
for username in `awk -F: '{print $1}' /etc/passwd`
do
    echo "Username $((i++)): $username"
done