0% found this document useful (0 votes)
12 views14 pages

Writing Simple Scripts

Uploaded by

minhgoofleman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views14 pages

Writing Simple Scripts

Uploaded by

minhgoofleman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

WRITING SIMPLE SCRIPTS

What is Shell Script ?

• Shells are interactive

• Sequence of 'n' number of commands

• Store this sequence of command to text


file – Shell script
Why to Write Shell Script ?
• Shell script can take input from user, file
and output them on screen.
• Useful to create our own commands.
• Save lots of time.
• To automate some task of day today life.
• System Administration part can be also
automated.
Writing Simple Scripts
• Starting a shell script
#!/bin/bash
# This is a simple script that can be used to display
# my documents in a folder, while pausing the output.
ls -al *.doc | more
ls -al *.$1 | more

• Writing a basic script


– chmod u+x script_name
– Path_to_scripts
– bash script_name
– . Script_name
Writing Simple Scripts
• Commands
+ grep
+ find
+ cut
+ sed
+ echo
+ mail
• Using variable
$0, $1, …. $9
Variable=value
Variable=`command`
ip=`route -n | grep UG | tr -s “ “ | cut -f 2 -d “ “`
Writing Simple Scripts
• Reading user input
Read var1 var2 var3
debian:~# read first last
lee johnson
debian:~# echo $first
lee
debian:~# echo $last
johnson
Writing Simple Scripts
• Testing conditions
test -flag string = string
test -flag string != string
[ -flag file/variable/value ]
• Flow control – The case statement
– The if/else statement case string in
if condition pattern1 )
then statements ;;
statement(s) pattern2 )
[elif condition statements ;;
then pattern3 )
statement(s)] statements ;;
[else ...
statement(s)] esac
fi
– The while and until statements
– The for loop while condition
for item [in list] do
do statement(s)
statements done
Done
Writing Simple Scripts
Writing Simple Scripts
Writing Simple Scripts
• Condition
– Example:
• if [ -f regularfile ]; then
• If test –f regularfile; then
– Always keep spaces between the brackets
and the actual check/comparison
– Always terminate the line before putting a new
keyword like "then“
– You can invert a condition by putting an "!" in
front of it
• if [ ! -f regularfile ]; then
Writing Simple Scripts
• Different condition syntaxes
– 1. Single-bracket syntax
• File-based conditions
– if [ -L symboliclink ]; then
• String-based conditions
– if [ -z "$emptystring" ]; then
– if [ "$stringvar1" == "cheese" ]; then
• Arithmetic (number-based) conditions
– if [ $num -lt 1 ]; then
– 2. Double-bracket syntax
• if [[ "$stringvar" == *[sS]tring* ]]; then
• if [[ $stringvarwithspaces != foo ]]; then
• if [[ $num -eq 3 && "$stringvar" == foo ]]; then
Writing Simple Scripts
• Different condition syntaxes
– 3. Double-parenthesis syntax
• There also is another syntax for arithmetic
(number-based) conditions, most likely adopted
from the Korn shell:
– if (( $num <= 5 )); then
• It supports the "&&" and "||" combining expressions
• It not support the -a and -o ones
Writing Simple Scripts
• Example1: Nhập vào một số và kiểm tra số âm hay dương
#!/bin/bash
#This is a simple script that can be used to demontration
if [ $1 -ge 0 ]; then
echo "$1 la so duong"
else
echo "$1 la so am"
Fi
• Example2: Kiểm tra dung lượng sử dụng của người dùng trong thư
mục home của họ và đưa ra cảnh báo nếu vượt ngưỡng cho phép
#!/bin/bash
quota=10
for i in $( ls /home ); do
check=`du -sm /home/$i | cut -f 1`
If [ $check –gt $quota ]; then
echo "user $i nhieu du lieu qua"
fi
done
Writing Simple Scripts
• Example3: Viết ra màn hình các ký tự như sau: (số nhập từ bàn phím)
• 1
• 22 #!/bin/bash
echo -n "Enter Number between (5 to 9) : "
• 333 read number
• 4444 for (( i=1; i<=number; i++ ))
• 55555 do
for (( s=number; s>=i; s-- ))
• 666666 do
echo -n " "
done
for (( j=1; j<=i; j++ ))
do
echo -n " $i"
done
echo ""
done

You might also like