Passing external variable to awk

Hi,

I am trying to write a bash script in which I need to pass a external variable to the awk program. I tired using -v but it not accepting the value.

Here is my sample code.

#!/usr/bin/bash
######################################################################################
#### This bash script is to form meta
#### To run the script we have to do in the following format ./meta_script.sh frame_no lun_id's no_of_way_meta
###  If you want to create 8 way meta for devics 18DF:18FE on frame 5643 EG: ./meta_script.sh 5643 18DF:18FE 8

ARGS=3
E_WRONGARGS=85

if [ $# -ne "$ARGS" ] # Check for proper number of command-line args.
then
   echo "Usage: `basename $0` frame_no lund_id NO_of_WaY_meta "
   exit $E_WRONGARGS
fi
frame_no=$1;
lun_id=$2
meta_way=$3;

echo "$frame_no, $lun_id, $meta_way "

#if [ $frame_no -ne "\[0-9\]*" ] 
#then
#echo " please enter frame no EG: 1234 "
#exit $E_WRONGARGS
#fi
#if [ $lun_id != "[0-9A-F]*:[0-9A-F]*" ]
#then
#  echo " please enter device in 18DF:18FE format "
#  exit $E_WRONGARGS
#fi

#symaccess -sid $frame_no list assign -devs ${lun_id}|awk '/^[0-9]/ {print $1}'|nawk -v m=$meta_way 'NR%m==1{print " this is the value m";val=$0;print "form meta from dev "$0" config=striped;";next;}{print "add dev "$0" to meta "val";"}' >meta

symaccess -sid $frame_no list assign -devs ${lun_id}|awk '/^[0-9]/ {print $1}'

cat meta |awk '$5~/^[0-9]/ {print "bind tdev "$5" to pool VP_TIER_SATA,preallocate size=aaaa cyl;";}'> binding 

cat binding |awk '{print $3}'|xargs|sed 's/ /,/g'
# 

In the above code I am trying to pass the variable 8 which is $meta_way to the awk program but it is not accepting. Let me know if I am doing anything wrong.
Any help is greatly appreciated. Thanks in advance.

Pls paste the error you are getting and which OS you are in to?

The line using meta_way is commented out, so - no surprise it doesn't print.
In case it were uncommented, are you sure meta_way 's contents is numerical?
In case it were numerical, the code as is should print the message for the first line and for every other 8th line, then, i.e. line 9, 17, and so on. Is that what you desire?

I noticed the variable "m" is not being printed inside your awk statement. If you want to to use it, it needs to be outside the double quotes, for example:

print " this is the value: " m

You can test if it is being passed correctly, by testing with:

nawk -v m=$meta_way '{print m}'

Thanks scrutinizer. I got the output. I am using Solaris 10.
I was wondering if you could help me if statement. as mention in the code. I want to check if the entered values is numeric and it should be 4 digit. is there any way to check if the entered values are digits for frame and the entered values are hexa decimal for lun_ids and numeric for meta_way.

The most universal solution would be to use a case statement, for example:

case $val in 
  [0-9][0-9][0-9][0-9]) echo OK ;;
  *)                    echo not a 4-digit number
esac

Additionally, in bash you could also use:

if [[ $val == [0-9][0-9][0-9][0-9] ]]; then

or in recent bash:

if [[ $val =~ ^[0-9]{4}$ ]]; then 

Note the double square brackets