Operating System - HP-UX
1824988 Members
2345 Online
109678 Solutions
New Discussion юеВ

Re: for loop + shell scripting

 
network_4
Advisor

for loop + shell scripting

Hi

Issue is i have a list of files and have to delete all files one by one but i am not able to use for loop. Actually there is one directory where some files like a1, a2 a3 will be there and we have to move files on the basis of lowest seqence file first. So let me know how we can do this.
6 REPLIES 6
Steven E. Protter
Exalted Contributor

Re: for loop + shell scripting

Shalom,

How about a while loop

ls -1 > list

filetonotdelete=nodelete.dat

while read -r filename
do
if [ "$filename" != "$filetonotdelete" ]
then
echo "Not deleting $filetonotdelete"
else
echo "Deleting $filename"
rm -f $filename
fi
done < list

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
SANTOSH S. MHASKAR
Trusted Contributor

Re: for loop + shell scripting

Hi,

Give details of file sequence & Some examples.

Steven Schweda
Honored Contributor

Re: for loop + shell scripting

This is not a clear description, but if you
want to keep "a3", you might use something
like this:

$ cat pur.sh
#!/bin/sh

ls -1 $1 | \
(
read c
while [ -n "$c" ] ; do
p="$c"
read c
if [ -n "$c" ] ; then
rm "$p"
fi
done
)

./pur.sh "a*"

But that would break when you got up to
"a10".

Until you can say exactly what you would like
to do, there is not much hope of figuring out
how to do it. After that, it's a lot like
writing a computer program.
Dennis Handly
Acclaimed Contributor

Re: for loop + shell scripting

>Issue is i have a list of files and have to delete all files one by one

If you know what files you want to save, remove them from your list. Then to remove the rest with:
$ rm -f $( < list-of-files )

(This assumes they fit within the shell's command line.)
network_4
Advisor

Re: for loop + shell scripting

Thread Closed
network_4
Advisor

Re: for loop + shell scripting

Closed