Unix / Linux - Shell Basic Operators

Hello, aspiring programmers! I'm thrilled to guide you through the fascinating world of Unix/Linux shell operators. As someone who's been teaching computer science for over a decade, I can assure you that mastering these operators will be a game-changer in your programming journey. Let's dive in!

Unix / Linux - Basic Operators

Arithmetic Operators

Arithmetic operators are the building blocks of mathematical operations in shell scripting. They allow us to perform basic calculations within our scripts.

Here's a table of the most common arithmetic operators:

Operator Description Example
+ Addition expr $a + $b
- Subtraction expr $a - $b
* Multiplication expr $a \* $b
/ Division expr $a / $b
% Modulus expr $a % $b
= Assignment a=$b

Let's look at a practical example:

#!/bin/bash
a=10
b=5

echo "Addition: `expr $a + $b`"
echo "Subtraction: `expr $a - $b`"
echo "Multiplication: `expr $a \* $b`"
echo "Division: `expr $a / $b`"
echo "Modulus: `expr $a % $b`"

In this script, we're using the expr command to perform arithmetic operations. The backticks () are used to execute theexpr` command and substitute its output.

When you run this script, you'll see:

Addition: 15
Subtraction: 5
Multiplication: 50
Division: 2
Modulus: 0

Remember, in shell scripting, spaces are crucial. expr$a+$b won't work, but expr $a + $b will. It's like giving your variables room to breathe!

Relational Operators

Relational operators are used to compare values. They're essential for creating conditions in your scripts.

Here's a table of relational operators:

Operator Description Example
-eq Equal to [ $a -eq $b ]
-ne Not equal to [ $a -ne $b ]
-gt Greater than [ $a -gt $b ]
-lt Less than [ $a -lt $b ]
-ge Greater than or equal to [ $a -ge $b ]
-le Less than or equal to [ $a -le $b ]

Let's see these in action:

#!/bin/bash
a=10
b=20

if [ $a -eq $b ]
then
echo "$a is equal to $b"
elif [ $a -gt $b ]
then
echo "$a is greater than $b"
else
echo "$a is less than $b"
fi

This script compares a and b using relational operators. When you run it, you'll see:

10 is less than 20

Pro tip: Think of these operators as asking questions. "-eq" is like asking "Are these equal?", "-gt" is asking "Is this greater than that?". It helps to verbalize these comparisons when you're writing your scripts.

Boolean Operators

Boolean operators allow us to combine multiple conditions. They're like the logical glue of your scripts.

Here's a table of boolean operators:

Operator Description Example
! NOT [ ! false ]
-o OR [ $a -lt 20 -o $b -gt 100 ]
-a AND [ $a -lt 20 -a $b -gt 100 ]

Let's use these in a script:

#!/bin/bash
a=10
b=20

if [ $a -lt 20 -a $b -gt 15 ]
then
echo "Both conditions are true"
else
echo "At least one condition is false"
fi

When you run this script, you'll see:

Both conditions are true

I like to think of boolean operators as decision-making tools. They help your script make choices based on multiple factors, just like we do in real life!

String Operators

String operators are used to compare and manipulate text strings. They're incredibly useful when working with text data.

Here's a table of string operators:

Operator Description Example
= Equal to [ $a = $b ]
!= Not equal to [ $a != $b ]
-z String is null [ -z $a ]
-n String is not null [ -n $a ]
str String is not empty [ $a ]

Let's see these in action:

#!/bin/bash
a="Hello"
b="World"

if [ $a = $b ]
then
echo "Strings are equal"
elif [ -z $a ]
then
echo "String a is empty"
elif [ -n $b ]
then
echo "String b is not empty"
else
echo "None of the above conditions are true"
fi

When you run this script, you'll get:

String b is not empty

String operators are like the grammar rules of shell scripting. They help you make sense of the text your script is working with.

File Test Operators

File test operators are used to test different file conditions. They're crucial when your script needs to interact with files and directories.

Here's a table of some common file test operators:

Operator Description Example
-d file Directory exists [ -d $file ]
-f file File exists and is a regular file [ -f $file ]
-r file File is readable [ -r $file ]
-w file File is writable [ -w $file ]
-x file File is executable [ -x $file ]
-s file File is not zero size [ -s $file ]

Let's use these in a script:

#!/bin/bash
file="/etc/passwd"

