[SOLVED] Bash Shell Script - Store a variable as a string not an integer
Linux - GeneralThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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?
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
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.
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).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.