|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
Pls paste the error you are getting and which OS you are in to?
|
| Sponsored Links | ||
|
|
#3
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
|||
|
|||
|
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
|
||||
|
||||
|
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}$ ]]; thenNote the double square brackets Last edited by Scrutinizer; 03-01-2013 at 06:49 AM.. |
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|