开发者

ksh - passing parameter with space

am trying to pass a parameter with a space in ksh via variables.

Here is some sample code to demonstrate the issue. As you will see the param开发者_StackOverflow中文版eter with the space is later handled as two variables - not what I am after.

*Update - I didn't copy and paste all the code in the origianl question. **

Contents of param_test.sh

#!/bin/ksh

echo "check 1"

param_string=${1}
param_string2=${2}

echo "check 2"

echo param_string = $param_string
echo param_string2 = $param_string2

echo "check 3"

Contents of call_param_test.sh

#!/bin/ksh

param_test.sh 'a b' c

CMD="param_test.sh 'a b' c"

# CMD=param_test.sh
# CMD="${CMD} 'a b c'"


echo CMD is  $CMD

echo now running CMD
${CMD}

echo back to calling script

echo at end


Results of executing call_param_test.sh

check 1
check 2
param_string = a b
param_string2 = c
check 3
CMD is param_test.sh 'a b' c
now running CMD
check 1
check 2
param_string = 'a
param_string2 = b'
check 3
back to calling script
at end

Thanks,


BashFAQ/050 is applicable. In general you should try to avoid putting commands in variables. If you must do it then an array is probably the way to go:

# create an array
CMD=(param_test.sh 'a b' c)
# execute the command contained in the array
${CMD[@]}


I changed the calling script something like this

#param_test.sh 'a b' c

#CMD="./param_test.sh \'a b\' c"
CMD="./param_test.sh"
ARGS1="a b"
ARGS2=c

# CMD=param_test.sh
# CMD="${CMD} 'a b c'"


echo CMD is  $CMD  $ARGS

echo now running CMD
#${CMD}
#${CMD} "$ARGS1" ${ARGS2}

echo back to calling script

echo at end

Since the args have spaces its quite tricky to give its as argument(I hope there shuld be better way). But the above works even though not an effective approach

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