开发者

problem in shell script

开发者 https://www.devze.com 2022-12-08 19:58 出处:网络
I a开发者_StackOverflowm trying this option #!/bin/ksh echo $1 awk \'{FS=\"=\";print $2}\' $1 and on the command line

I a开发者_StackOverflowm 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.


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


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.


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.


I think you left the pipe (|)

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

Alternative

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


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

精彩评论

暂无评论...
验证码 换一张
取 消