if [ -f $file ]
then
echo "File exists"
if [ -r $file ]
then
echo "File is readable"
fi
if [ -w $file ]
then
echo "File is writable"
fi
if [ -x $file ]
then
echo "File is executable"
fi
else
echo "File does not exist"
fi

When you run this script, you might see something like:

File exists
File is readable

File test operators are like your script's file detective tools. They help your script understand what it can and can't do with different files.

C Shell Operators

The C Shell (csh) has its own set of operators that are slightly different from the Bourne Shell. Here are some examples:

#!/bin/csh
set a = 10
set b = 20

if ($a == $b) then
echo "a is equal to b"
else if ($a != $b) then
echo "a is not equal to b"
endif

if ($a > $b) then
echo "a is greater than b"
else if ($a < $b) then
echo "a is less than b"
endif

In C Shell, we use == for equality and != for inequality. Also, we can directly use > and < for comparisons.

Korn Shell Operators

The Korn Shell (ksh) combines features from both the Bourne Shell and the C Shell. Here's an example:

#!/bin/ksh
a=10
b=20

if ((a == b))
then
print "a is equal to b"
elif ((a != b))
then
print "a is not equal to b"
fi

if ((a > b))
then
print "a is greater than b"
elif ((a < b))
then
print "a is less than b"
fi

In Korn Shell, we can use (( and )) for arithmetic operations and comparisons, which makes the syntax cleaner and more intuitive.

Remember, practice makes perfect! Don't be afraid to experiment with these operators in your own scripts. Each time you use them, you'll become more comfortable and proficient. Happy scripting!


Unix / Linux - Shell Basic Operators

Halo, para pemrogram yang sedang belajar! Saya sangat gembira untuk memandu Anda melalui dunia menarik operator shell Unix/Linux. Sebagai seseorang yang telah mengajar ilmu komputer selama lebih dari satu dekade, saya dapat menjamin Anda bahwa memahami operator ini akan menjadi perubahan besar dalam perjalanan pemrograman Anda. Ayo masuk ke dalamnya!

Operator Aritmetik

Operator aritmetik adalah fondasi operasi matematika dalam skrip shell. Mereka memungkinkan kita untuk melakukan perhitungan dasar dalam skrip kita.

Berikut adalah tabel operator aritmetik yang paling umum:

Operator Deskripsi Contoh
+ Penjumlahan expr $a + $b
- Pengurangan expr $a - $b
* Perkalian expr $a \* $b
/ Pembagian expr $a / $b
% Modulus expr $a % $b
= Penugasan a=$b

Ayo lihat contoh praktis:

#!/bin/bash
a=10
b=5

echo "Penjumlahan: `expr $a + $b`"
echo "Pengurangan: `expr $a - $b`"
echo "Perkalian: `expr $a \* $b`"
echo "Pembagian: `expr $a / $b`"
echo "Modulus: `expr $a % $b`"

