Reputation:
I want to create a extremely simple bash script, my_copy.sh
, that reads arbitrary number of input files, a destination directory, and finally asks for confirmation if you want to copy the files.
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 exist, it should be created by the script.
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.
Upvotes: 1
Views: 1224
Reputation: 4792
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
Upvotes: 0
Reputation: 4778
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.
Upvotes: 4
Reputation: 8229
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?
Upvotes: 4
Reputation: 13862
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.
Upvotes: 1