Shell Programming and Scripting

BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Problem with gstat and octal value

👤 Login to reply

 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-21-2018
newbie2010 newbie2010 is offline
Registered User
 
Join Date: Nov 2012
Last Activity: 25 June 2018, 3:28 PM EDT
Posts: 204
Thanks: 88
Thanked 12 Times in 2 Posts
Problem with gstat and octal value

Here are a few lines from my script. The problem I am having is that the statement for gstat returns this error:

Code:
line 43: [[: 08: value too great for base (error token is "08")

The statement is coming from gstat.
Is there a way to fix it? Apparently -eq 02 is coming up as some octal value, I need it to be recognized as 02.

Apparently in this case the script still works but the error appears, not sure how to get rid of it.

Code:
if [[ "$file" =~ [A-Z] && $CDATE = "Jan" ]]; then jx1="01-January";
#jx1="03-Mar";
yeardir=$CYEAR;
mv "$file" /tmp/Move\ Lists/$dir/$yeardir/$jx1;
elif [[ "$file" =~ [A-Z] && $CDATE = "Feb" && $(/opt/csw/bin/gstat "$file" |grep Mod|awk '{print $2}'|cut -b 6,7) -eq 02 ]];
then jx1="02-February";
yeardir=$CYEAR;
mv "$file" /tmp/Move\ Lists/$dir/$yeardir/$jx1;


Moderator's Comments:
Problem with gstat and octal value Please use CODE tags as required by forum rules!

Last edited by RudiC; 03-21-2018 at 06:48 PM.. Reason: Added / Changed CODE tags.
Sponsored Links
    #2  
Old 03-21-2018
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 19 July 2018, 7:29 AM EDT
Location: Aachen, Germany
Posts: 13,075
Thanks: 449
Thanked 4,016 Times in 3,693 Posts
It's always recommended to mention your OS' and shell's versions; that keeps people from guessing.
Assuming bash. Look into its man page:
Quote:
ARITHMETIC EVALUATION
. . .
Constants with a leading 0 are interpreted as octal numbers. . . . Otherwise, numbers take the form [base#]n . . .
So - use a 10# prefix to eliminate your problem.

Some asides:
- the closing fi is missing.
- yeardir could be assigned outside the if construct.
- mv "$file" /tmp/Move\ Lists/$dir/$yeardir/$jx1; could be executed outside as well.
- "$file" =~ [A-Z] is tested twice - unnecessarily.
- grep Mod|awk '{print $2}'|cut -b 6,7 could be condensed into one single awk command.
The Following User Says Thank You to RudiC For This Useful Post:
jim mcnamara (03-21-2018)
Sponsored Links
    #3  
Old 03-22-2018
newbie2010 newbie2010 is offline
Registered User
 
Join Date: Nov 2012
Last Activity: 25 June 2018, 3:28 PM EDT
Posts: 204
Thanks: 88
Thanked 12 Times in 2 Posts
So should it then look like this?

Code:
if [[ "$file" =~ [A-Z] && $CDATE = "Feb" && $(/opt/csw/bin/gstat "$file" |grep Mod|awk '{print $2}'|cut -b 6,7) -eq 10#02

I believe this probably isn't what you meant?

Moderator's Comments:
Problem with gstat and octal value Please use CODE tags as required by forum rules!

Last edited by RudiC; 03-22-2018 at 11:29 AM.. Reason: Added CODE tags.
    #4  
Old 03-22-2018
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 19 July 2018, 7:29 AM EDT
Location: Aachen, Germany
Posts: 13,075
Thanks: 449
Thanked 4,016 Times in 3,693 Posts
You are right, this

Quote:
Originally Posted by newbie2010 View Post
. . .
Code:
if [[ "$file" =~ [A-Z] && $CDATE = "Feb" && $(/opt/csw/bin/gstat "$file" |grep Mod|awk '{print $2}'|cut -b 6,7) -eq 10#02

.
.
.
is not what I meant. Try (untested!):
Code:
if [[ "$file" =~ [A-Z] && $CDATE = "Feb" &&  $(/opt/csw/bin/gstat "$file" |awk '/Mod/ {print "10#" substr ($2, 6, 2}') -eq 02 ]] . . .

The 02 is a valid octal number, while the "command substitution" 's result might not be. Another option might be to suppress the leading 0 by printing 0 + substr (...).
Sponsored Links
    #5  
Old 03-22-2018
apmcd47 apmcd47 is offline
Registered User
 
Join Date: Feb 2011
Last Activity: 19 July 2018, 3:36 AM EDT
Posts: 360
Thanks: 16
Thanked 102 Times in 95 Posts
Step back a minute. Is gstat Gnu stat from the OpenCSW packages for Solaris?

Try this convolution instead of the one you were using (original post):

Code:
elif [[ "$file" =~ [A-Z] && $CDATE = "Feb" && $(/opt/csw/bin/gdate +%_m --date="$(/opt/csw/bin/gstat --format=%y "$file")") -eq 02 ]];

This way you get only the file modification time out of gstat (removes the need for both grep and awk); then feed it into gdate which then pops out the month. gdate +%_m also replaces leading zeroes with leading spaces which should get rid of your 08 isn't an octal number problem.

The $(..."$(..."..."...)"...) should look after themselves.

Andrew

PS This is why you should say what OS, etc, you are using.
Sponsored Links
👤 Login to reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
syntax for IF test or AWK for octal sboxtops Shell Programming and Scripting 3 03-03-2012 12:05 PM
Deleting octal values vino.paal UNIX for Dummies Questions & Answers 5 06-14-2011 11:57 PM
Problem with simple octal to decimal code qf_woodfox Homework & Coursework Questions 3 04-07-2011 12:04 PM
[bash]printf octal instead of decimal dolphin06 Shell Programming and Scripting 3 04-14-2009 06:38 AM
octal and strings strider Programming 1 03-06-2006 09:17 AM



All times are GMT -4. The time now is 03:25 PM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
×
UNIX.COM Login
Username:
Password:  
Show Password





Not a Forum Member?
Forgot Password?