Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
At least on my machine...but either way, yes, it looks like an inefficient, fragile, tedious approach when a single date command could do it better.
To answer the fundamental question - commands in a BASH script are no different than commands on the command line, so why not just run them and see what they do? You'll learn much more than asking us to interpret code. For example, anything in back ticks is a command, and the output of which is stuck in its place.
So for the first command, "set -- `date`". First "date" is run, and the output is passed to "set --". So to test it, just run "date" on the command line, look at what the output is. Then run "set -- `date`". The next few lines are referencing $2, $3, and $6, clearly these are variables set up by the set command, so just print them out and see what they are. Now that you know what $6 is, run the expr command to see what it's doing and what it's assigning to the "year" variable. Finally, print out the final line to see all of it:
A long time ago, in my first (and so far only) significant foray into BASH scripting (to automate mysql dumps in this case), I set a directory name using a date.
I'm sure this is very clumsy, but it worked. I extended it to use hours and minutes because, in testing the script, I wanted to be able to run it multiple times in the same day.
Code:
#Define now as today's day-month-year-hour-minute.
NOW=$(date +%m-%d-%Y-%H-%M)
#Make the directory to hold the backup file.
mkdir /root/backups/sql/$NOW
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.