0% menganggap dokumen ini bermanfaat (0 suara)
89 tayangan19 halaman

Memahami Shell dan Shell Script

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
Kami menangani hak cipta konten dengan serius. Jika Anda merasa konten ini milik Anda, ajukan klaim di sini.
Format Tersedia
Unduh sebagai PDF, TXT atau baca online di Scribd
0% menganggap dokumen ini bermanfaat (0 suara)
89 tayangan19 halaman

Memahami Shell dan Shell Script

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
Kami menangani hak cipta konten dengan serius. Jika Anda merasa konten ini milik Anda, ajukan klaim di sini.
Format Tersedia
Unduh sebagai PDF, TXT atau baca online di Scribd

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

Common questions

Didukung oleh AI

String comparison operators such as ‘-z’, ‘-n’, ‘=’, and ‘!=’ are used to evaluate string conditions in shell scripts. ‘-z’ checks if a string length is zero, while ‘-n’ checks if it’s non-zero. Operators ‘=’ and ‘!=’ determine if strings are equal or not. These are crucial for controlling script logic based on string values .

To compute the factorial of a number using a shell script, a 'while' loop is used. First, initialize the factorial value to 1 and a counter. Use 'while' to iterate over numbers from 1 to the given number, multiplying them to the running total using 'expr'. Increment the counter on each iteration until the number is reached. This results in the factorial being calculated and stored .

Multiple conditions in an 'if' statement can be integrated using logical operators like '-a' for 'AND' and '-o' for 'OR'. Parentheses can group conditions, while '!' negates a condition. Complex conditions enable advanced decision-making flows in scripts, allowing the execution of code based on multifaceted logic .

Boolean expressions and file operators in shell scripts allow for detailed decision-making processes. Relational operators like '-eq', '-ne', and '-ge' help compare numeric values, while file operators like '-f' and '-d' check for file existence and type. These expressions are used within control flow structures like 'if', enabling scripts to make logic-based decisions based on conditions .

Shell scripting supports control flow structures like 'if', 'while', and 'for'. 'if' is used to make conditional decisions, with 'if-then' and 'if-elif-else' statements handling various conditions. 'While' loops execute a block of commands as long as a condition holds true, and 'for' loops iterate over sets of values. These structures allow for complex logic to be implemented in shell scripts .

A shell in an operating system is fundamentally used as a command interpreter and a scripting language. This allows users to execute commands manually and automate tasks using scripts that can perform complex sequences of commands. Shells like Bash, Csh, Tcsh, Ksh, and Zsh are different types of command interpreters available for use .

Basic integer arithmetic in shell scripts can be performed using the 'expr' command. For instance, to add two numbers, you can use 'expr 1 + 1'. To store the result of a command in a variable, backquotes are used: 'i=`expr $i + 1`'. This allows arithmetic operations to be seamlessly integrated within shell scripts .

The 'read' command is used to read user input and assign it to a variable. For example, the command 'read name' will store input into the variable 'name'. This input can then be used within the script by referencing the variable. Additionally, multiple inputs can be read at once and stored in respective variables within a script .

In shell programming, variables are defined by assigning values using the '=' operator without spaces, such as 'number=100'. Variable names can consist of letters, numbers, and underscores but must start with a letter. All values are stored as strings. Variables can be utilized by referencing them with a '$', such as '$number' .

To execute a shell script, it must first have execute permissions, which can be set using the command 'chmod u+rx scriptname'. Scripts are typically executed by calling their path using './scriptname'. The first line of the script should specify the shell, such as '#!/bin/sh', to indicate the script interpreter .

Anda mungkin juga menyukai