Passing external variable to awk | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Passing external variable to awk

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-01-2013
jpkumar10 jpkumar10 is offline
Registered User
 
Join Date: Jun 2011
Last Activity: 4 January 2014, 7:28 PM EST
Posts: 26
Thanks: 13
Thanked 0 Times in 0 Posts
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.

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.
Sponsored Links
    #2  
Old 03-01-2013
panyam panyam is offline Forum Advisor  
Registered User
 
Join Date: Sep 2008
Last Activity: 3 April 2014, 10:13 AM EDT
Posts: 1,148
Thanks: 20
Thanked 104 Times in 99 Posts
Pls paste the error you are getting and which OS you are in to?
Sponsored Links
    #3  
Old 03-01-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 17 April 2014, 7:46 AM EDT
Location: Aachen, Germany
Posts: 3,514
Thanks: 51
Thanked 819 Times in 782 Posts
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?
    #4  
Old 03-01-2013
Scrutinizer's Avatar
Scrutinizer Scrutinizer is online now Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 18 April 2014, 2:13 PM EDT
Location: Amsterdam
Posts: 8,874
Thanks: 230
Thanked 2,141 Times in 1,922 Posts
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:

Code:
print " this is the value: " m

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

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

Sponsored Links
    #5  
Old 03-01-2013
jpkumar10 jpkumar10 is offline
Registered User
 
Join Date: Jun 2011
Last Activity: 4 January 2014, 7:28 PM EST
Posts: 26
Thanks: 13
Thanked 0 Times in 0 Posts
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.
Sponsored Links
    #6  
Old 03-01-2013
Scrutinizer's Avatar
Scrutinizer Scrutinizer is online now Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 18 April 2014, 2:13 PM EDT
Location: Amsterdam
Posts: 8,874
Thanks: 230
Thanked 2,141 Times in 1,922 Posts
The most universal solution would be to use a case statement, for example:

Code:
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:

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

or in recent bash:

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

Note the double square brackets

Last edited by Scrutinizer; 03-01-2013 at 06:49 AM..
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
passing file extension using external variable nrjrasaxena Shell Programming and Scripting 4 09-26-2012 11:55 AM
Help needed with awk external variable InduInduIndu Shell Programming and Scripting 1 08-24-2012 11:27 AM
[awk] - how to insert an external variable graysky Shell Programming and Scripting 3 11-27-2011 11:06 AM
If statement in awk with external variable doublejz Shell Programming and Scripting 6 12-11-2008 04:09 PM
awk help (external variable) pupp Shell Programming and Scripting 8 02-29-2008 10:53 PM



All times are GMT -4. The time now is 02:14 PM.