Dalam skrip ini, kita menggunakan perintah expr untuk melakukan operasi aritmetik. Backticks () digunakan untuk menjalankan perintahexpr` dan menggantikan outputnya.

Ketika Anda menjalankan skrip ini, Anda akan melihat:

Penjumlahan: 15
Pengurangan: 5
Perkalian: 50
Pembagian: 2
Modulus: 0

Ingat, dalam skrip shell, spasi sangat penting. expr$a+$b tidak akan bekerja, tetapi expr $a + $b akan. Itu seperti memberikan ruang bagi variabel Anda untuk "bernafas"!

Operator Relasional

Operator relasional digunakan untuk membandingkan nilai. Mereka penting untuk membuat kondisi dalam skrip Anda.

Berikut adalah tabel operator relasional:

Operator Deskripsi Contoh
-eq Sama dengan [ $a -eq $b ]
-ne Tidak sama dengan [ $a -ne $b ]
-gt Lebih besar dari [ $a -gt $b ]
-lt Lebih kecil dari [ $a -lt $b ]
-ge Lebih besar atau sama dengan [ $a -ge $b ]
-le Lebih kecil atau sama dengan [ $a -le $b ]

Ayo lihat contohnya dalam skrip:

#!/bin/bash
a=10
b=20

if [ $a -eq $b ]
then
echo "$a is equal to $b"
elif [ $a -gt $b ]
then
echo "$a is greater than $b"
else
echo "$a is less than $b"
fi

Skrip ini membandingkan a dan b menggunakan operator relasional. Ketika Anda menjalankan itu, Anda akan melihat:

10 is less than 20

Tips profesional: PERTANYAANLAH operator ini. "-eq" seperti menanyakan "Apakah ini sama?", "-gt" seperti menanyakan "Apakah ini lebih besar dari itu?". Itu membantu untuk mengucapkan perbandingan ini saat Anda menulis skrip Anda.

Operator Boolean

Operator boolean memungkinkan kita untuk menggabungkan beberapa kondisi. Mereka seperti lem logis dari skrip Anda.

Berikut adalah tabel operator boolean:

Operator Deskripsi Contoh
! NOT [ ! false ]
-o OR [ $a -lt 20 -o $b -gt 100 ]
-a AND [ $a -lt 20 -a $b -gt 100 ]

Ayo gunakan ini dalam skrip:

#!/bin/bash
a=10
b=20

if [ $a -lt 20 -a $b -gt 15 ]
then
echo "Both conditions are true"
else
echo "At least one condition is false"
fi

Ketika Anda menjalankan skrip ini, Anda akan melihat:

Both conditions are true

Saya suka untuk menganggap operator boolean sebagai alat pengambilan keputusan. Mereka membantu skrip Anda membuat pilihan berdasarkan faktor ganda, seperti halnya kita melakukan dalam kehidupan nyata!

Operator String

Operator string digunakan untuk membandingkan dan memanipulasi string teks. Mereka sangat berguna saat bekerja dengan data teks.

Berikut adalah tabel operator string:

Operator Deskripsi Contoh
= Sama dengan [ $a = $b ]
!= Tidak sama dengan [ $a != $b ]
-z String kosong [ -z $a ]
-n String tidak kosong [ -n $a ]
str String tidak kosong [ $a ]

Ayo lihat contohnya dalam skrip:

#!/bin/bash
a="Hello"
b="World"

if [ $a = $b ]
then
echo "Strings are equal"
elif [ -z $a ]
then
echo "String a is empty"
elif [ -n $b ]
then
echo "String b is not empty"
else
echo "None of the above conditions are true"
fi

Ketika Anda menjalankan skrip ini, Anda akan mendapatkan:

String b is not empty

Operator string seperti aturan grammar dari skrip shell. Mereka membantu Anda memahami teks yang skrip Anda bekerja dengannya.

Operator Tes File

Operator tes file digunakan untuk menguji berbagai kondisi file. Mereka penting saat skrip Anda perlu berinteraksi dengan file dan direktori.

Berikut adalah tabel operator tes file yang umum:

Operator Deskripsi Contoh
-d file Direktori ada [ -d $file ]
-f file File ada dan adalah file biasa [ -f $file ]
-r file File dapat dibaca [ -r $file ]
-w file File dapat ditulis [ -w $file ]
-x file File dapat dieksekusi [ -x $file ]
-s file File bukan ukuran nol [ -s $file ]

Ayo gunakan ini dalam skrip:

#!/bin/bash
file="/etc/passwd"

if [ -f $file ]
then
echo "File exists"
if [ -r $file ]
then
echo "File is readable"
fi
if [ -w $file ]
then
echo "File is writable"
fi
if [ -x $file ]
then
echo "File is executable"
fi
else
echo "File does not exist"
fi

Ketika Anda menjalankan skrip ini, Anda mungkin akan melihat sesuatu seperti:

File exists
File is readable

Operator tes file seperti detektif file dari skrip Anda. Mereka membantu skrip Anda memahami apa yang dapat dan tidak dapat dilakukan dengan berbagai file.

Operator C Shell

C Shell (csh) memiliki set operator yang sedikit berbeda dari Shell Bourne. Berikut adalah beberapa contoh:

#!/bin/csh
set a = 10
set b = 20

if ($a == $b) then
echo "a is equal to b"
else if ($a != $b) then
echo "a is not equal to b"
endif

if ($a > $b) then
echo "a is greater than b"
else if ($a < $b) then
echo "a is less than b"
endif

Dalam C Shell, kita menggunakan == untuk kesetaraan dan != untuk perbedaan. Juga, kita dapat secara langsung menggunakan > dan < untuk perbandingan.

Operator Korn Shell

Korn Shell (ksh) menggabungkan fitur dari Shell Bourne dan C Shell. Berikut adalah contoh:


#!/bin/ksh
a=10
b=20

if ((a == b))
then
print "a is equal to b"
elif ((a != b))
then
print "a is not equal to b"
fi

if ((a > b))
then
print "a is greater than b"
elif ((a < b))
then
print "a is less than b"
fi

Credits: Image by storyset