0

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.

5 Answers 5

3

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
Sign up to request clarification or add additional context in comments.

Comments

2

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.

1 Comment

put the FS variable in BEGIN block.
1

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.

Comments

0

I think you left the pipe (|)

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

Alternative

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

Comments

0
#!/bin/ksh
echo $1 | cut -f2 -d=

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.