User Tools

Site Tools


linux:var_bash

BASH Scripting

Tips and Summarys

Parameter Function
$? Return Value (i.e. 0 or 1 if an error occured)
$0 the Name of the Script
$1 First Parameter
$2 Second Parameter
$[n] n Parameter
$# Number of entered Parameters

Command substitution

The Command in the $( … ) will be executed first and the result will be used as the parameter for chmod.

 chmod 770 $(ls | grep rizon.sh)

xargs

Another possibility to pass on results from commands is xargs

find /usr/ share -name '*README*' | xargs ls -l

whoami

rootcheck.sh
if [ `whoami` != "root" ]; then
        echo "This installation must be run as user root."
        echo "Use the 'su' or 'sudo' command to become root, and run this script again."
        echo
        exit 1
fi

Various Scripts

mailtoskull.sh

A tiny script that tars a specific file and mails it to my Mailbox

mailtoskull.sh
#!/bin/bash
tar -czf $1.tar.gz ./$1
mutt -s "script generated mail" -a $1.tar.gz skull@darktemple.ch
rm $1.tar.gz

rsnapshot_hourly

rsnapshot_hourly.sh
#!/bin/bash
# //inetpub/data/scripts/rsnapshot_hourly
# based on a Script written by Chrigi Hartmann
# http://www.calandacomp.ch
[ -f /var/log/rsnapshot.log ] && rm /var/log/rsnapshot.log || echo "Creating File"
date > /var/log/snapshot.log
#/bin/mount /dev/sdc1 /mnt/exthd -- Example how to mount an external drive
/usr/bin/rsnapshot -v hourly >> /var/log/snapshot.log
/usr/bin/rsnapshot du >> /var/log/rsnapshot.log
#/bin/umount -l /dev/sdc1 --dont forget tu umount it!
date >> /var/log/snapshot.log
mutt -s "Rsnapshot Hourly" admin@darktemple.ch < /var/log/rsnapshot.log

eadir.sh

eadir.sh
  1 #!/bin/bash
  2 #===============================================================================
  3 #
  4 #          FILE:  eadir.sh
  5 #
  6 #         USAGE:  ./eadir.sh
  7 #
  8 #   DESCRIPTION:  removes every @eaDir which were created by synology backup
  9 #
 10 #       OPTIONS:  ---
 11 #  REQUIREMENTS:  ---
 12 #          BUGS:  ---
 13 #         NOTES:  ---
 14 #        AUTHOR:  SKull (), skull@darktemple.ch
 15 #       COMPANY:
 16 #       VERSION:  1.0
 17 #       CREATED:  08/05/2011 10:09:59 PM CEST
 18 #      REVISION:  ---
 19 #===============================================================================
 20
 21 #output=$(locate @eaDir)
 22 #rm -rf "$output"
 23 #RM='rm -rf'
 24 while read line
 25 do
 26   rm -rf "$line"
 27 done < 'result.txt'

dev_test.sh

dev_test.sh
  1 #!/bin/bash
  2 #===============================================================================
  3 #
  4 #          FILE:  dev_test.sh
  5 #
  6 #         USAGE:  ./dev_test.sh
  7 #
  8 #   DESCRIPTION:  testst if devices are connected
  9 #
 10 #       OPTIONS:  ---
 11 #  REQUIREMENTS:  ---
 12 #          BUGS:  ---
 13 #         NOTES:  ---
 14 #        AUTHOR:  SKull (), skull@darktemple.ch
 15 #       COMPANY:
 16 #       VERSION:  1.0
 17 #       CREATED:  09/19/2011 08:37:05 AM CEST
 18 #      REVISION:  ---
 19 #===============================================================================
 20
 21 #!/bin/bash
 22 if [ -b /dev/sdc  ]; then
 23     echo "Device sdc detected"
 24     echo "Doing stuff on sdc"
 25   elif [ -b /dev/sdd ]; then
 26     echo "Device sdd detected"
 27     echo "Doing stuff on sdd..."
 28   else
 29     echo "No device detected"
 30     echo "Doing nothing."
 31 fi
linux/var_bash.txt · Last modified: 2012/04/15 19:38 by skull@darktemple.ch