LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 09-12-2012, 07:02 AM   #1
RML1992
LQ Newbie
 
Registered: Jun 2012
Posts: 16

Rep: Reputation: Disabled
Bash Shell Script - Store a variable as a string not an integer


Is it possible to store a variable as a string rather than an integer? For instance a " date +'%d%m%y' " so as to keep the leading zero and avoid the "value is too great for base" error when the value begins in either 08 or 09.
 
Old 09-12-2012, 07:16 AM   #2
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 647

Rep: Reputation: 189Reputation: 189
bash already considers all variables as strings, not ints.
If you're missing leading zeroes then it's likely you're using some kind of operator which translates the string from the "date" command into an int.
 
Old 09-12-2012, 07:23 AM   #3
kabamaru
Member
 
Registered: Dec 2011
Location: Greece
Distribution: Slackware
Posts: 276

Rep: Reputation: 134Reputation: 134
You might want to have a look at this. Bash sees the leading zeros and thinks this is an octal value. That's why 08 and 09 are "too great for base".

Also see this about Bash variables.

Last edited by kabamaru; 09-12-2012 at 07:25 AM.
 
Old 09-12-2012, 07:58 AM   #4
RML1992
LQ Newbie
 
Registered: Jun 2012
Posts: 16

Original Poster
Rep: Reputation: Disabled
Thank you very much for your replies!

I am setting the variable as follows:

backupDate=$(date +'%d%m%y');

Does it automatically convert it into a string? As it still seems to bring up the same error that it is 'too great for base'. I have also tried emitting the leading zero and then adding it back onto the front of a separate variable calling in the first to follow, with no such luck, the same error is produced.

Is it at all possible for it to not be recognised as Octal so that the leading zero will remain?
 
Old 09-12-2012, 08:10 AM   #5
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
That works for me, what system are you on?
 
Old 09-12-2012, 08:28 AM   #6
RML1992
LQ Newbie
 
Registered: Jun 2012
Posts: 16

Original Poster
Rep: Reputation: Disabled
Thanks!

The system I'm running is Cent 5.6. Currently the script reads as follows:

#!/bin/bash

dow=$(date +'%a');

function determineBackup {
if [ "$dow" == "Wed" ]; then
let backupDate=$(date +'%d%m%y' -d "3 days ago");
else
let backupDate=$(date +'%d%m%y' -d "1 day ago");
fi
}

determineBackup;

echo Backup Date: "$backupDate";

exit

As 3 (and 4) days ago are 08 or 09 then it kicks up the error 'too great for base'.

Last edited by RML1992; 09-12-2012 at 08:40 AM. Reason: wrong
 
Old 09-12-2012, 08:41 AM   #7
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 647

Rep: Reputation: 189Reputation: 189
Why are you using let?
Simply define the backupDate variable in a global scope (right after dow, for example) and you'll be able to access it both from inside determineBackup and outside of it:
Code:
#!/bin/bash

dow=$(date +'%a')
backupDate=""

function determineBackup {
if [ "$dow" = "Wed" ]; then
	backupDate=$(date +'%d%m%y' -d "3 days ago")
else
	backupDate=$(date +'%d%m%y' -d "1 day ago")
fi
}

determineBackup

echo Backup Date: "$backupDate"
Please use code tags ([code]insert code here[/code]) to post source code or commands output, as it makes it more readable.
Note that all those ";" I removed are useless, as the exit at the end of the script with no value.
 
Old 09-12-2012, 08:44 AM   #8
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Quote:
Originally Posted by 414N View Post
Why are you using let?
Simply define the backupDate variable in a global scope (right after dow, for example) and you'll be able to access it both from inside determineBackup and outside of it:
Code:
#!/bin/bash

dow=$(date +'%a')
backupDate=""

function determineBackup {
if [ "$dow" = "Wed" ]; then
	backupDate=$(date +'%d%m%y' -d "3 days ago")
else
	backupDate=$(date +'%d%m%y' -d "1 day ago")
fi
}

determineBackup

echo Backup Date: "$backupDate"
Please use code tags ([code]insert code here[/code]) to post source code or commands output, as it makes it more readable.
Note that all those ";" I removed are useless, as the exit at the end of the script with no value.
Correct. The problem is not the leading 0 in your date output, the problem is your syntax. Get rid of the let's and the useless semicolons and it should work (does for me).
 
Old 09-12-2012, 09:19 AM   #9
RML1992
LQ Newbie
 
Registered: Jun 2012
Posts: 16

Original Poster
Rep: Reputation: Disabled
Thank you for your help! Works brilliantly now
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Cannot read text from text file and store it in a variable using shell script anurupr Linux - Newbie 2 03-03-2010 01:38 PM
converting string in integer in bash script dziadgba Linux - Newbie 5 08-31-2009 05:59 PM
how to get integer part from string in Shell Script sunilvadranapu Programming 4 11-02-2008 04:51 AM
Need shell script to concatenate a string and a variable into a directory name AwesomeMachine Linux - Newbie 2 05-07-2006 03:42 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:59 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