Problem if parameter has space in it using loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem if parameter has space in it using loop
# 8  
Old 06-15-2009
Quote:
Originally Posted by cfajohnson

No, it will not work. It will lump all the arguments into a single parameter; not what the OP wants at all.

In contrast, "$@" expands to each argument as a separate parameter, keeping arguments containing spaces as distinct parameters.

See the difference with this script:
Code:
## NAME: xx.sh
echo  'Using $*:'
printf "%s\n" $* ""

echo  'Using "$*":'
printf "%s\n" "$*" ""

echo  'Using "$@":'
printf "%s\n" "$@" ""

And a sample run:

Code:
xx.sh qw er 'ty ui' op
Using $*:
qw
er
ty
ui
op

Using "$*":
qw er ty ui op

Using "$@":
qw
er
ty ui
op


Code:
Definitions : 
$* :
All of the positional parameters, seen as a single word
"$*" must be quoted.
$@ :
Same as $*, but each parameter is a quoted string, that is, the parameters are passed on intact, without
interpretation or expansion. This means, among other things, that each parameter in the argument list
is seen as a separate word.
Of course, "$@" should be quoted.





kindly see this exapmle where $*,$@ & "$@" act in same behaviour

e.g:-

#!/bin/bash
# If $IFS set, but empty,
#+ then "$*" and "$@" do not echo positional params as expected.
mecho () # Echo positional parameters.
{
echo "$1,$2,$3";
}
IFS="" # Set, but empty.
set a b c # Positional parameters.
mecho "$*" # abc,,
mecho $* # a,b,c
mecho $@ # a,b,c
mecho "$@" # a,b,c
# The behavior of $* and $@ when $IFS is empty depends
#+ on whatever Bash or sh version being run.
# It is therefore inadvisable to depend on this "feature" in a script.

conclusion :- 
1- The $@ and $* parameters differ only when between double quotes.
2- The $* and $@ parameters sometimes display inconsistent and puzzling behavior,
depending on the setting of $IFS.

# 9  
Old 06-15-2009
Quote:
Originally Posted by ahmad.diab
Definitions :
$* :
All of the positional parameters, seen as a single word

Incorrect; wordsplitting is performed on $* (it is unquoted).
Quote:
"$*" must be quoted.

It must only be quoted if you want it as a single word; if you want it as more than one word it must not be quoted.
Quote:

$@ :
Same as $*,

True.
Quote:
but each parameter is a quoted string, that is, the parameters are passed on intact,

That is not true. Each parameter is a separate parameter (not a quoted string) only if $@ is in quotes.
Quote:
without
interpretation or expansion. This means, among other things, that each parameter in the argument list
is seen as a separate word.
Of course, "$@" should be quoted.

It should be quoted if you want each parameter to be regarded as such in the script.
Quote:

kindly see this exapmle where $*,$@ & "$@" act in same behaviour

e.g:-

#!/bin/bash
# If $IFS set, but empty,
#+ then "$*" and "$@" do not echo positional params as expected.
mecho () # Echo positional parameters.
{
echo "$1,$2,$3";
}

It is much more reliable to use printf than echo if you really want to see exactly what the parameters are:

Code:
mecho() #@ Print each argument on a separate line.
{
 printf "%s\n" "$@" "" ## add a blank line after the parameters
}

Quote:
IFS="" # Set, but empty.
set a b c # Positional parameters.

You would have a much more meaningful demonstration if you had spaces in some of the parameters.
Quote:
mecho "$*" # abc,,
mecho $* # a,b,c
mecho $@ # a,b,c
mecho "$@" # a,b,c
# The behavior of $* and $@ when $IFS is empty depends
#+ on whatever Bash or sh version being run.
# It is therefore inadvisable to depend on this "feature" in a script.

conclusion :-
1- The $@ and $* parameters differ only when between double quotes.
2- The $* and $@ parameters sometimes display inconsistent and puzzling behavior,
depending on the setting of $IFS.
[/CODE]

Expressed more concisely and precisely:

