shell script assistance

shell script assistance

Post by Todd Jone » Fri, 22 May 1998 04:00:00



Gurus,

How can I use a csh foreach command so that it won't split up filenames
with spaces in them into separate entries?

e.g.

Quote:>ls -1 mydir/

thing1
thing 2
thing3
Quote:>foreach foo (`ls mydir`)

?ls -l $foo
?end
-rwxr-xr-x   thing1    toddj   blah blah
ls: file not found: thing

How can I get the expression which populates foo to consider "thing 2"
the entry and not "thing" and "2" as separate entries?

Thanks in Advance,

-Todd Jones

 
 
 

shell script assistance

Post by David Waffe » Fri, 22 May 1998 04:00:00


Sort of long winded but read Todd.

mydir/
dir1
dir  2
dir3

csh prints ls listings as single lines, but if you redirect
to a file (>/tmp/file) then you'll get lined output.

Example...
# ls /mydir
dir1/ dir 2/ someother* console.log dir3/

# ls /mydir > /tmp/file_list
dir1/
dir 2/
someother*
console.log
dir3/

IMPORTANT:  make sure ls is not aliased!  ls -F (you'll get file types)

You need to append the output to quote the
file names.  You can not use a whitespace
removal technique because that will logically
alter the filename and ls will give you
"no such file or directory"

ie..  "dir 2" becomes dir2 and that
really does not exist on the file system if you
attempt a cd or ls...blah blah blah.

If you don't want to write a executable script nor
save to a tmp file and vi adding, "file name", quotes
around each file name, then from the command line
you need to stream edit the file.

=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~

foreach dirname (`cat /mydir>/tmp/output; cat output|sed '1,$s/^/"/'|sed
'1,$s/$/"/'`)
?echo "===== $dirname ==="
?ls -l $dirname
?echo " "
?end

sed '1,$s/^/"/'  #(1)line 1,($)till end of line /^/insert at the
begining of the each line /"/ a double quote.
Similarly, /$/ /"/ says add a double quote at the end of each line. You do
not have to add a "g" (/$/ /"/g) for global!

Making your output
"dir1"
"dir 2"
"someother"
"console.log"
"dir3"

David Waffen

"I ran into a similar problem with users' home directory mailboxes.
I needed a script to return the names and it got hosed."

 
 
 

shell script assistance

Post by Dave Carriga » Sat, 23 May 1998 04:00:00


 Todd> Gurus, How can I use a csh foreach command so that it won't split
 Todd> up filenames with spaces in them into separate entries?

Don't use ls to get the filename list; just use a glob:

 belfast% ls -1    
 1file
 2file
 3file with spaces
 4file
 belfast% foreach file ( * )
 ? echo File is $file
 ? end
 File is 1file
 File is 2file
 File is 3file with spaces
 File is 4file

Better yet, s/csh/{ba,k,}sh/, but that's a story for another day.

--

Sr. Technical Analyst, Technology Services
Interprovincial Pipe Line Inc.

 
 
 

shell script assistance

Post by David Waffe » Sat, 23 May 1998 04:00:00


I meant ls not cat in my original.

foreach filename (ls /mydir>/tmp/output; cat output|sed '1,$s/^/"/'|sed
'1,$s/$/"/'`)

 
 
 

shell script assistance

Post by Paul Hagyar » Tue, 26 May 1998 04:00:00


Hi Todd,

At a guess, use
ls -l "$foo"

Good luck!

Cheers,

Paul



Quote:> >foreach foo (`ls mydir`)
> ?ls -l $foo
> ?end
> -rwxr-xr-x   thing1    toddj   blah blah
> ls: file not found: thing

> How can I get the expression which populates foo to consider "thing 2"
> the entry and not "thing" and "2" as separate entries?

 
 
 

1. Assistance in Korn shell script

Hi there,

Perhaps someone could offer me some help.  I wrote a very small script
as follows:

#!/usr/bin/ksh
count=0
while [[ $count < 200 ]]
do
        RunThisProgram
        sleep 2
        kill %1
        ((count+=1))
done

The purpose of this script is to execute the program 'RunThisProgram'
for 2 seconds and kill it.  This exercise is iterated 200 times.

The difficulty I encounterd when running this script is that once the
program 'RunThisProgram' is executed, it is not killed.  In short, when
the script has completed, there will be 200 instances of
'RunThisProgram' running in the system.

Could you tell if there is something wrong with the script ?

Thanks in advance,
Wai

2. Linux Disks Locks Up on StarMax

3. Shell Scripting / Unix Admin Assistance Required

4. HTTP PUT

5. handling SIGINT in shell scripts when executing another shell script.

6. unix.o: undefined reference HELP!!!

7. Q: How can I have a shell script call another shell script...

8. SMC2602 PCI Wireless NIC

9. Shell script invoking other shell scripts

10. Convert Bash shell script to Korn shell script

11. Shell Script Help (C-Shell Script)

12. How to pass a variable from a shell script to another shell script...

13. Shell script acting as interactive shell - $0 not script name??