views:

71

answers:

5

I am trying this option

#!/bin/ksh

echo $1
awk '{FS="=";print $2}' $1

and on the command line

test_sh INSTANCE=VIJAY

but awk is failing. Is there any problem here? Basically I need the value VIJAY passed on the command line.

A: 

I think you left the pipe (|)

echo $1 | awk '{FS="=";print $2}'

Alternative

echo $1 | cut -d'=' -f2
PeterMmm
+2  A: 

for awk, the second parameter is a name of the file to process. So you asked it to process the file named INSTANCE=VIJAY

Instead, do

echo "$1" | awk '{FS="=";print $2}'

Just to be clear, what this does is pass the input to be processed to awk via standard input through a pipe from output of echo; instead of awk reading it input from a file.

To quote from Awk manual:

If there are no files named on the command line, gawk reads the standard input.

DVK
put the FS variable in BEGIN block.
ghostdog74
A: 
#!/bin/ksh
echo $1 | cut -f2 -d=
Warren Young
+1  A: 

I think a simpler one is

#!/bin/sh

echo $1
echo $1 | cut -d= -f2

as cut can split on the equal sign as well and then show the second token. Also note that the passing $1 to awk was not correct as that argument is not a file.

Dirk Eddelbuettel
+2  A: 

ksh (and Bash) can do the splitting for you:

#!/bin/ksh
var="${1%=*}"
val="${1#*=}"
echo "Var is $var"
echo "Val is $val"

Running it:

$ ./scriptname INSTANCE=VIJAY
Var is INSTANCE
Val is VIJAY
Dennis Williamson