Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I wrote a function in bash which gets * as input and therefore sould list all files in that particualar dircectory. but doesn't. Here is what i wrote:

    # a function that mass deletes files in a directory but asks before deleting
    yrm()
    {
    echo "The following files are going to be deleted:"
    ls "${1}"
    read -e -n 1 -p "Do you want to delete these files? Y/n" ANSWER
    ${ANSWER:="n"} 2> /dev/null 

    if [ $ANSWER == "Y" ]
        then
            rm $1
        else
            echo "aborted by user"

    fi

}

However I tested it with these files:

l1zard@Marvin:~/.rclocal/test$ ls *
test1.txt  test2.txt  test3.txt  test5.txt  test7.txt  test8.txt  test9.txt  testm7m767.txt

and I get this output from my function:

l1zard@Marvin:~/.rclocal/test$ yrm *
The following files are going to be deleted:
test1.txt
Do you want to delete these files? Y/nn
aborted by user

How can I fix it so it lists the files as expected?

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

try something like:

ls "$@"
read ...
if [ "${ANSWER:=n}" = Y ]
then
  rm "$@"

But you would also need to test if files were specified and if they exist..

share|improve this answer
    
Yeah it works with $@. Thanks man. –  l1zard Jul 13 '13 at 20:38
    
Also i was able to shorten the funtoion a little bit: –  l1zard Jul 13 '13 at 20:43
add comment

The function now looks like this and it does work thanks to Scrutinizer:

# a function that mass deletes files in a directory but asks before realy delting those
yrm()
{
echo "The following files are going to be deleted:"
ls "$@"
rm -rI "$@"
}
share|improve this answer
1  
The reason this is the answer is that the function does not receive * as a parameter: the shell expands the asterisk before the function is invoked. See the expansions section of the bash manual. –  glenn jackman Jul 14 '13 at 1:58
    
Well this is kind of awkward but i did know about $@ and $* but did not consider using it. Thanks again for all your help. –  l1zard Jul 14 '13 at 9:37
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.