views:

302

answers:

4

I want to create a extremely simple bash script, (my_copy.sh), that reads arbitrary number input files, a destination directory, and finally asks for confirmation if you want to perform the copy.

Example usage: ./my_copy.sh

 Type in the file names to copy:
 file1 file2 Anna Kurt Arne
 Type in the directory to copy to:
 dir_3
 Are you sure you want to copy the files:
 Anna
 Kurt
 Arne
 to the directory dir_3 (y/n)?

If the destination directory does not exists it should be created by the script.

and my next question:

I want the * character to do a simple ls command. So if I type ./my_copy * , in the command line it should list all files in my directory.

+1  A: 

Your second question is quite difficult. The shell will attempt to interpret * and replace it with all items in the current directory. The only way the shell will give you a * as the only entry in the argument list is if all the files in the directory have names starting with a dot. So, your example command would actually get called with $0 = my_copy and $1 = my_copy.

Matt Kane
+4  A: 

Unless the * is escaped or quoted when calling your script, the shell will expand it before you script gets it.

./my_copy '*'

or

./my_copy \*

It looks like you're trying to add a simple confirmation wrapper around 'cp'. Or are you trying to make it interactively prompt the user?

Ryan Graham
Some shells do in fact use an environment variable to indicate which parameters are a result of wildcard expansion. However, assuming you know which shell the user has might not be the wisest of ideas.
Joshua
+4  A: 

You could use "cp -i", which makes it interactive and prompt before overwriting. You could also add that as an alias to .bash_profile, so it always prompts.

sfossen
A: 

You cannot give * as argument, but you can replace it with letter "e" for example and do this:

if [ "$1" = "e" ]
then
    ls
else
    return 0
fi
maxorq