Getting foreach to read a parameter with blank space


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Getting foreach to read a parameter with blank space
# 1  
Old 04-20-2012
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 words into 'king' and 'cobra.dat' then runs them separately. I need it to read as a whole 'king cobra.dat' into $file.

I heard something about using "shift" to get it to read as a whole, but I'm not sure how to implement it.

Code:
#!/bin/csh                                                                   \

set ext="$1"
shift

echo the remaining are $*
foreach file ($*)
echo $file
if (-r "$file") then
set newName=`echo "$file" | sed 's/\.[A-Za-z0-9]*$/'".$ext"'/g'`
echo $newName
if ( "$file" == "$newName" ) then
:
else
mv "$file" "$newName"
endif
end
else
echo "$file": No such file
end
endif

Thanks!
# 2  
Old 04-21-2012
According to the csh man page (csh -- C Shell, a shell (command interpreter) with C-like syntax) the syntax should be:

Code:
foreach file (${*:q})

which will properly handle parameters with spaces. You are always at the mercy of the person entering the command, inasmuch as they must properly quote the parameters, but when quoted correctly on the command line this should work.

I haven't coded in csh since the early 90s, and have forgotten most of it, so this is based purely on a quick peek at the man page and one small test.
# 3  
Old 04-24-2012
agama, that code foreach file (${*:q}) would fix the issue with the spaces but what if the file extension had the special character "$" in it. For example, what if the file name was foo$e.b$l and I wanted to rewrite it to foo$e.ball using the same script...

Example: ./chExt.sh 'ball' 'foo$e.b$l'

This command would not create the file "foo$e.ball"

Any help with this? Must be one little tweek of the code somewhere.

Last edited by Marhsall34; 04-24-2012 at 10:01 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help to identify blank space in a file

Hello, I have a dictionary of over 400,000 words with the following structure source=target The database contains single words as well as phrases. To train the data, I need only mappings with out a space i.e. where both source and target do not have any space in between. I use Ultraedit as... (4 Replies)
Discussion started by: gimley
4 Replies

2. Shell Programming and Scripting

Removing blank space using VI

Hi, How to remove blank spaces using vi (I am using AIX)? #cat siva.txt AAA BBB CCC DDD EEE FFF Need to remove space between 2 columns. Regards, Siva (7 Replies)
Discussion started by: ksgnathan
7 Replies

3. Shell Programming and Scripting

Not delete space blank

Hi everyone, i need to "grep" a file with a string with space blanks, like this: grep "XXXX XX" file.txt The problem, i need put the "XXXX XX" in a string variable. When the script executes the grep, do: gresp XXXX XX file.txt How can i solve this problem? The... (5 Replies)
Discussion started by: Xedrox
5 Replies

4. Shell Programming and Scripting

grep to remove blank space

Hi All, Need help to grep blank and copy to file. I have a file in below format dns1dm06_10, dns2dm02_04, dbidub,10000000c9a46d0c gbpuhci,10000000c948b00a ibtur001,10000000c9a1ccda yubkbtp1,10000000c93fec5b I need to copy to all lines which doesn't have wwn >> no-wwn.txt 1... (2 Replies)
Discussion started by: ranjancom2000
2 Replies

5. UNIX for Dummies Questions & Answers

Replace colon with blank space

Dear Gurus, I have a unix file with multiple colons on each row, and I would like to replace each colon with a blank space using the awk command. For example, I have the following data: Data: --------- A~000000000000518000~SLP:~99991231~20090701~00102.00~USD:~CS:~... (2 Replies)
Discussion started by: chumsky
2 Replies

6. UNIX for Dummies Questions & Answers

blank space

hi everyone, i have a problem in unix script , i need to remove line that has blank , not blank line . example: mahm,,jdggkhsd,ghskj,,fshjkl can anyone help? (4 Replies)
Discussion started by: Reham.Donia
4 Replies

7. Shell Programming and Scripting

Cut last blank space

Hello, I am using this to get only directories : ls -l | grep '^d'and here is the result : drwx------ 13 so_nic sonic 13 Nov 4 13:03 GLARY drwx------ 3 so_nic sonic 3 May 6 2010 PSY2R drwx------ 15 so_nic sonic 15 Oct 14 08:47 PSYR1 But I only need to keep this... (7 Replies)
Discussion started by: Aswex
7 Replies

8. Shell Programming and Scripting

Blank space cause error when use perl

I write a script with register and login user. So, i encrypt password with encryptedpass=`perl -e "print crypt("${mypass}",salt)"` if password do not contain blank space, it work but if password have blank space, it cause error in that line the error is: syntax error at -e ..... Anyone... (3 Replies)
Discussion started by: WuZun
3 Replies

9. AIX

How can i replace a character with blank space?

i want a command for my script!!! say file consists of character 123 125 127. i need a query to replace the number 2 with 0 so the output should be 103 105 107. i use unix-aix (8 Replies)
Discussion started by: rollthecoin
8 Replies

10. Shell Programming and Scripting

append blank space

Hi, I would like to add blank space for fixed length(50) if length of string <30. Scenario: File Size AAA.CSV 123 BB.CSV 134 Expected: File Size AAA.CSV 123 BB.CSV 134 I want append blank space until 30 character. Thanks and Regards, HAA (1 Reply)
Discussion started by: HAA
1 Replies
Login or Register to Ask a Question