"$@" always expands to each of the positional parameters as a separate argument.

"$*" always expands to a single argument comprising all the positional parameters; how they are separated depends on the value of $IFS.

$* and $@ always expand each word (not each argument) on the command line as a separate argument.

Here are the definitions according to the POSIX shell specification.

@
Expands to the positional parameters, starting from one. When the expansion occurs within double-quotes, and where field splitting (see Field Splitting ) is performed, each positional parameter shall expand as a separate field, with the provision that the expansion of the first parameter shall still be joined with the beginning part of the original word (assuming that the expanded parameter was embedded within a word), and the expansion of the last parameter shall still be joined with the last part of the original word. If there are no positional parameters, the expansion of '@' shall generate zero fields, even when '@' is double-quoted.
*
Expands to the positional parameters, starting from one. When the expansion occurs within a double-quoted string (see Double-Quotes ), it shall expand to a single field with the value of each parameter separated by the first character of the IFS variable, or by a <space> if IFS is unset. If IFS is set to a null string, this is not equivalent to unsetting it; its first character does not exist, so the parameter values are concatenated.
# 10  
Old 06-16-2009
I just want to say that :- $* , $@ & "$@" will work in the same behavior if "Input Field Seperator " IFS = ""
in conclusion:- code posted in the first place is true only if IFS="" .....


you can see this result if you test the bellow example and see the difference when changing the IFS from " " to ":" to "".
BR


Code:
Example:-



set -- "First one" "second" "third:one" "" "Fifth: :one"
# Setting the script arguments, $1, $2, etc.
echo
echo 'IFS unchanged, using "$*"'
c=0
for i in "$*" # quoted
do echo "$((c+=1)): [$i]" # This line remains the same in every instance.
# Echo args.
done
echo ---
echo 'IFS unchanged, using $*'
c=0
for i in $* # unquoted
do echo "$((c+=1)): [$i]"
done
echo ---
echo 'IFS unchanged, using "$@"'
c=0
for i in "$@"
do echo "$((c+=1)): [$i]"
done
echo ---
echo 'IFS unchanged, using $@'
c=0
for i in $@
do echo "$((c+=1)): [$i]"
done
echo ---
IFS=:
echo 'IFS=":", using "$*"'
c=0
for i in "$*"
do echo "$((c+=1)): [$i]"
done

echo 'IFS=":", using $*'
c=0
for i in $*
do echo "$((c+=1)): [$i]"
done
echo ---
var=$*
echo 'IFS=":", using "$var" (var=$*)'
c=0
for i in "$var"
do echo "$((c+=1)): [$i]"
done
echo ---
echo 'IFS=":", using $var (var=$*)'
c=0
for i in $var
do echo "$((c+=1)): [$i]"
done
echo ---
var="$*"
echo 'IFS=":", using $var (var="$*")'
c=0
for i in $var
do echo "$((c+=1)): [$i]"
done
echo ---
echo 'IFS=":", using "$var" (var="$*")'
c=0
for i in "$var"
do echo "$((c+=1)): [$i]"
done
echo ---
echo 'IFS=":", using "$@"'
c=0
for i in "$@"
do echo "$((c+=1)): [$i]"
done
echo ---
echo 'IFS=":", using $@'
c=0
for i in $@
do echo "$((c+=1)): [$i]"
done
echo ---
var=$@
echo 'IFS=":", using $var (var=$@)'
c=0
for i in $var
do echo "$((c+=1)): [$i]"
done
echo ---
echo 'IFS=":", using "$var" (var=$@)'

c=0
for i in "$var"
do echo "$((c+=1)): [$i]"
done
echo ---
var="$@"
echo 'IFS=":", using "$var" (var="$@")'
c=0
for i in "$var"
do echo "$((c+=1)): [$i]"
done
echo ---
echo 'IFS=":", using $var (var="$@")'
c=0
for i in $var
do echo "$((c+=1)): [$i]"
done
echo
# Try this script with ksh or zsh -y.
exit 0

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to pass function parameter to do loop?

