Bash script variable = variable1
I am new to scripting but I can cut and paste and there are a lot of examples out there.
I have a script that I need to know what day today is, this month and last month. I have figured that all out but I need to do an if statement and if today is 01 the make this month last month. I run a script at midnight so the video name needs to be put in a folder for the correct month.
This what I have
LASTMONTH=$(date -d "-1 month" +%B)
if [ $THISDAY = "01" ]
then [ $THISMONTH = $LASTMONTH ] <--- this is not working $THISMONTH still = $THISMONTH
echo "THISMONTH = LASTMONTH"
Can you give me the correct syntax for then [ $THISMONTH = $LASTMONTH ]
Thank you it works fine now. :)
Please use [code][/code] tags around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.
If I may make a suggestion, clean, consistent formatting makes code readable and more easily debuggable. Indent all your sub-commands, and separate logical sections with whitespace. Add comments anywhere the code isn't completely obvious (and remember, what seems obvious to you now will not be a year or so down the line).
Since environment variables are generally all upper-case, it's good practice to keep your own user variables in lower-case or mixed-case, to help differentiate them.
I also personally recommend putting your then and do keywords on the same line as your if/for/while/until keywords. it helps keep the clutter down and visually separate the sub-commands.
I would format the code you posted above (with fix) like this:
I also suggest getting into the habit of quoting ALL of your variable substitutions. You should never leave the quotes off a variable expansion unless you explicitly want the resulting string to be word-split by the shell.
This is particularly important when using the old "[" test, which can break if it sees the wrong number of arguments. The newer "[[" test keyword isn't as vulnerable to this kind of thing, and has other benefits as well, so you should really be using it for your bash scripts instead anyway.
|All times are GMT -5. The time now is 08:51 PM.|