Operating System - HP-UX
1827699 Members
3248 Online
109967 Solutions
New Discussion

Re: Need Help for C shell variables assigment from file

 
Rick Jones_2
Occasional Advisor

Need Help for C shell variables assigment from file

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 "names read to vars from file"
echo "${src_file}"
echo "${new_file}"
echo "mv command fails"
mv "${src_file}" "{new_file}"
@ f_cnt++
end
echo "names in filelist"
cat /tmp/filelist
------------------------

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
3 REPLIES 3
Vitek Pepas
Valued Contributor

Re: Need Help for C shell variables assigment from file

There is missing $ in mv line.
It should be
mv "${src_file}" "${new_file}"
Pete Randall
Outstanding Contributor

Re: Need Help for C shell variables assigment from file

This question has already been solved. See:

http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=319408


Pete

Pete
Rick Jones_2
Occasional Advisor

Re: Need Help for C shell variables assigment from file

Thanks Everyone for your responses

Rod gave me the answer by using:

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

instead of what I was using and now the

var keeps the multipule spaces between words.

Thank again Rod