Tag Archives: bash

bash test the various possibilities

The following are examples of how you can test the various possibilities, and it works in bash or any POSIX-compatible shell:

if [ -z “${VAR}” ]; then
echo “VAR is unset or set to the empty string”
fi
if [ -z “${VAR+set}” ]; then
echo “VAR is unset”
fi
if [ -z “${VAR-unset}” ]; then
echo “VAR is set to the empty string”
fi
if [ -n “${VAR}” ]; then
echo “VAR is set to a non-empty string”
fi
if [ -n “${VAR+set}” ]; then
echo “VAR is set, possibly to the empty string”
fi
if [ -n “${VAR-unset}” ]; then
echo “VAR is either unset or set to a non-empty string”
fi

A variable in bash (and any POSIX-compatible shell) can be in one of three states:

unset
set to the empty string
set to a non-empty string
Most of the time you only need to know if a variable is set to a non-empty string, but occasionally it’s important to distinguish between unset and set to the empty string.

bash RBL check script

#!/bin/bash

# IPs or hostnames to check if none provided as arguments to the script
hosts=’
example.com
example.net
example.org
192.0.43.10

# Locally maintained list of DNSBLs to check
LocalList=’
b.barracudacentral.org

# pipe delimited exclude list for remote lists
Exclude=’^dnsbl.mailer.mobi$|^foo.bar$|^bar.baz$’

# Remotely maintained list of DNSBLs to check
WPurl=”http://en.wikipedia.org/wiki/Comparison_of_DNS_blacklists”
WPlst=$(curl -s $WPurl | egrep “

([a-z]+\.){1,7}[a-z]+

” | sed -r ‘s|||g;/$Exclude/d’)

# ———————————————————————

HostToIP()
{
if ( echo “$host” | egrep -q “[a-zA-Z]” ); then
IP=$(host “$host” | awk ‘/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/ {print$NF}’)
else
IP=”$host”
fi
}

Repeat()
{
printf “%${2}s\n” | sed “s/ /${1}/g”
}

Reverse()
{
echo $1 | awk -F. ‘{print$4″.”$3″.”$2″.”$1}’
}

Check()
{
result=$(dig +short $rIP.$BL)
if [ -n “$result” ]; then
echo -e “MAY BE LISTED \t $BL (answer = $result)”
else
echo -e “NOT LISTED \t $BL”
fi
}

if [ -n “$1” ]; then
hosts=$@
fi

if [ -z “$hosts” ]; then
hosts=$(netstat -tn | awk ‘$4 ~ /[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/ && $4 !~ /127.0.0/ {gsub(/:[0-9]+/,””,$4);} END{print$4}’)
fi

for host in $hosts; do
HostToIP
rIP=$(Reverse $IP)
# remote list
echo; Repeat – 100
echo ” checking $IP against BLs from $WPurl”
Repeat – 100
for BL in $WPlst; do
Check
done
# local list
echo; Repeat – 100
echo ” checking $IP against BLs from a local list”
Repeat – 100
for BL in $LocalList; do
Check
done
done

curl and POST, GET, PUT, DELETE

Make requests with data:
Since we learn how to make POST, GET, PUT, DELETE requests, we can now make same requests with data. In order to send data with those requests, we should use –data parameter. Here are some examples:

::::/bin/bash
# send login data with POST request
curl –request POST ‘http://www.linuxhow.tk/login/’ \
–data ‘username=myusername&password=mypassword’

# send search data to with get request
curl –request GET ‘http://www.linuxhow.tk/results?search_query=my_keyword’

# send PUT request with data
curl –request PUT ‘http://www.linuxhow.tk/rest-api/user/12345/’\
–data ’[email protected]

# same thing but this one get data from a file named data.txt
curl –request PUT ‘http://www.linuxhow.tk/user/12345/’\
–data @data.txt

create rsync incremental backups with hardlinking

Quite short and simple bash script:

#!/bin/bash
# Simple weekly bash backup script all variables explain themselves
# Version 1.03

what=”/ka”
where=”/ba”
set -e

today=$(date +’%Y-%m-%d’)
last=$(ls -r $where | head -1)
delete=$(ls -r $where | tail -n +7)

cd $where
nice -n 19 rsync –bwlimit=10000 –info=progress2 –link-dest=$where/$last $what $where/$today

[ -z “$delete” ] || rm -rf $delete

OpenVZ create VPS script

This bash script is useful to create Centos or other new VPS in few seconds. You can download it cr_vm.

Source below:

#!/bin/bash

if [ -z "$2" ]; then
echo usage: $0 ctid ipaddr
echo example: 521 192.168.122.152
exit
fi
if [ -f /vz/template/cache/centos-6-x86_64-20130522.tar.xz ]; then
echo "OK"
else

echo "================================================================"
echo "Download a Centos (6.0) template"
echo "================================================================"
wget http://mirror.duomenucentras.lt/openvz/contrib/template/precreated/centos-6-x86_64-20130522.tar.xz -O /vz/template/cache/centos-6-x86_64-20130522.tar.xz

fi

echo "================================================================"
echo "Create a new container named $1"
echo "================================================================"
vzctl create $1 --ostemplate centos-6-x86_64-20130522

echo "================================================================"
echo "Set the hostname"
echo "================================================================"
vzctl set $1 --hostname $1 --save

echo "================================================================"
echo "Set the IP address"
echo "================================================================"
vzctl set $1 --ipadd $2 --save

echo "================================================================"
echo "Set OpenDNS servers 208.67.222.222 and 208.67.220.220"
echo "================================================================"
vzctl set $1 --nameserver 208.67.222.222 --nameserver 208.67.220.220 --save

echo "================================================================"
echo "Set ROOT user password"
echo "================================================================"
vzctl set $1 --userpasswd root:plainpass

echo "================================================================"
echo "Stop and start the container named $1 and wait 10 secs"
echo "================================================================"
vzctl stop $1 && vzctl start $1 && sleep 10

echo "================================================================"
echo "Ping test to google.com"
echo "================================================================"
vzctl exec $1 ping -c 3 google.com

echo "================================================================"
echo "Restarting the node $1"
echo "================================================================"
vzctl restart $1

echo "================================================================"
echo "Test command 'ps aux' executed in the node $1"
echo "================================================================"
vzctl exec $1 ps aux

You can edit this script for your needs.

 

bash console shortcuts

Ctrl + A Go to the beginning of the line you are currently typing on
Ctrl + E Go to the end of the line you are currently typing on
Ctrl + L Clears the Screen, similar to the clear command
Ctrl + U Clears the line before the cursor position. If you are at the end of the line, clears the entire line.
Ctrl + H Same as backspace
Ctrl + R Let’s you search through previously used commands
Ctrl + C Kill whatever you are running
Ctrl + D Exit the current shell
Ctrl + Z Puts whatever you are running into a suspended background process. fg restores it.
Ctrl + W Delete the word before the cursor
Ctrl + K Clear the line after the cursor
Ctrl + T Swap the last two characters before the cursor
Esc + T Swap the last two words before the cursor
Alt + F Move cursor forward one word on the current line
Alt + B Move cursor backward one word on the current line
Tab Auto-complete files and folder names