Why double quotation marks doesn't work in ksh function parameters passing?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Why double quotation marks doesn't work in ksh function parameters passing?
# 1  
Old 06-13-2012
Why double quotation marks doesn't work in ksh function parameters passing?

I'm working on AIX 6, ksh shell. The parameters are some strings quotated by double quotation marks which from a file. They are quotated because there may be spaces in them.
Example:
Code:
 "015607" "10" "    " "A"

I want to pass these parameters to a shell function by writing the following command:
Code:
 parse $(cat para.txt)

But I didn't get 4 parameters as thought before, instead, there are 5 parameters!

OK, it's hard to say that problem clearly. the script is:

Code:
#!/bin/ksh
#################################
#
function parse {
    echo "[$1]"
    shift
    echo "[$1]"
    shift
    echo "[$1]"
    shift
    echo "[$1]"
    shift
    echo "[$1]"
    shift
}

echo write parameters directly
parse "009085" "              " "0405103025    " "0912" "0"
echo get parameters from file
parse $(cat para.txt)
####################################

The results is:
Code:
write parameters directly
[009085]
[              ]
[0405103025    ]
[0912]
[0]
get parameters from file
["009085"]
["]
["]
["0405103025]
["]

Why is that? What's the differents between these 2 ways of function call?
Is there a way to solve this problem?
Thanks for all information on it.

Last edited by Scott; 06-13-2012 at 05:16 AM.. Reason: Code tags
# 2  
Old 06-13-2012
Interesting...

As far as I can guess, when you directly pass parameters to the function using quotes to delimit them, the shell will consider the parameter list without the quotes but with the embedded white-spaces...

Code:
set -x

parse "009085" " " "0405103025 " "0912" "0"

+ parse 009085   0405103025  0912 0
[009085]
[ ]
[0405103025 ]
[0912]
[0]

But, when you get the argument list from a file using command substitution, the shell will do nothing more about it. It will use the current IFS value to decide the positional parameters.

Code:
parse "$(cat para.txt)"

+ cat para.txt
+ parse "009085" " " "0405103025 " "0912" "0"
["009085"]
["]
["]
["0405103025]
["]

# 3  
Old 06-13-2012
The issue is the order of events. We need to delay the Shell processing the parse line until after the cat has run.
Code:
#!/bin/ksh
#################################
#
function parse {
    echo "[$1]"
    shift
    echo "[$1]"
    shift
    echo "[$1]"
    shift
    echo "[$1]"
    shift
    echo "[$1]"
    shift
}

echo write parameters directly
parse "009085" "              " "0405103025    " "0912" "0"
echo get parameters from file
eval parse "$(cat para.txt)"

./scriptname
write parameters directly
[009085]
[              ]
[0405103025    ]
[0912]
[0]
get parameters from file
[009085]
[              ]
[0405103025    ]
[0912]
[0]


Contents of para.txt
"009085" "              " "0405103025    " "0912" "0"


Last edited by methyl; 06-13-2012 at 09:19 AM.. Reason: bad paste
# 4  
Old 06-14-2012
Thanks very much!

It's amazing...
I found there is so much knowledge need to learn.
Thanks a lot!
This User Gave Thanks to Shimmey For This Post:
# 5  
Old 06-14-2012
Though it is a solution to your problem, I don't recall ever putting an eval command in a production script.
There is always a better design like formatting your parameter file such that it is valid Shell syntax and can be executed in the current environment.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[Bash] passing variables to executable doesn't work

Bash version 4.4.20 / Ubuntu 16.0.4 Hello, I tried to write a script that gathers some data and passes them to an executable. The executed application answers with an error. The echo output in the script returns correct values. If I copy/paste the last echo command, it get's executed... (2 Replies)
Discussion started by: sushi2k7
2 Replies

2. Shell Programming and Scripting

Multiple carriage returns within quotation marks causing new lines in csv

There is a closed thread called "carriage returns within quotation marks causing new lines in csv" that I am unable to post to, so I am starting a new thread. The awk solution worked perfectly in most cases. We have some cases where there are multiple carriage returns within a single quoted... (9 Replies)
Discussion started by: Mary Roberts
9 Replies

3. Shell Programming and Scripting

Function doesn't work

Hello, and here's my problem: I can't get my function to do what I want. When I call my function get_from_A_to_F I give it an argument $remainder. I want my function to substitute a number higher than 9 to a specific letter. If the argument is equal to 10 than it should change it to "A".... (8 Replies)
Discussion started by: linas
8 Replies

4. Shell Programming and Scripting

carriage returns within quotation marks causing new lines in csv

I have a csv file with 3 columns. Fields are comma delimited and strings are enclosed with quotation marks "". About 40% of the time, the line of values will start a new line thanks to carriage return characters within a string. Example: "apple","banana","orange" "pineapple","grape","straw... (6 Replies)
Discussion started by: koeji
6 Replies

5. Shell Programming and Scripting

Passing parameters to bash script function (or subroutine)

I've found a few posts regarding passing parameters to a function or subroutine, but for some reason when I try to run a command based on part with these parameters it's not working. If I have the function echo the parameters they show correctly so I believe they are being passed right but the... (2 Replies)
Discussion started by: withanh
2 Replies

6. Shell Programming and Scripting

Count number of character occurence but not from quotation marks

I have the following string: 31-01-2012, 09:42:37;OK;94727132638;"Mozilla/5.0 (Linux; U; Android 2.2.1)";3G;WAP;I need a script which is counting the occurrence of semicolons ( ; ) but exclude the ones from the quotation marks. In the string given as example there are 8 semicolons but the script... (3 Replies)
Discussion started by: calinlicj
3 Replies

7. Shell Programming and Scripting

Passing the parameters using a function

Hi All, I am new to shell scripting required some help in passing the parameter value to the shell script. I am writing a shell script, in the script I have created two functions as below. first function get_trend_ids () { Here I am connecting to the database and getting all the... (3 Replies)
Discussion started by: shruthidwh
3 Replies

8. Shell Programming and Scripting

export variable from ksh script doesn't work

Hi there, in a script I have #!/usr/bin/ksh TEST=hello export TEST Problem is, that the variable doesn't get exported. I also tried typeset -x TEST=hello When I put the two lines in my .profile, the variable is set fine. Whats could be the problem here? (4 Replies)
Discussion started by: doc_symbiosis
4 Replies

9. Shell Programming and Scripting

passing values to function in Ksh

Hi, I'm trying to work on the script given below #!/bin/ksh -x pfile() { echo "$1" } touch smp19 echo "Hi" > smp19 result=$(pfile $smp19) echo $result As highlighted , when i pass $smp19 as parameter ,it does not display the output.However when i try giving "Hi" instead... (2 Replies)
Discussion started by: Sheema
2 Replies

10. Shell Programming and Scripting

enclose a line with quotation marks

i have a file like this aaaa bbbb cccc aaa aaaa aa cccccccccccccccc aaaaaaa aaaa aaaa i want to enclose this lines with double quotation: "aaaa bbbb cccc" "aaa aaaa" "aa cccccccccccccccc" "aaaaaaa aaaa aaaa" any idea? (preferably without using sed) thanks in advance... (3 Replies)
Discussion started by: gfhgfnhhn
3 Replies
Login or Register to Ask a Question