- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Question relating to c shell and the use of sp...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-19-2003 02:58 AM
тАО12-19-2003 02:58 AM
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
Solved! Go to Solution.
- Tags:
- csh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-19-2003 03:23 AM
тАО12-19-2003 03:23 AM
Re: Question relating to c shell and the use of spaces in vars
#!/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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-19-2003 03:39 AM
тАО12-19-2003 03:39 AM
Re: Question relating to c shell and the use of spaces in vars
I really need to do this in "c" shell so that it can be added to some other "c" shell stuff.
Thanks
Rick
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-19-2003 03:45 AM
тАО12-19-2003 03:45 AM
Re: Question relating to c shell and the use of spaces in vars
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-19-2003 05:28 AM
тАО12-19-2003 05:28 AM
Solutionsetenv 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-19-2003 05:35 AM
тАО12-19-2003 05:35 AM
Re: Question relating to c shell and the use of spaces in vars
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-19-2003 05:56 AM
тАО12-19-2003 05:56 AM
Re: Question relating to c shell and the use of spaces in vars
It works great Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-19-2003 06:18 AM
тАО12-19-2003 06:18 AM
Re: Question relating to c shell and the use of spaces in vars
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.
- Tags:
- scummy C shell
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-19-2003 07:17 AM
тАО12-19-2003 07:17 AM
Re: Question relating to c shell and the use of spaces in vars
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-19-2003 08:20 AM
тАО12-19-2003 08:20 AM
Re: Question relating to c shell and the use of spaces in vars
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-19-2003 08:30 AM
тАО12-19-2003 08:30 AM
Re: Question relating to c shell and the use of spaces in vars
set test = "foo____bar"
(where "_" is a space) then 'echo $test'
prints:
foo_bar
while 'echo "$test"' prints:
foo____bar
The difference is in the "echo" command,
not the "set" command. 'echo $test'
expands to 'echo foo____bar', which is
'echo' followed by 2 arguments 'foo' and
'bar'. On the other hand, 'echo "$test"'
expands to 'echo' followed by 1 argument
'foo____bar'.