Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 02-27-2012, 05:47 PM   #1
Registered: Aug 2005
Location: Apr-Oct Enderby, BC Canada Oct-Mar Somewhere warm
Distribution: Debian 6 and 7
Posts: 46

Rep: Reputation: 0
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

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

Old 02-27-2012, 06:09 PM   #2
Senior Member
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 243Reputation: 243Reputation: 243
if [[ $THISDAY = "01" ]]
1 members found this post helpful.
Old 02-27-2012, 11:52 PM   #3
Registered: Aug 2005
Location: Apr-Oct Enderby, BC Canada Oct-Mar Somewhere warm
Distribution: Debian 6 and 7
Posts: 46

Original Poster
Rep: Reputation: 0
Thank you it works fine now.

Last edited by dingo-den; 02-28-2012 at 12:13 AM.
Old 02-28-2012, 10:33 AM   #4
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,837

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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:

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

	echo "$thismonth = $lastmonth"


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.


bash scripting

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] bash: how ls content of a variable in bash script porphyry5 Programming 6 07-28-2011 01:10 PM
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 04:11 AM
bash script: use the directory of the script file as variable? phling Linux - Newbie 12 01-16-2010 07:16 PM
Problem with bash script - variable name within variable name steven.c.banks Linux - Newbie 3 03-10-2009 03:08 AM
Bash Script Help - Trying to create a variable inside script when run. webaccounts Linux - Newbie 1 06-09-2008 02:40 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:11 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration