Bash
A Simple Command Line Menu
Usage: menu or menu <option>
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
Detect when a disc is full
# 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
If statement that uses grep like eregi() in php
#> 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
http://www.tldp.org/LDP/Bash-Beginners-Guide/html/index.html
Check if a directory is empty
http://www.cyberciti.biz/faq/linux-unix-shell-check-if-directory-empty/
FILE=""
DIR="/tmp"
# init
# look for empty dir
if [ "$(ls -A $DIR)" ]; then
echo "Take action $DIR is not Empty"
else
echo "$DIR is Empty"
fi
# rest of the logic
Beginning Bash Programming
Bourne Shell Programming
http://www.ooblick.com/text/sh/
Advanced Bash-Scripting Guide
http://www.tldp.org/LDP/abs/html/
Create a progess bar in a script
http://ubuntuforums.org/showthread.php?t=526721
Arrays in bash scripting
found here: http://www.linuxjournal.com/content/bash-arrays
If you're used to a "standard" *NIX shell you may not be familiar with bash's array feature. Although not as powerful as similar constructs in the P languages (Perl, Python, and PHP) and others, they are often quite useful.
Bash arrays have numbered indexes only, but they are sparse, ie you don't have to define all the indexes. An entire array can be assigned by enclosing the array items in parenthesis:
Individual items can be assigned with the familiar array syntax (unless you're used to Basic or Fortran):
arr[1]=World
But it gets a bit ugly when you want to refer to an array item:
To quote from the man page:
In addition the following funky constructs are available:
${!arr[*]} # All of the indexes in the array
${#arr[*]} # Number of items in the array
${#arr[0]} # Length of item zero
The ${!arr[*]} is a relatively new addition to bash, it was not part of the original array implementation.
The following example shows some simple array usage (note the "[index]=value" assignment to assign a specific index):
array=(one two three four [5]=five)
echo "Array size: ${#array[*]}"
echo "Array items:"
for item in ${array[*]}
do
printf " %s\n" $item
done
echo "Array indexes:"
for index in ${!array[*]}
do
printf " %d\n" $index
done
echo "Array items and indexes:"
for index in ${!array[*]}
do
printf "%4d: %s\n" $index ${array[$index]}
done
Running it produces the following output:
Array size: 5 Array items: one two three four five Array indexes: 0 1 2 3 5 Array items and indexes: 0: one 1: two 2: three 3: four 5: five
Note that the "@" sign can be used instead of the "*" in constructs such as ${arr[*]}, the result is the same except when expanding to the items of the array within a quoted string. In this case the behavior is the same as when expanding "$*" and "$@" within quoted strings: "${arr[*]}" returns all the items as a single word, whereas "${arr[@]}" returns each item as a separate word.
The following example shows how unquoted, quoted "*", and quoted "@" affect the expansion (particularly important when the array items themselves contain spaces):
array=("first item" "second item" "third" "item")
echo "Number of items in original array: ${#array[*]}"
for ix in ${!array[*]}
do
printf " %s\n" "${array[$ix]}"
done
echo
arr=(${array[*]})
echo "After unquoted expansion: ${#arr[*]}"
for ix in ${!arr[*]}
do
printf " %s\n" "${arr[$ix]}"
done
echo
arr=("${array[*]}")
echo "After * quoted expansion: ${#arr[*]}"
for ix in ${!arr[*]}
do
printf " %s\n" "${arr[$ix]}"
done
echo
arr=("${array[@]}")
echo "After @ quoted expansion: ${#arr[*]}"
for ix in ${!arr[*]}
do
printf " %s\n" "${arr[$ix]}"
done
When run it outputs:
Number of items in original array: 4 first item second item third item After unquoted expansion: 6 first item second item third item After * quoted expansion: 1 first item second item third item After @ quoted expansion: 4 first item second item third item