Passing space separated value to a function - error


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Passing space separated value to a function - error
# 1  
Old 09-30-2009
Question 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.
Code:
Enter Names : Bala Sundar Sridhar
read names
namesCheck $names

function namesCheck() {
myname=$1

for i in myname
do
printf "\n$i"
done
}

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!
# 2  
Old 09-30-2009
Hi,
If you want to print all the arguments the use $@

function namesCheck()
{
echo $@
}
# 3  
Old 09-30-2009
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.

Thank You.

The UNIX and Linux Forums
# 4  
Old 09-30-2009
You can fix your code by putting double quotes around the variable names when calling your function
Code:
read names
namesCheck "$names"

function namesCheck() {
myname=$1

for i in $myname
do
printf "\n$i"
done
}


Last edited by BubbaJoe; 09-30-2009 at 10:28 AM..
# 5  
Old 09-30-2009
use for loop to call function namesCheck, not in function namesCheck to use for loop.

Code:
function namesCheck
{
  printf "\n$1"
}

for name in `echo "Bala Sundar Sridhar"`
do
  namesCheck $name
done

# 6  
Old 09-30-2009
Quote:
Originally Posted by balamv
how to get all values in the function
How about getopts (see lower part of the page linked) ...?
# 7  
Old 09-30-2009
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:

Code:
command arg1 arg2 arg3

"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.

Code:
command "arg1 arg2" arg3

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

Code:
namesCheck $names

you feed the function namesCheck 3 arguments, which it gladly receives. The first line in namesCheck() is

Code:
myname=$1

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():

Code:
nameCheck "$names"

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.

I hope this helps.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing variable value in a function to be used by another function

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)
Discussion started by: aderamos12
1 Replies

2. Shell Programming and Scripting

Need Help on For Loop to pass space separated value as one value

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)
Discussion started by: sumanthupar
3 Replies

3. Homework & Coursework Questions

C++ Attempting to modify this function to read from a (;) semi-colon-separated file

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)
Discussion started by: briandanielz
1 Replies

4. UNIX for Dummies Questions & Answers

How convert space separated list to matched columns?

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)
Discussion started by: Manchesterpaul
8 Replies

5. UNIX for Dummies Questions & Answers

[solved] Comma separated values to space separated

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)
Discussion started by: lost.identity
0 Replies

6. Shell Programming and Scripting

Passing arguments that contain space

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)
Discussion started by: gvkk
3 Replies

7. Shell Programming and Scripting

How to loop through space separated values?

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)
Discussion started by: kchinnam
12 Replies

8. Shell Programming and Scripting

Problem while using grep (multi-level) with the space-separated filepath.

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)
Discussion started by: NanJ
3 Replies

9. Shell Programming and Scripting

Passing global variable to a function which is called by another function

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)
Discussion started by: sars
4 Replies

10. UNIX for Advanced & Expert Users

Parameter passing in a function

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)
Discussion started by: fastgoon
2 Replies
Login or Register to Ask a Question