0% menganggap dokumen ini bermanfaat (0 suara)
209 tayangan

Pemrograman Shell

Shell adalah bahasa skrip yang digunakan untuk menjalankan perintah dan otomatisasi tugas. Shell dapat digunakan sebagai interpreter perintah dan bahasa skrip. Jenis shell umum meliputi Bash, Csh, dan Ksh. Shell skrip menggunakan sintaks khusus untuk menjalankan perintah berulang, mengontrol aliran, dan menangani input pengguna."

Diunggah oleh

Farhan Syamsuddin
Hak Cipta
© © All Rights Reserved
Format Tersedia
Unduh sebagai PDF, TXT atau baca online di Scribd
0% menganggap dokumen ini bermanfaat (0 suara)
209 tayangan

Pemrograman Shell

Shell adalah bahasa skrip yang digunakan untuk menjalankan perintah dan otomatisasi tugas. Shell dapat digunakan sebagai interpreter perintah dan bahasa skrip. Jenis shell umum meliputi Bash, Csh, dan Ksh. Shell skrip menggunakan sintaks khusus untuk menjalankan perintah berulang, mengontrol aliran, dan menangani input pengguna."

Diunggah oleh

Farhan Syamsuddin
Hak Cipta
© © All Rights Reserved
Format Tersedia
Unduh sebagai PDF, TXT atau baca online di Scribd
Anda di halaman 1/ 19

Pemrograman Shell

Apa itu Shell ?


INPUT

shell

OUTPUT ERROR
Shell
Shell dapat digunakan sebagai
• command interpreter
• scripting language
Jenis-jenis Shell
• Bash
• Csh
• Tcsh
• Ksh
• Zsh
Shell Scripts
Shell script merupakan sebuah file yang memuat perintah-perintah
shell dengan beberapa perintah dan syntax tambahan

• Baris pertama memuat informasi interpreter dalam hal ini shell Bash:
#!/bin/sh

• Sebuah shell script harus dapat dibaca dan dieksekusi .


chmod u+rx scriptname

• Sebagaimana perintah lainnya, sebuah shell script dijalankan dengan


memanggil pathnya.
“./scriptname” bukan “scriptname”
Contoh Shell Script
• “hello world” shell script:

$ cat hello
#!/bin/sh
echo "Hello world"
$ ./hello
Hello world
$

• echo perintah untuk mencetak string pada standard output (shell)


Variabel Shell
• Nama variable dapat berupa sembarang urutan huruf, angka
dan underscore, tetapi harus diawali dengan huruf.

• Memberikan nilai pada variabel: (tanpa spasi sebelum dan


sesudah operator assignment “=“)
number=100
name=“Budi"
• Seluruh nilai disimpan dalam bentuk string
User Input
• Perintah read digunakan untuk membaca input dan menyimpannya pada variabel.
#!/bin/sh
echo "Enter name: "
read name
echo "How many friends do you have? "
read number
echo "$name has $number friends!“
---------------------------------------------
#!/bin/sh
echo "Enter name and how many friends:"
read name number
echo "$name has $number friends!"
expr
• Expr digunakan untuk melakukan perhitungan integer sederhana .
$ i=1
$ expr $i + 1
2

• Untuk memasukan output dari sembarang perintah shell ke dalam


variable digunakan backquotes `__`:
$ i=1
$ i=`expr $i + 1`
$ echo "$i"
2
Control Flow
Shell mendukung beberapa statement control
flow:
• if
• while
• for
if
#!/bin/sh
user=`whoami`
if [ $user = “ini" ]
then
echo "Hi Budi!"
fi

Harus ada sepasi antara brackets [ ] dan pernyataan logika


if then else
#!/bin/sh
user=`whoami`
if [ $user = “ini" ]
then
echo "Hi Budi!"
else
echo "Hi $user!"
fi
if elif else
#!/bin/sh
users=`who | wc -l`
if [ $users -ge 4 ]
then
echo "Heavy load"
elif [ $users -gt 1 ]
then
echo "Medium load"
else
echo "Just me!"
fi
Boolean Expressions
• Relational operators:
-eq, -ne, -gt, -ge, -lt, -le

• File operators:
-f fileTrue if file exists and is not a directory
-d fileTrue if file exists and is a directory
-s fileTrue if file exists and has a size > 0

• String operators:
-z string True if the length of string is zero
-n string True if the length of string is nonzero
s1 = s2 True if s1 and s2 are the same
s1 != s2 True if s1 and s2 are different
s1 True if s1 is not the null string
File Operator Example
#!/bin/sh
if [ -f letter1 ]
then
echo "We have found the evidence!"
cat letter1
else
echo "Keep looking!"
fi
And, Or, Not
-a And
-o Or
! Not

#!/bin/sh
if [ `who | grep gates | wc -l` -ge 1 -a `whoami` != “gates" ]
then
echo "Bill is loading down the machine!"
else
echo "All is well!"
fi
while
#!/bin/sh
resp="no"
while [ $resp != "yes" ]
do
echo "Wakeup [yes/no]?"
read resp
done
while
#!/bin/sh
echo "Enter number: "
read n
fac=1
i=1
while [ $i -le $n ]
do
fac=`expr $fac \* $i`
i=`expr $i + 1`
done
echo "The factorial of $n is $fac"
For
#!/bin/sh
for i in 1 2 3 4 5
do
echo "Looping ... number $i"
done

Anda mungkin juga menyukai