Operating System - HP-UX
1752815 Members
6246 Online
108789 Solutions
New Discussion юеВ

Question relating to c shell and the use of spaces in vars

 
SOLVED
Go to solution
Rick Jones_2
Occasional Advisor

Question relating to c shell and the use of spaces in vars

Hi Everyone,

I was hoping that someone might know of a way to handle this using C shell.

Basically I'm trying to write a csh script that will look into a directory on a hpux machine that is a samba share. I want to do a number of things to the files in this directory in a batch mode from this script.

I first "ls" and redirect the listing to a file, I then read the file 1 line at a time and assign the files' filename to a var for use in later opperations.

Here is my problem, this directory hold many files from windows based systems and many have spaces in their names (which the scipt can deal with) however some files have multipule spaces in a row within the filename and when the name is assigned to the var, it loses any multipule space no matter how I try to quote it or what utility I use to read the filelist I.E. sed,awk etc..

Here is a little script that will illistrate the problem.
1) it will create a "test" dir under /tmp
2) it will touch 2 files with multipule spaces in there names.
3)it will then put the names into a list which
contains the right filenames with the spaces.
4) then it will try to assign 1 filename at a time from the list to a var.

You will then see that the var has the filename but with only 1 space where there were multipules.

Here is the scripts text
-------------------------
#!/bin/csh -f
cd /tmp
mkdir test
cd test
touch "test file with three spaces"
touch "test file with four spaces"

set f_cnt = 1
set f_max = 0
ls -1A > /tmp/filelist
@ f_max = `cat /tmp/filelist | wc -l`
while (${f_cnt} <= ${f_max})
set src_file = `sed -n "${f_cnt}p" /tmp/filelist`
if (${#src_file} == 0) exit
set new_file = "${src_file}.new"
echo "${src_file}"
echo "${new_file}"
mv "${src_file}" "{new_file}"
@ f_cnt++
------------------------

HELP !!!

If someone could please show me how to read one line at a time from a file and assign it to a var in c shell and have the var maintain the multipule spaces so that the var can then be used to "mv" "del" or whatever on the file from a script.

Thanks

Rick
10 REPLIES 10
John Palmer
Honored Contributor

Re: Question relating to c shell and the use of spaces in vars

Sorry Rick, have never used the 'C' shell. However you can do it in Korn/Posix shell as follows:

#!/usr/bin/sh
cd /tmp
mkdir test
cd test
touch "testfile with three spaces"
touch "test file with four spaces"

ls | {
while read X
do
print "$X"
mv "$X" "$X.new"
done
}

Regards,
John
Rick Jones_2
Occasional Advisor

Re: Question relating to c shell and the use of spaces in vars

Thanks for the answer John but

I really need to do this in "c" shell so that it can be added to some other "c" shell stuff.

Thanks
Rick
Rick Jones_2
Occasional Advisor

Re: Question relating to c shell and the use of spaces in vars

Everyone,

It lokks like when I pasted the test script
on to the board I lost the "end" and the bottom of the script. It also lost the multipule spaces in the "touch" command.

It should be
"touch test{3spaces here}file with three spaces"
and
"touch test{4spaces here}file with four
spaces"

Sorry for the bad info

Rick

Rodney Hills
Honored Contributor
Solution

Re: Question relating to c shell and the use of spaces in vars

Rather than using "set" use "setenv".

setenv src_file "`sed -n '${f_cnt}p' /tmp/filelist`"

This will leave the spaces alone...

(by the way- I don't know csh, but it seems odd it doesn't have an equivalent to the "read" command under ksh?)

HTH

-- Rod Hills
There be dragons...
Rodney Hills
Honored Contributor

Re: Question relating to c shell and the use of spaces in vars

Rick,

I've noticed in your other postings you haven't responded to any of the solutions recommended by members of this forum.

If any solution works (even partially), please post that back to your original message. Thus if someone else has the same problem, they can benefit from your original request. Assigning points is also a nice way to say how well a solution worked ;-)

My 2 cents

-- Rod Hills
There be dragons...
Rick Jones_2
Occasional Advisor

Re: Question relating to c shell and the use of spaces in vars

Thanks Rod,

It works great Thanks

Gregory Fruth
Esteemed Contributor

Re: Question relating to c shell and the use of spaces in vars

Step 1: Using a large rancid fish, slap
anybody who tells you to write a csh script.
Csh is an awful programming language.

Step 2: Ask why this routine must be in csh.
You can easily call scripts in other (more
reasonable) languages from csh.

Step 3: If you really must use csh, you
might be better off trying the following
approach:

set dir = "the dir in question"
pushd "$dir" > /dev/null
foreach file (*)
echo "$file"
end
popd > /dev/null

The variable "dir" is the directory
containing the files. pushd/popd are
used to chdir into/out from that directory
so that the "*" wildcard will work properly.
The /dev/null is used to discard the
diagnostic output from pushd/popd. Where
I have used "echo" you can do the appropriate
processing. Note that the current directory
will be "dir" while you are inside the
pushd/popd.

Step 4: Assign points where appropriate.
Rick Jones_2
Occasional Advisor

Re: Question relating to c shell and the use of spaces in vars

Greg,

Your right about your first statememts but in
this case I need to modify a old fairly complex script and I rather not re-write it or if possible not have it call other scripts.

Rod got me an answer to my problem.


The csh solution you provided will not work
because the filenames that are within the
list have 1 or multipule space in the names and when they get assigned to the var they become multipule entries (elements) of the var.

I.E.
set test = "rttrtr{3 space here}ytyty"

if you echo "${test}"

it will look like == rttrtr{1 space here}ytyty

and the various text of the filename that is surrounded by whitespace will become seperate elements in your "for" loop.

The "mv" or other command will fail because the source filename will be wrong because it won't have the same amount of spaces in it.


Thank for the suggestion

Rick
Gregory Fruth
Esteemed Contributor

Re: Question relating to c shell and the use of spaces in vars

Hmmm, the script (attached) seems to work
fine when I run it, even when there are
multiple spaces in the file names.

The "*" operator seems to be expanding
filenames verbatim, while using
set foo = "..." chops out the extra spaces.