Workshop
From Chania-LUG Wiki
Bash Shell as a Programming Language
- if, elif, for, for, while, until, case, select
- Conditions
- Variables (Μεταβλητές)
- Lists (Λίστες)
- Functions (Συναρτήσεις)
- Build-in commands
- Here Documents
Ας λερώσουμε τα χέρια μας (Level 1)
-Goals: #!, #, Commands, make executable, ', ", \.
#! /bin/bash # This is a silly script echo "hello, I'm your first script" echo "What do you think?"
Level 2
Ας γράψουμε ένα script που να δέχεται ένα χρήστη και να υπολογίζει το μέγεθος που έχει ο home του κατάλογος. -Goals: Variables, Special Variables, Conditions, if, test-[
#! /bin/bash # LGU v3 of course... # This script calculates and prints the User's home usage. if test $# -lt 1 #test --> [ then echo "You have to specify at least one user." exit #exit 1 fi cd /home/$1 SPACE=$(du -s | cut -f 1) echo "$1 is using $SPACE kilobytes." exit # exit 0
Level 3
Ας το τροποποιήσουμε ώστε να δέχεται παραπάνω από ένα χρήστη.
-Goals: exit status, loops - for, stdin(out) vs exit
#! /bin/bash # LGU v3 of course... # This a second script calculates and prints the User's home usage. if [ $# -lt 1 ] then echo "You have to specify at least one user." exit 1 fi for SOMEONE in $@ do cd /home/$SOMEONE SPACE=$(du -s | cut -f 1) # du -s 2>/dev/null echo "$SOMEONE is using $SPACE kilobytes." done exit 0
Level 4
Ας το κάνουμε μόνο για τον root και χωρίς να παίρνει παραμέτρους.
-Goals: more in to loops, lists, blocks, check exit status
#! /bin/bash
# LGU v3 of course...
# This a third script calculates and prints the User's home usage.
# check if /etc/passwd exists (hahahaha)
[ ! -f /etc/passwd ] && { echo "Ouaou!!!!"
exit 1
}
if [ "$(id -u)" != "0" ] # root login check
then
echo "Go try to login as root and come again!!!"
exit 1
fi
for SOMEONE in $(cat /etc/passwd | grep '/bin/bash' | cut -d ':' -f 1) #Φυσικά κάτω από ARG_MAX
do
cd /home/$SOMEONE
if [ $? -ne 0 ]
then
echo "Can not read $SOMEONE\'s home directory"
continue
fi
SPACE=$(du -s /home/$SOMEONE 2>/dev/null | cut -f 1) #περιττό 2>/dev/null
echo "$SOMEONE is using $SPACE kilobytes."
done
exit 0
Level 5
Και τώρα πια που μάθαμε και κάτι, ας του βάλουμε και παραμέτρους
#! /bin/bash
# LGU v3 of course...
# This a third script calculates and prints the User's home usage.
SEARCH_USERS="/tmp/tmpfile"
true > "$SEARCH_USERS"
# check if /etc/passwd exists (hahahaha)
[ ! -f /etc/passwd ] && { echo "Ouaou!!!!"; exit 1; }
if [ "$(id -u)" != "0" ] # root login check
then
echo "Go try to login as root and come again!!!"
exit 1
fi
if [ -z "$1" ]
then
echo "Error. Type $0 --help"
exit 1
fi
while [ -n "$1" ] # [ ! -z xxx ]
do
case "$1" in
-a)
cat /etc/passwd | grep '/bin/bash' | cut -d ':' -f 1 > "$SEARCH_USERS"
shift
;;
-u)
echo "$2" >> "$SEARCH_USERS"
shift; shift
;;
*)
echo "Error. Type $0 --help"
exit 1
;;
esac
done
IFS=$'\n'
for SOMEONE in $(cat $SEARCH_USERS) #Φυσικά κάτω από ARG_MAX
do
cd /home/$SOMEONE 2>/dev/null
if [ $? -ne 0 ]
then
echo "Can not read $SOMEONE's home directory"
continue
fi
SPACE=$(du -s /home/$SOMEONE 2>/dev/null | cut -f 1) #περιττό 2>/dev/null
echo "$SOMEONE is using $SPACE kilobytes."
done
rm -f "$SEARCH_USERS"
exit 0
Level 6 - Are you nuts?
Πάμε να βάλουμε συναρτήσεις και SIGNALS? Αχά...
#! /usr/bin/env bash
# LGU v3 of course...
# This a third script calculates and prints the User's home usage.
SEARCH_USERS="/tmp/tmpfile"
#-------------------------------------
# initialization routine
init ()
{
true > "$SEARCH_USERS"
# check if /etc/passwd exists (hahahaha)
[ ! -f /etc/passwd ] && quit "Ouaou!!!!" 1
# root login check
[ "$(id -u)" != "0" ] && quit "Go try to login as root and come again!!!" 1
IFS=$'\n'
}
#--------------------------------------
# usage:
# quit "message" "exit status"
quit ()
{
echo "$1"
rm -f "$SEARCH_USERS"
exit "$2"
}
#--------------------------------------
# Usage:
# par_parser $@
par_parser ()
{
[ -z "$1" ] && quit "Error. Type $0 --help" 1
while [ -n "$1" ] # [ ! -z xxx ]
do
case "$1" in
-a)
cat /etc/passwd | grep '/bin/bash' | cut -d ':' -f 1 > "$SEARCH_USERS"
shift
;;
-u)
echo "$2" >> "$SEARCH_USERS"
shift; shift
;;
*)
quit "Error. Type $0 --help" 1
;;
esac
done
}
#-----------------------------------------------------------
# main routine
trap 'quit "Process terminated..." 0' SIGINT SIGTERM SIGHUP
init
par_parser $@
for SOMEONE in $(cat $SEARCH_USERS) #Φυσικά κάτω από ARG_MAX διορθώνεται στο επόμενο level.
do
cd /home/$SOMEONE 2>/dev/null #Είπαμε, αυτό
if [ $? -ne 0 ]
then
echo "Can not read $SOMEONE's home directory"
continue
fi
SPACE=$(du -s /home/$SOMEONE 2>/dev/null | cut -f 1) #ή αυτό
echo "$SOMEONE is using $SPACE kilobytes."
done
quit "" 0