2010-09-14 5 views
2

J'essaie de passer un paramètre avec un espace dans ksh via des variables.ksh - passant le paramètre avec l'espace

Voici un exemple de code pour illustrer le problème. Comme vous le verrez, le paramètre avec l'espace est géré plus tard comme deux variables - pas ce que je suis après.

* Mise à jour - Je n'ai pas copié et collé tout le code dans la question origianl. **

Contenu de 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" 

Contenu de 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 

Merci,

Répondre

0

BashFAQ/050 est applicable. En général, vous devriez essayer d'éviter de placer des commandes dans des variables. Si vous devez le faire, alors un tableau est probablement la voie à suivre:

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

Merci Dennis, la sauce secrète dont j'avais besoin était $(). Ce qui a semblé fonctionner pour moi est d'utiliser ceci - myvar = '\ test \ test' out = $ (imprimer -R $ myvar | sed 's/\\/\\\\\\/g') – Anthony

0

je changé quelque chose de script appelant comme celui-ci

#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 

Puisque les args ont des espaces, il est assez difficile de donner son argument (j'espère qu'il devrait y avoir un meilleur moyen). Mais ce qui précède fonctionne même si pas une approche efficace

+0

Salut Raghuram, merci pour answere mais je l'ai mis à jour le message original. J'espère que cela va expliquer ce que j'essaie de faire mieux. – Anthony

Questions connexes