Hi All, I have created one function for KSH and was running well with one parameter input since I just had to use $1 to get the parameter. Now I want to do loop for each parameter(actually filenames) . I have try to use do loop, but $i does not resolve to parameter instead it resolves to 1,... (5 Replies)
Discussion started by: mysocks
5 Replies

2. UNIX for Dummies Questions & Answers

Help on for loop with a parameter

Hi, need help to pass an paremeter to for loop script $cat tp.ksh for i in `grep $1 | cut -d "/" -f 5 | cut -d" " -f2` do fgrep $i $1 | grep 'with value' | cut -d "|" -f 2 done $tp.ksh test_data.plan_49989_2015-05-01-00-13-38.log command doesnot return the values. (5 Replies)
Discussion started by: AAHinka
5 Replies

3. Shell Programming and Scripting

Loop over awk or passing parameter

I wrote this script which works well when I manually input 55518622 and 1 but I need this script to be generic and loop over the following table awk '$4>(55518622-500000) && $4<(55518622+500000)' chr1_GEN2bim | awk 'BEGIN {min=1000000000; max=0;}; {\ if($4<min && $4 != "") min = $4; if($4>max... (8 Replies)
Discussion started by: fat
8 Replies

4. Shell Programming and Scripting

Pwd with space paths but on the parameter don't work

hi, i'm finding to solve on the parameter: for example: directory Value 1 root@value 1 > pwd /home/user/root/value 1 root@value 1 > pwd | sed 's/ /\\ /g' /home/user/root/value\ 1 root@value 1 > test="$(pwd | sed 's/ /\\ /g')" root@value 1 > echo "$test" /home/user/root/value\ 1 ... (4 Replies)
Discussion started by: gsflash80
4 Replies

5. Shell Programming and Scripting

Getting foreach to read a parameter with blank space

my program is designed to take the first parameters as extension, then the rest of the parameters as files to be searched for and, if found, modified by the extension. If not found, it prints an error. Everything is great until: ./chExt.sh 'com' 'king cobra.dat' where $file splits up the two... (2 Replies)
Discussion started by: username652719
2 Replies

6. Shell Programming and Scripting

Dot operator and space, Parameter not set

Hi, i have this script setenv.sh if then echo "is empty" fi echo "done" The following is the result when i run the script from command without and with a dot and space operator $ setenv.sh is empty done $ . setenv.sh sh: VAR_1: Parameter not set. $ It's our standard to run... (5 Replies)
Discussion started by: ysrini
5 Replies

7. Shell Programming and Scripting

nawk and space in the parameter

Hi, Could you please tell me how nawk command works when there is a asterisk <*> or space with asterisk < *> or <* > in the parameter. I am just trying to read line by line and fetch fourth parameter separated by delimiter (|). But if there is a * or < *> or <* > in the fourth parameter it... (7 Replies)
Discussion started by: nram_krishna@ya
7 Replies

8. UNIX for Dummies Questions & Answers

grep for a backslash as for loop parameter

Hello everyone, My main objective is to search for text within a file, namely a block of text where each line ends with a backslash "\". However, the block must begin with a keyword, like "loginstring". Here is an example of a file that contains a block: ############### loginstring \... (2 Replies)
Discussion started by: idlechatter
2 Replies

9. Shell Programming and Scripting

Passing Parameter containing space in between to Shell Script

Hi, I have one shell script which use two parameter however one of its parameter have space in between. eg. a.sh 20110114 b c d here b c d is one parameter I used 'b c d' but its not giving correct result. Also i tried b\c\d but this one also didnt work. Any help would be... (5 Replies)
Discussion started by: diehard
5 Replies

10. Shell Programming and Scripting

Using the counter of a for loop in command line parameter

Say I have (in psuedocode) For i=1 to 10 tar cvfb /... 5*i /junk(i) end What I mean is that I want each successive for loop to have the block size parameter be 5 times the current counter. This isn't my actual code, just a stupid example...So the question is how do I descrive that parameter... (2 Replies)
Discussion started by: jeriryan87
2 Replies
Login or Register to Ask a Question