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
# 1  
Old 06-15-2009
Problem if parameter has space in it using loop

Code:
for cmdopts in $*;do
 case $cmdopts in 
     -mode) 
  mode="$2";shift 2
     ;;
     -server) 
  server="$2";shift 2
     ;;
     -Id) 
  Id="$2";shift 2
     ;; 
     -passwd) 
  passwd="$2";shift 2
     ;;
     -rmtDir) 
  rmtDir="$2";shift 2
     ;; 
     -lcDir) 
  lcDir="$2";shift 2
     ;; 
     -file) 
  file="$2";shift 2;;
 esac
    done

statements... using above parameters.

This works fine if i pass
rmtDir='test/abc'
when i pass rmtDir='test/ghi jkl'
It complains connot find 'test/ghi" as it is looking of test/ghi rather than "test/ghi jkl"
The problem comes with the space in the directory path.
How do i handle this.

Help is appreciated.
# 2  
Old 06-15-2009
you can solve this problem easily :

only put "" for the files separated by space:

e.g:- /test/"gg hh"

in this case the system will saw one file named as "gg hh"
BR


Smilie
# 3  
Old 06-15-2009
Quote:
Originally Posted by ahmad.diab
you can solve this problem easily :

only put "" for the files separated by space:

e.g:- /test/"gg hh"

in this case the system will saw one file named as "gg hh"
BR

Smilie

I tried this it doesnt work.

The "test/ghi jkl" is the directory path where "ghi jkl is also a direcotry name.

Help is appreciated.
# 4  
Old 06-15-2009
Code:
let me discuss this issue in more details:-

each parameter (in bash shell) 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 if not quoted so you have to quote the variable to prevent the separation from occurring.

so the solution for you problem will be as follows"-

rmtDir=/what/ever/path/test/"ghi jkl" .... in here "ghi jkl" is one word

to test it do (to expand the variable):

cat (or cd if it is a directory) "${rmtDir}"

is it clear now?

BR


Last edited by ahmad.diab; 06-15-2009 at 02:33 PM..
# 5  
Old 06-15-2009

Code:
for cmdopts in "$@";do

# 6  
Old 06-15-2009
Quote:
Originally Posted by cfajohnson

Code:
for cmdopts in "$@";do

Code:
also for cmdopts in "$*";do will work for the reason I said in previous quote
which is using "" in a var is necessary if there is spaces in this variable.
BR

# 7  
Old 06-15-2009
Quote:
Originally Posted by ahmad.diab
Code:
also for cmdopts in "$*";do will work for the reason I said in previous quote
which is using "" in a var is necessary if there is spaces in this variable.
BR


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

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