Bash

Bash: Numbering the processing command

Submitted by admin on Thu, 04/18/2019 - 17:29

Define a function echo_count and count the function name to be called in the script. You can refer to a variable ${TOTAL} .For example,
#!/bin/bash

export TOTAL=$(( $(grep '^echo_count' $0 | wc -l) ))
export COUNT=1

function echo_count () {

echo -n "($(( COUNT++ ))/${TOTAL}) $1"
}

echo
echo_count 'Processing one - Stopping Apache2... '
sudo service apache2 stop
echo 'Done'

echo_count 'Processing two - '
echo 'Apache2 is not working.'

Tags

AWS EC2: Use NPT inside VPC

Submitted by admin on Mon, 01/22/2018 - 14:57

AWS announced to provide their own internal NTP inside VPC at AWS re:Invent 2017. Here is how to do it:
CentOS
sudo yum erase ntp*; sudo yum -y install chrony; sudo service chronyd start

Debian/Ubuntu
sudo apt-get remove -y ntp*; sudo apt-get -y install chrony; sudo service chronyd start; apt-get autoremove

Tags

SSH: How to Avoid Connection Error

Submitted by admin on Tue, 06/20/2017 - 16:08

Add an option -o StrictHostKeyChecking=no into a ssh command:
ssh -o StrictHostKeyChecking=no your_username@example.com

OR
export TARGET_HOST='example.com'

if [ -z `ssh-keygen -F ${TARGET_HOST}` ]; then
ssh-keyscan -H ${TARGET_HOST} >> ~/.ssh/known_hosts
fi

ssh-keygen -R ${TARGET_HOST}
ssh-keyscan -f ~/.ssh/known_hosts ${TARGET_HOST} >>~/.ssh/known_hosts 2>/dev/null

Tags

AWS EC2: How to Create AMI Image by Bash Shell Script

Submitted by admin on Thu, 11/17/2016 - 14:48

#!/bin/sh

export DATE_CURRENT=`date +%Y-%m-%d`
export TIME_CURRENT=`date +%Y%m%d%H%M%S`
export PURGE_AFTER_DAYS=30
export PURGE_AFTER=`date -d +${PURGE_AFTER_DAYS}days -u +%Y-%m-%d`
export BACKUP_TAG='backup'

# 1-1 Get the list of instances that you want to get backup
INSTANCES=`aws ec2 describe-tags --filters "Name=resource-type,Values=instance" "Name=key,Values=${BACKUP_TAG}" | awk '{print $3}'`

for INSTANCE in ${INSTANCES}; do

BACKUP=`aws ec2 describe-tags --filters "Name=resource-type,Values=instance" "Name=resource-id,Values=${INSTANCE}" "Name=key,Values=${BACKUP_TAG}" | awk '{print $5}'`

# 1-2 Check if the instance is marked as backup or not
if [ "${BACKUP}" == "true" ]; then

# 1-3 Create a backup
AMI_ID=`aws ec2 create-image --instance-id ${INSTANCE} --name "${INSTANCE}_${TIME_CURRENT}" --no-reboot`

# 1-4 Create a tag for search for later use
aws ec2 create-tags --resources ${AMI_ID} --tags Key=PurgeAllow,Value=true Key=PurgeAfter,Value=$PURGE_AFTER
fi
done

# 2-1 Search backups by tag
AMI_PURGE_ALLOWED=`aws ec2 describe-tags --filters "Name=resource-type,Values=image" "Name=key,Values=PurgeAllow" | awk '{print $3}'`

for AMI_ID in ${AMI_PURGE_ALLOWED}; do
PURGE_AFTER_DATE=`aws ec2 describe-tags --filters "Name=resource-type,Values=image" "Name=resource-id,Values=${AMI_ID}" "Name=key,Values=PurgeAfter" | awk '{print $5}'`

if [ -n ${PURGE_AFTER_DATE} ]; then
DATE_CURRENT_EPOCH=`date -d ${DATE_CURRENT} +%s`
PURGE_AFTER_DATE_EPOCH=`date -d ${PURGE_AFTER_DATE} +%s`

if [[ ${PURGE_AFTER_DATE_EPOCH} < ${DATE_CURRENT_EPOCH} ]]; then
# 2-2 Delete backup if it is marked as backup
aws ec2 deregister-image --image-id ${AMI_ID}

SNAPSHOT_ID=`aws ec2 describe-images --image-ids ${AMI_ID} | grep EBS | awk '{print $4}'`
aws ec2 delete-snapshot --snapshot-id ${SNAPSHOT_ID}
fi
fi
done

Tags

Bash: How to Insert One White Space before and after English Words in Japanese

Submitted by admin on Wed, 09/28/2016 - 13:38

Consider to insert one white space before and after English word in Japanese. Run the following script by changing a string 'YOUR_FILENAME'. The script copy the original file as YOUR_FILENAME.org and overwrite and replace the original file by inserting one white space before and after an English word. It is useful multi-byte language such as CJK (Chinese, Japanese and Korean).
export FILENAME='index.rst'; \
cp ${FILENAME} ${FILENAME}.org; \
cat ${FILENAME}.org | sed y/0123456789/0123456789/ | \

Tags