Wednesday, June 6, 2007

Parse line to variables in KSH

I needed to read information from line and store it in separate variables in ksh. After some googling and tests I finally found out the right solution. It's so obvious and simple. I have to learn to think in more simple way. So here it is.

File with infos:

Columns:"volume group" "mount point" "size" "mount group"

datavg /data/test1 10 data
datavg /data/test2 10 data
datavg /data/test3 70 data
datavg /data/test3 10 data


The script:
#!/bin/ksh

#
# Name: genfs.sh
# Auth: Miroslav Pilat
# Date: 5.6.2007
# Desc: Script will create LVs and FSs in described VG.
# Infos are read from genfs.txt
#

F_SRC="genfs.txt"
CNT=0
LPSIZE=16

while read VG MNTP RSIZE MNTG; do

if (( $CNT < 10 )); then
CNTS=00$CNT
else
CNTS=0$CNT
fi


LVNAME="L$VG""_""$CNTS"


if (( $RSIZE < $LPSIZE )); then
LP=1;
else
LP=$(( $RSIZE / $LPSIZE ));
fi

echo "mklv -t jfs2 -y $LVNAME $VG $LP"
echo "crfs -v jfs2 -d /dev/$LVNAME -m $MNTP -u $MNTG -a size=$RSIZE""M"
echo "-----------------------------------------------------------------"

((CNT++))
done < $F_SRC


The bold text in the code section is the magic. It took me almost 2 hours to find out how to do it. It's plain simple. KSH/Bash variable IFS is by default set to space tab new line (\n) so when you read from file or stdin the information is automaticly split in to variables you specify after read command. Very simple.

Some description to the script:
It's not a bulletproof script so it just echos the commands needed for LV and FS creation. Use it on your own risk.

Purpose:
Speed up creation of testing LVM enviroment on AIX machine.

Abbreviations:
  • VG - volume group
  • LV - logical volume
  • FS - filesystem
  • IFS - internal field separator

No comments: