LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Bash script variable = variable1 (http://www.linuxquestions.org/questions/programming-9/bash-script-variable-%3D-variable1-931628/)

dingo-den 02-27-2012 05:47 PM

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

THISMONTH=$(date +%B)
THISDAY=$(date +%d)
LASTMONTH=$(date -d "-1 month" +%B)

if [ $THISDAY = "01" ]
then [ $THISMONTH = $LASTMONTH ] <--- this is not working $THISMONTH still = $THISMONTH
echo "THISMONTH = LASTMONTH"
fi

Can you give me the correct syntax for then [ $THISMONTH = $LASTMONTH ]

Thanks

Cedrik 02-27-2012 06:09 PM

Code:

if [[ $THISDAY = "01" ]]
then
  THISMONTH=$LASTMONTH
  echo "THISMONTH = LASTMONTH"
fi


dingo-den 02-27-2012 11:52 PM

Thank you it works fine now. :)

David the H. 02-28-2012 10:33 AM

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:

Code:

thismonth=$(date +%B)
thisday=$(date +%d)
lastmonth=$(date -d "-1 month" +%B)

#if the current day is 01, thismonth needs to be reset to lastmonth
if [[ "$thisday" = "01" ]]; then

        thismonth=$lastmonth
        echo "$thismonth = $lastmonth"

fi


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.

http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes

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.

http://mywiki.wooledge.org/BashFAQ/031


All times are GMT -5. The time now is 10:43 AM.