LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 02-27-2012, 05:47 PM   #1
dingo-den
Member
 
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
echo "THISMONTH = LASTMONTH"
fi

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

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

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
if [[ $THISDAY = "01" ]]
then 
  THISMONTH=$LASTMONTH
  echo "THISMONTH = LASTMONTH"
fi
 
1 members found this post helpful.
Old 02-27-2012, 11:52 PM   #3
dingo-den
Member
 
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: Arch + Xfce
Posts: 6,852

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


Reply

Tags
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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:08 PM.

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