bash - Shell programming, lopping thorugh files -


i trying loop through files in specified directory. can't seem figure out logic. can help? looping through each file , asking if want delete file.

#!/bin/bash dirpath=$1 y=y y=y echo "changing directory '$dirpath' `cd $dirpath`"  f in $1/* ##################################### if test -f `ls -1 $1` echo -n "remove file '$f' `ls -1` ?" read answer ########################## if test $answer = $y || test $answer = $y    echo "processing $f file..."   echo `rm $f`     echo "file '$f' deleted " else echo "file '$f' not removed"    fi#2nd if loop ############################   else echo 'not file'   fi#1st if loop #######################################  done 

there's no need ls, have filename. change this:

if test -f `ls -1 $1` 

to:

if test -f "$f" 

why using echo , backticks here? change

echo `rm $f` 

to:

rm "$f" 

here's place you're using backticks unnecessarily. change this:

echo "changing directory '$dirpath' `cd $dirpath`" 

to:

echo "changing directory '$dirpath'" cd "$dirpath" 

always quote variables contain filenames.


Comments

Popular posts from this blog

Add email recipient to all new Trac tickets -

400 Bad Request on Apache/PHP AddHandler wrapper -

php - Change action and image src url's with jQuery -