I have this script which dumps a db:
cat /usr/bin/dbbkup
#!/bin/bash
mysqldump -uroot -proot db > db_$(date +%d%H%M%S).sql
Which I execute like so:
dbbkup && ls -atlr
But how can I pass an argument into this script that will prefix the db dump? E.G.
dbbkup -arg "changed name of field" && ls -atlr
Then change the script to something like:
#!/bin/bash
mysqldump -uroot -proot db > db_$(arg)_$(date +%d%H%M%S).sql
Thanks =)
Bash scripts reference parameters in order, e.g.:
becomes the variable
$1in your script.So to continue your example, to create a db dump with a custom filename based on date and with a certain prefix, your command would be:
and the contents of your script would be:
This will produce a file called
db_prefix_20120919104317.sql. I changed the date format, as you have more information, and in a directory listing you can now sort by name and also have them sorted by time.Also, I’m assuming you want this as an example of how to use bash parameters, as the parameter you are passing in has no bearing on the output created, just the filename, in which case why not hardcode the name of the file? I don’t see the use case for files with multiple names with the same (albeit updated) data.