Jun 1, 2010

Bash programming.

Manipulation of number

total=`expr ${total} + ${pathsize}

let sum=0
let sum=$sum+$num


Testing Using While loop.

#!/bin/bash
COUNT=6
# bash while loop
while [ $COUNT -gt 0 ]; do
echo Value of count is: $COUNT
let COUNT=COUNT-1
done


Calculating the Date Modified
Sample Code:

CUR_DATE=$(date +"%s");
for JOB_FILE in [0-9]*; do
MOD_FILE_DATE=$(stat -c "%Y" $JOB_FILE)
echo -e "$JOB_FILE\t$CUR_DATE\t$MOD_FILE_DATE"
done

Or

if [ "$(( $(date +"%s") - $(stat -c "%Y" $somefile) ))" -gt "7200" ]; then
echo "$somefile is older then 2 hours"
fi

VIM Command


http://www.yolinux.com/TUTORIALS/LinuxTutorialAdvanced_vi.html

Encoding String using BASH


OUTPUT=$(php -r "echo rawurlencode('$(sed "s/'/\\\\'/g" <<< "$OUTPUT")');")


Getting Absolute File Path


Originally FROM: how-to-get-the-absolute-path-within-the-running-bash-scrip

# Absolute path to this script. /home/user/bin/foo.sh
SCRIPT=$(readlink -f $0)
# Absolute path this script is in. /home/user/bin
SCRIPTPATH=`dirname $SCRIPT`


Multi di-mentional ARRAY



#!/bin/bash
A=("abcd" "dcba")
Z=("zxyw" "wxyu" "tuxz")
ARR=(A Z)

for ROW in ${ARR[*]}
do
echo "processing row: $ROW"
TEMP="\${$ROW[*]}"
ROW=`eval echo $TEMP`
for ELEMENT in $ROW
do
echo " processing element: $ELEMENT"
done
done

Original Post:bash-multidimensional-array

Bash : converting unix timestamp to date


converting unix timestamp to date

No comments: