http://en.wikibooks.org/wiki/Bourne_Shell_Scripting/Appendix_C:_Quick_Reference
#!/bin/bash = Starting line
echo “print text”
$X = Read the value of X
if [ -n $X ]; then # -n tests to see if the argument is non empty
Loops
while [ $X -le 20 ] // ( -le is equel ) ( -lt Less than)
do
touch $X.html
X=$((X+1)) // (or) let X=X+1
done
for i in $( ls ); do //`seq 1 10` read 1 to 10 numbers
echo item: $i
done
COUNTER=20 until [ $COUNTER -lt 10 ]; do echo COUNTER $COUNTER let COUNTER-=1 done Script to cut word from a line ls | grep cpmove | grep 'tar.gz' | cut -d'-' -f2 | cut -d. -f1 >/tmp/usernames (or) ls | grep cpmove | grep 'tar.gz' | cut -d'-' -f2 | cut -d. -f1 | uniq >/tmp/usernames (uniq means uniqueness) Script to restore pakage with help from the above for i in `cat /tmp/usernames ` ; do echo "/scripts/restorepkg $i" ; done
ls | grep cpmove | grep ‘tar.gz’ | cut -d’-‘ -f2 | cut -d. -f1 >/tmp/usernames
for i in `cat /tmp/usernames ` ; do mv cpmove-$i.* cpmove-$i.jpg; done
script to cut lines which contain the word FOUND
for i in ‘cat test’; do grep FOUND $i > rr; done
Script to restart a service
/usr/bin/pgrep named
if [ $? -ne 0 ]
then
/etc/init.d/named restart
fi
Script to add spf record for all accounts
———————————————
cd /var/named
ls *.db > test
cat test | grep .db | cut -d’.’ -f1,2 > k
for i in `cat k` ; do echo $i. IN TXT “v=spf1 ~all” >> $i.db ; done
Sample script for create multiple cpanel accounts
——————————————————
#!/bin/bash
for i in `cat domains`;
do
echo “account $i creating”
echo ” enter the account username:”
read r;
/scripts/createacct $i $r
echo “Account $r created”;
done
cat /dev/null > result;
for i in `cat file`;
do echo $i >> result; echo ===================== >> result;
s=`pwd`
p=`grep -irl $i *`
for j in $p;
do
h=${s}/${p}
echo $h >> result;
done
done
Script to correct cPanel users and files ownership
——————————————————————————-
do
chown $i.$i /home/$i -R;
chown $i.mail /home/$i/etc -R;
chown $i.nobody /home/$i/public_html;
done;
Script to move specific files from different folders to a specific Folder
—————————————————————————————- For Hussin
#!/bin/bash
for ((i=12; i do
echo recup_dir.$i >> test1
done
for j in `cat test1`;
do
cd /vz/restore
cd $j
ls | grep *.txt | xargs mv *.txt ../texts/
ls | grep *.tar | xargs mv *.tar ../tar-files
ls | grep *.gz | xargs mv *.gz ../gz-files
ls | grep *.*.gz | xargs mv *.*.gz ../gz-files
ls | grep *.tar.gz | xargs mv *.tar.gz ../tar.gz/
ls | grep *.html | xargs mv *.html ../HTML
ls | grep *.js | xargs mv *.js ../js-files
ls | grep *.php | xargs mv *.php ../PHP-files
ls | grep public_html | xargs mv public_html ../PUBLIC-folders
ls | grep log | xargs mv *grep* ../logs
ls | grep *.7z | xargs mv *.7z ../7z-files
ls | grep *.zip | xargs mv *.zip ../zip-files
done
—————————————————————————————-
Script to rsync some data if no other backup process are running
Find and remove files which is older than 1year
find ./ ctime +365 | xargs rm -f