1.  Execute command and return the process ID

Say you want to execute the date command and return the process ID.

$ date & echo $!

2.  My bash profile

I keep my profile in a file named .username and source it when I need to use it.

Right now my basic one is,

### Command Prompt
PS1="\n---------------------------------\[\e]0;\w\a\]\n\t \[\e[32m\]\u@\h \[\e[33m\]\w\[\e[0m\]\n\$ "

### Aliases
alias rm='rm -i'
alias mv='mv -i'
alias cp='cp -i'
alias pico='pico -r3000'
alias ls='ls -F'
alias ll='ls -l'
alias ltr='ls -ltr'
#alias vi="vim"
alias htdocs="cd /var/www/localhost/htdocs"
alias w="w | sort"

3.  Comparing two files for a difference

If you have file1 and file2 and you want to know where they differ, you can use the following if statement.

if ! diff file1 file2 >> /dev/null; then
   echo "they differ"
else
   echo "they don't differ"
fi 

4.  Arithmetic from the command line

$ echo $((1+1))

Outputs 2 of course.

5.  Remove the last line of a file

cat file.txt | sed '$d'

6.  Handy MySQL backup script

I called this script backup_mysql.bash

Change the permissions of this script,

$ chmod 700 /path/to/backup_mysql.bash

I put this in cron as,

30 * * * * /path/to/backup_mysql.bash

which backups up and zips your database dump once an hour if there are changes.

#!/bin/bash

# Usage: ./backup_mysql.bash [database]
# Database is optional. If left off, all databases will be zipped into one dump.

mysql_username="username"
mysql_password="password"

curdatetime=`date +%Y%m%d%H%M%S`

cd /var/www/mysql_backups

if [[ "${1}" != "" ]]; then
        mysql_database="${1}"
        mysqldump -u ${mysql_username} --password=${mysql_password} --opt ${mysql_database} > ${curdatetime}.${mysql_database}.mysql
else
        mysql_database="all_databases"
        mysqldump -u ${mysql_username} --password=${mysql_password} --all-databases > ${curdatetime}.${mysql_database}.mysql
fi

sed '$d' ${curdatetime}.${mysql_database}.mysql > 2; mv -f 2 ${curdatetime}.${mysql_database}.mysql

if [[ -e last.${mysql_database}.mysql ]]; then
        curdump=`cat ${curdatetime}.${mysql_database}.mysql`
        lastdump=`cat last.${mysql_database}.mysql`
        if ! diff ${curdatetime}.${mysql_database}.mysql last.${mysql_database}.mysql >> /dev/null; then
                cp -f ${curdatetime}.${mysql_database}.mysql last.${mysql_database}.mysql
                gzip ${curdatetime}.${mysql_database}.mysql
                chmod 700 ${curdatetime}.${mysql_database}.mysql.gz last.${mysql_database}.mysql
        else
                rm -f ${curdatetime}.${mysql_database}.mysql
        fi
else
        cp -f ${curdatetime}.${mysql_database}.mysql last.${mysql_database}.mysql
        gzip ${curdatetime}.${mysql_database}.mysql
        chmod 700 ${curdatetime}.${mysql_database}.mysql.gz last.${mysql_database}.mysql
fi

7.  Monitor for file transfer to be done

I used this script once when I transfered a music directory to another computer with scp. I ran the script on the receiving computer. It assumed the stuff would go into ~/Music/MyDir.

#!/bin/bash

lastsize=`du -sk ~/Music/MyDir`

sleep 15

cursize=`du -sk ~/Music/MyDir`
echo $cursize

while [ "$lastsize" != "$cursize" ]; do
        lastsize=`du -sk ~/Music/MyDir`

        sleep 15

        cursize=`du -sk ~/Music/MyDir`
        echo $cursize
done

echo "It's all done"

8.  A simple menu for common tasks

I named this file menu and put it in a directory that's in my path. Don't forget to chmod 755 menu so that you can execute it.

Usage: menu

or

Usage: menu <one of the options Example: menu 3

#!/bin/bash

option="${1}"

if [[ $option == "" ]]; then
        clear

        echo "
        SSH
        ---
        1: ssh account 1 (foo-server-1)
        2: ssh account 2 (foo-server-2)

        Open Files
        ----------
        3: some_file.txt
        4: another.xml
        5: nice_latex.tex

"

        read option
fi

if [[ $option == "1" ]] || [[ $option == "2" ]] || [[ $option == "3" ]] || [[ $option == "4" ]] || [[ $option == "5" ]]; then
  if [[ $option == "1" ]]; then
                ssh joefoo@foo-server-1
        elif [[ $option == "2" ]]; then
                ssh manbar@foo-server-2
        elif [[ $option == "3" ]]; then
                vi /path/to/some_file.txt
        elif [[ $option == "4" ]]; then
                vi /path/to/another.xml
        elif [[ $option == "5" ]]; then
                vi /path/to/nice_latex.tex
        fi
else
        echo "
        I don't know what to do!!!

"
fi

8.1  Detect when a disk drive is about full

#!/bin/bash

# This script does a very simple test for checking disk space.

space=`df -h | awk '{print $5}' | grep % | grep -v Use | sort -n | tail -1 | cut -d "%" -f1 -`
alertvalue="80"

if [ "$space" -ge "$alertvalue" ]; then
  echo "At least one of my disks is nearly full!" | mail -s "daily diskcheck" root
else
  echo "Disk space normal" | mail -s "daily diskcheck" root
fi

8.2  If statment that uses grep like eregi() in PHP

#!/bin/bash

#> error and success are two files
# error="No pages of output"
# success="Output written on"

if grep "No pages of output" error > /dev/null; then
  echo "We found 'No pages of output' in error"
fi
#> out puts the echo above

if grep "written on" success > /dev/null; then
  echo "We found 'Output written on' in success"
fi

8.3  Math

x=1
y=3
z=$[$x + $y]
echo $z
#> outputs 4 duh!
echo $[1 + 2]
#> outputs 3

8.4  Print last file by modification date

# ls -ltr some_directory | tail -n1 | sed 's/^.*\ //g' | sed 's/\/$//g'

or

# ls -tr | tail -n1 | sed 's/\/$//g'

8.5  References

9.  Page Comments (Click to edit)

aMxQN0 <a href="http://djipzhujzpqx.com/ »">djipzhujzpqx</a>, [url=http://qvwnqrgihayq.com/ »]qvwnqrgihayq[/url], [link=http://cqfxlfdwygkb.com/ »]cqfxlfdwygkb[/link], http://nqtmtqlfnbdb.com/ »

Design by N.Design Studio, adapted by solidGone.org (version 1.0.0)
Powered by pmwiki-2.2.0-beta65