LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (http://www.linuxquestions.org/questions/linux-software-2/)
-   -   simple argument passing (http://www.linuxquestions.org/questions/linux-software-2/simple-argument-passing-483197/)

tostay2003 09-13-2006 02:36 PM

simple argument passing
 
I am trying to pass two arguments to the script, say test.sh

by using ./test.sh abc "xyz"

I want to have the double quotes captured in the variable COLUMN. i.e. the output of this script should be "xyz". How do I do that?


Code:

#!/usr/bin/ksh

if [ $# -lt 2 ]
then
 echo " "
 echo "Invalid number of input parameters. Parameters:"
 exit 1
fi

TABLE=$1
COLUMN=$2

echo ${COLUMN}


MensaWater 09-13-2006 02:46 PM

Make your input to the script:

abc \"xyz\".

The backslashes escape the special meaning of the quotes so they are passed in litterally.

tostay2003 09-13-2006 02:52 PM

Is there no way that we can capture it after we pass on the arguments to the script.

Does the $2 dont take quotes at all or is it during the assignment that it gets lost??

marozsas 09-13-2006 02:52 PM

for more complicated inputs, e.g. a"entry"with"several"weird"quotes"""around"
you can use the single quote to pass the entire string litterally:

a"entry"with"several"weird"quotes"""around"

tostay2003 09-13-2006 02:54 PM

I want to do it after getting into the script? Is that possible?

FreeDoughnut 09-13-2006 03:03 PM

It isn't because the quotes are done at the shell level. You'd have to guess and add the quotes in the script where you think it would be.

tostay2003 09-13-2006 03:19 PM

To guess in script I need to check whether all the characters in the argument are lower case. Could you give me a function that tells, if all the letters in the argument are lower case.

I looked in google to check for that the simple function to check lower case, but its leading to complex codes always.

I want to implement this. Will it work.

Code:

if [check whether $2 is all lower case]
then
  COLUMN=$2
else
  COLUMN=`\"$2\"`
fi


makyo 09-13-2006 04:11 PM

Hi.

I have a partially humorous suggestion. It is true as FreeDoughnut says that the quotes are removed when the shell processes a command -- at least for bash and other Bourne-shell related shells.

The rc shell handles quotes differently. Here's an example of an rc script being called from an interactive rc shell:
Code:

% rc
; cat r1
#!/usr/bin/rc

# @(#) r1      Demonstrate rc shell quoting.

echo "Hello, world."

echo First argument is $1
; ./r1 "a"
"Hello, world."
First argument is "a"

As you can see, the quotes were passed in and echoed.

The rc shell never really caught on, and the only place I saw it used much was at Thinking Machines (TMC) in Boston. However, it seems to be available in many distributions.

Nice to know perhaps, but, as I said, probably not a serious solution -- and, no, I don't know much more about it than that :) See your friendly man rc page for details (and it is a pretty good man page) ... cheers, makyo


All times are GMT -5. The time now is 03:57 AM.