Pemrograman Shell
Pemrograman Shell
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
$ cat hello
#!/bin/sh
echo "Hello world"
$ ./hello
Hello world
$
• 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