Passing space separated value to a function - error
Taking inputs in the script which is space separated and passing this to a function and i have assigned like below
And then when I use for loop for the inputs i got from the user, it is taking only the first argument.
this is printing always the first name .. here as Bala
Pl help me how to get all values in the function.
Last edited by vgersh99; 09-30-2009 at 10:14 AM..
Reason: code tags, PLEASE!
To keep the forums high quality for all users, please take the time to format your posts correctly.
First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)
Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.
Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.
The reason is that <space> is a special character to the shell: it is the "field separator". If you feed a command (and a shell function in this regard is similar to a command) some arguments they get plucked from the commandline one by one and the shell supposes them to be separated by blanks:
"command" will suppose "arg1" to be the first argument, "arg2" to be the second, and so on. If it takes only 2 arguments it will simply ignore the rest . this is what has happened to your other names.
What to do if you want to pass an argument which contains space characters? This is where quoting comes in.
Now "command" will suppose "arg1 arg2" to be the first argument and "arg3" to be the second.
No let us examine your script: with the line
you feed the function namesCheck 3 arguments, which it gladly receives. The first line in namesCheck() is
which means: take the first argument and store it in $myname. Exactly this is what the shell does - it stores the first argument passed - the first name - into $myname.
To solve this problem you have two possibilities: either pass all the names as one argument, then you could use the for-loop like you did. For this you will have to surround $names by double quotes when you call namesCheck():
Or you could pass the names as separate arguments, but then the function has to have provisions to deal with multiple arguments, like pravin27 mentioned.
Hello All,
I would like to ask help from you on how to pass variable value from a function that has been called inside the function. I have created below and
put the variables in " ". Is there another way I can do this? Thank you in advance.
readtasklist() {
while read -r mod ver... (1 Reply)
Hi,
I am having a file say list1 with a output like below
jun 12 18:23
may 20 18:23
Now i want to pass the above two values into for loop,I have written a script like this.
#!/bin/bash
a=`cat list1`
for i in $a
do
echo "HI $i"
done
expected output:
HI jun 12 18:23 (3 Replies)
After some thought.
I am uncomfortable issuing my professors name where, there may be unintended side effects from any negative responses/feedback. Willing to re post if I can omit school / professor publicly, but can message moderator for validation? I am here for knowledge and understanding,... (1 Reply)
Hi
I have been racking my (limited) brains to get this to work without success
I have a file output which is a list of lists - ie a single column of data that is separated by space into sub lists below - I need to both split this so that each list is in a separate column (eg tab or semicolon... (8 Replies)
Hi,
I have a large number of files which are written as csv (comma-separated values).
Does anyone know of simple sed/awk command do achieve this?
Thanks!
---------- Post updated at 10:59 AM ---------- Previous update was at 10:54 AM ----------
Guess I asked this too soon. Found the... (0 Replies)
hi All,
i am trying to pass arguments that contain space , value will be stored in variables to be used further in script , i went thru previous posting , still its not clear to how to implement for my case.
passing 3 args
test.sh it is 'fun to work in unix'
inside shell
... (3 Replies)
How do I loop thru space separated values in a variable?
I hate to use very complicated counter increment logic for this kind of simple problem.
Expected result(using ksh)
$>echo "aaa bbbb cccc" | <looping code here>
var=aaa
var=bbbb
var=cccc
$>echo "aaa bbbb cccc" | while IFS=" "... (12 Replies)
Hi,
I've been trying to use grep to find out all the files which have two particular patterns in it (both pattern1 AND pattern2). I have a script to do the same, in which I'm getting the output of the first grep (with -l option) which contains the list of file paths and I'm trying to search for... (3 Replies)
Hi ,
I have three funcions f1, f2 and f3 .
f1 calls f2 and f2 calls f3 .
I have a global variable "period" which i want to pass to f3 .
Can i pass the variable directly in the definition of f3 ?
Pls help .
sars (4 Replies)
I need to pass a parameter to a function in a script. My parameter is a string. When I display the parameter within my function, I only get the first word from string I pass in.
How can I make the function receive the whole string (and not terminate at the first space it encounters)?.
part of... (2 Replies)