Shkrimi i skripteve ne Bash Shell
Disa rregulla te pergjithshme
1- Skripti duhet te filloje me
#!/bin/bash
ne menyre qe te percaktoje cilin shell do te perdorim per skriptin.
2- Komentet dhe komandat zgjasin vetem nje rresht
3- Komentet fillojne me #
4- Komandat mund te vendosen ne kllapa
Shembull i nje skripti
#!/bin/bash
# ky skript realizon nje pershendetjee
echo pershendetje nga skripti
Eshte e mundur qe te krijojme nje variabel ne skript dhe ti japim atij nje vlere, per shembull
Sintaksa
variable=$(shprehja qe i jep vlere vairablit)
psh
date_formatted=$(date +%m_%d_%y-%H.%M.%S)
Skripti me poshte
#!/bin/bash
#this will copy a file, appending the date and time
# to the end of the file name
date_formatted=$(date +%m_%d_%y-%H.%M.%S)
echo "Koha e formatuar eshte:" $date_formatted
do te shfaqte
student@ubuntu:~/bin$ sudo ./datecp
Koha e formatuar eshte: 04_08_13-18.24.15
Ne shembullin me poshte do te ndertojme nje skript qe merr si argument nje skedar dhe nje emer te ri per te. Skripti do te kopjoje skederin 1 me emrin skedar 2 dhe do ti shtoje emrit te ketij te fundit kohen ku u krye veprimi si nje prapashtese
#!/bin/bash
#this will copy a file, appending the date and time
# to the end of the file name
date_formatted=$(date +%m_%d_%y-%H.%M.%S)
echo "Koha e formatuar eshte:" $date_formatted
cp -i -v $1 $2.$date_formatted
Skripti duhet therritur me dy argumentat ($1 dhe $2)
student@ubuntu:~/bin$ sudo ./datecp datecp datecp
Koha e formatuar eshte: 04_08_13-18.35.42
`datecp' -> `datecp.04_08_13-18.35.42'
student@ubuntu:~/bin$ ls
datecp datecp.04_08_13-18.35.42 soft.04_08_13-18.27.24
student@ubuntu:~/bin$
Komanda cp kopjon nje skedar.
Opsioni -i = interactive , nese skedari i ri ekziston na pyet nese duam te mbivendosim.
Opsioni -v = verbose, sqaron veprimin qe po kryhet
Shembulli me poshte ilustron leximin e nje skedari rreshte per rresht dhe afishimin e tij duke shtuar nje numerues ne fillim te rreshtit
#!/bin/sh
fname=ict
# exec<$fname >$1
value=0
while read line
do
value=`expr $value + 1`;
echo $value $line;
done < $fname
echo "****$value";
Skriptet
site_template
#!/bin/bash
ip_address=192.168.1.104
echo shkruani emrin e domain
read domain
echo ************Duke shtuar domain Tuaj***************
echo " DO te shtohet gjithashtu edhe domain www.$domain"
echo ********************Duke shtuar zonen DNS*************************
./dns_config $domain
echo Eshte e nevojshme te restartohet webserveri,
echo ju lutem prisni......
service bind9 restart
echo **************DNS u shtua****************************
echo **************Duke shtuar perdoruesin****************************
echo shkruani emrin e perdoruesit qe do te jete administrator i ketij website
read theuser
echo Duke shtuar perdoruesin, mund te jete e nevojshme te jepni disa te dhena per perdoruesin
adduser $theuser
echo Perdoruesi u shtua sipas informacioneve te dhena nga ju.
echo
echo
echo **************Perdoruesi u shtua****************************
echo ***************Duke shtuar website******************************
./web_config $domain $theuser
echo Eshte e nevojshme te restartohet webserveri,
echo ju lutem prisni......
service apache2 restart
./webindex $domain $theuser
echo **************website u shtua****************************
dns_config
#!/bin/bash
domain=$1
ip_address=192.168.1.104
zonefile="db.$domain"
echo "zone '$domain' {" >> named.conf.local
echo " type master; " >> named.conf.local
echo " file '/etc/bind/db.$domain';" >>named.conf.local
echo "};"
exec > $zonefile
echo "$TTL 86400 "
echo "@ IN SOA voyager.$domain. admin.$domain. ("
echo " 1 ; Serial Number "
echo " 3h ; Refresh after 3 hours"
echo " 1h ; Retry after 1 hour"
echo " 1w ; Expire after 1 week"
echo " 1h ) ; Negative caching TTL of 1 hour"
echo "@ IN A $ip_address"
echo "; Name Servers defined here"
echo "$domain. IN NS voyager.$domain."
echo "; Mail Exchange servers defined here"
echo "$domain. IN MX 10 voyager.$domain."
echo "$domain. IN MX 10 mail.$domain."
echo "; Host Addresses Defined Here"
echo "voyager.$domain. IN A 192.168.1.104"
echo "mail.$domain. IN A 192.168.1.104"
echo "; Aliases"
echo "www.$domain. IN CNAME voyager.$domain."
echo "email.$domain. IN CNAME voyager.$domain."
echo "db.$domain. IN CNAME voyager.$domain."
web_config
#!/bin/bash
ip_address=192.168.1.104
domain=$1
theuser=$2
website="web.$domain"
# from here on I will redirect the execution output to another file
exec > $website
echo "<VirtualHost *:80>"
echo ServerAdmin webmaster@localhost
echo "ServerName $domain"
echo ServerAlias www.$domain
echo
echo "DocumentRoot /home/$theuser/web/"
echo "<Directory />"
echo Options FollowSymLinks
echo AllowOverride None
echo "</Directory>"
echo "<Directory /home/$theuser/web/>"
echo Options Indexes FollowSymLinks MultiViews
echo AllowOverride None
echo Order allow,deny
echo allow from all
echo "</Directory>"
echo
echo ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
echo '<Directory "/usr/lib/cgi-bin">'
echo AllowOverride None
echo Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
echo Order allow,deny
echo Allow from all
echo "</Directory>"
echo
echo ErrorLog ${APACHE_LOG_DIR}/error.log
echo
echo # Possible values include: debug, info, notice, warn, error, crit,
echo # alert, emerg.
echo LogLevel warn
echo
echo CustomLog ${APACHE_LOG_DIR}/access.log combined
echo
echo 'Alias /doc/ "/usr/share/doc/"'
echo '<Directory "/usr/share/doc/">'
echo Options Indexes MultiViews FollowSymLinks
echo AllowOverride None
echo Order deny,allow
echo Deny from all
echo Allow from 127.0.0.0/255.0.0.0 ::1/128
echo '</Directory>'
echo
echo '</VirtualHost>'
webindex
#!/bin/bash
domain=$1
theuser=$2
index_page="/home/$theuser/web/index.html"
exec > $index_page
echo "<h1> Welcome to the website of $domain"