[ale] Fwd: Bash script help

Doctor Who whodoctor at gmail.com
Wed Mar 12 09:39:04 EDT 2008


Sorry...meant to reply all.

---------- Forwarded message ----------
From: Doctor Who <whodoctor at gmail.com>
Date: Mar 12, 2008 9:38 AM
Subject: Re: [ale] Bash script help
To: Byron Jeff <byronjeff at clayton.edu>


On 3/12/08, Byron Jeff <byronjeff at clayton.edu> wrote:
> On Wed, Mar 12, 2008 at 05:50:43AM -0400, Doctor Who wrote:
> > I'm trying to return the value of when the last time the root password
> > was changed.  The script would do something like:
>
> Bash arithmetic mode makes it pretty simple to do:
>
> >
> > 1) more /etc/shadow | grep root | awk -F: '{print $3}'
>
> #Too complicated. Try:
>
> grep root /etc/shadow | cut -d: -f3
>
> #Also you'll want to capture that result into a shell variable. So:
>
> numdays=$(grep root /etc/shadow | cut -d: -f3)
>
> > 2) Take above value and add 1
> > 3) Multiple the result by 86400
>
> # Use bash arithmetic to do both
>
> numsecs=$[(numdays+1)*86400]
>
> > 4) Substitute the value above after the @ below
> > 5) date -d @XXXX +"%B %d %G"
> > 6) Give me the output value of this
>
> # Actually the date format is a bit more complicated. I found this page:
> # http://www.askdavetaylor.com/date_math_in_linux_shell_script.html
> # And the correct format for the -d is:
> # date -d "1970-01-01 $numsec sec"
> # so the final result is:
>
> date -d "1970-01-01 ${numsecs} sec" +"%B %d %G"
>
> # You can combine the arithmatic and the date command. So the final result:
>
> #!/bin/bash
> numdays=$(grep root /etc/shadow | cut -d: -f3)
> date -d "1970-01-01 $[(numdays+1)*86400] sec" +"%B %d %G"
>
> # Hope that helps
>
> BAJ
>

Thanks a lot....works well and makes sense from a scripting standpoint.


More information about the Ale mailing list