LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Help me fix my "day of the week" script (https://www.linuxquestions.org/questions/linux-newbie-8/help-me-fix-my-day-of-the-week-script-738948/)

mattp 07-09-2009 12:01 PM

Help me fix my "day of the week" script
 
I need a script that will tell me the day of the week for a file's modified date.

I have 2 lines of code, one that will tell me the day of the week for a specified date. The other will give me the file's last date modified.

See below:

date +%A --date='Jul 9'

and

ls -lart file.txt | cut -d" " -f6,7,8

What I need is the 2nd statements output to go into the --date= param of the first statement.

I found that you are supposed to use the `` marks to encapsulate a command to use its results as a parameter but when I do so I get errors. The errors seem to be coming from the " " (the deliminator for CUT).

Can any one help?

Edited for accuracy.

tredegar 07-09-2009 12:13 PM

Take a look at this:
Code:

tred@vaio:~$ touch file.txt
tred@vaio:~$ ls -lart file.txt
-rw-r--r-- 1 tred tred 0 2009-07-09 18:10 file.txt
tred@vaio:~$ ls -lart file.txt | cut -d" " -f6,7,8
2009-07-09 18:10 file.txt
tred@vaio:~$ ls -lart file.txt | cut -d" " -f6
2009-07-09
tred@vaio:~$ date +%A --date=2009-07-09
Thursday
tred@vaio:~$ date +%A --date=`ls -lart file.txt | cut -d" " -f6`
Thursday
tred@vaio:~$ date +%A --date=$(ls -lart file.txt | cut -d" " -f6)
Thursday
tred@vaio:~$

Backticks are considered "old-fashioned", but $(command) does the same and causes less confusion with normal ticks.

mattp 07-09-2009 12:23 PM

Thanks for the reply.

When I run those commands (the last two any way), I get:

date: invalid date `Jul'

When I modify the -f param of cut to be -f6,7,8 I get:

date: extra operand `8'

My ls -lart return the date as:
Jul 8 23:56

not like yours (2009-07-09 18:10)

Thanks.

tredegar 07-09-2009 12:51 PM

I don't think you need the art parameters for the ls command for this to work, only the l:
Code:

tred@vaio:~$ ls -l file.txt | cut -d" " -f6
2009-07-09
tred@vaio:~$

I don't know why yours is saying "Jul" - mine doesn't.

What shell are you running? Mine's bash
Code:

tred@vaio:~$ echo $SHELL
/bin/bash
tred@vaio:~$

Do you have some environment variable set so you receive non-standard dates? I don't, as far as I can see:
Code:

tred@vaio:~$ env
KDE_MULTIHEAD=false
SSH_AGENT_PID=6160
DM_CONTROL=/var/run/xdmctl
GPG_AGENT_INFO=/tmp/gpg-rQd3og/S.gpg-agent:6161:1
TERM=xterm
SHELL=/bin/bash
XDG_SESSION_COOKIE=deb427e5c4291d76f3ebfa1c484ab3d5-1247157784.845634-158166308
XDM_MANAGED=/var/run/xdmctl/xdmctl-:0,maysd,mayfn,sched,rsvd,method=classic
GTK2_RC_FILES=/home/tred/.gtkrc-2.0-kde:/home/tred/.kde/share/config/gtkrc-2.0
GTK_RC_FILES=/etc/gtk/gtkrc:/home/tred/.gtkrc:/home/tred/.kde/share/config/gtkrc
GS_LIB=/home/tred/.fonts
WINDOWID=56623111
KDE_FULL_SESSION=true
USER=tred
LS_COLORS=no=00:fi=00:di=01;34:ln=01;36:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.svgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.lzma=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.rar=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:
SSH_AUTH_SOCK=/tmp/ssh-XfbcBQ6063/agent.6063
SESSION_MANAGER=local/vaio:/tmp/.ICE-unix/6210
KONSOLE_DCOP=DCOPRef(konsole-13499,konsole)
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
DESKTOP_SESSION=default
KONSOLE_DCOP_SESSION=DCOPRef(konsole-13499,session-1)
PWD=/home/tred
KDE_SESSION_UID=
LANG=en_GB.UTF-8
HISTCONTROL=ignoreboth
HOME=/home/tred
SHLVL=1
XCURSOR_THEME=DMZ-White
LOGNAME=tred
LESSOPEN=| /usr/bin/lesspipe %s
DISPLAY=:0.0
LESSCLOSE=/usr/bin/lesspipe %s %s
COLORTERM=
_=/usr/bin/env
tred@vaio:~$

And check this from man ls
Code:

−−time−style=STYLE
      with −l, show times using style STYLE: full−iso, long−iso, iso, locale, +FORMAT. FORMAT is
      interpreted like ‘date’; if FORMAT is FORMAT1<newline>FORMAT2, FORMAT1 applies to
      non−recent files and FORMAT2 to recent files; if STYLE is prefixed with ‘posix−’, STYLE takes
      effect only outside the POSIX locale

[EDIT:]
Have you an alias for ls that is giving you this behaviour?
Code:

tred@vaio:~$ alias
alias ls='ls --color=auto'
tred@vaio:~$

[/EDIT]

Tinkster 07-09-2009 01:16 PM

Quote:

Originally Posted by mattp (Post 3602416)
Thanks for the reply.

When I run those commands (the last two any way), I get:

date: invalid date `Jul'

When I modify the -f param of cut to be -f6,7,8 I get:

date: extra operand `8'

My ls -lart return the date as:
Jul 8 23:56

not like yours (2009-07-09 18:10)

Thanks.


The format in which ls displays dates is largely tied to
the locale you're using. You can get "sensible" date formats
by forcing them:

ls -l --time-style=long-iso
will yield the date display in Tredegars example (and basically
will on all machines I ever had my hands on - the concept of having
Month and Day for dates less than six months away and a different format
including the year for older ones is just mentally crippled and sick).



Cheers,
Tink

mattp 07-09-2009 02:01 PM

Awesome! I got it sorted out using both of your tips. Sure enough I did have an alias for "LS". For anyone else with this issue, the command I ended up with is:

date +%A --date=$(ls -l file.txt --time-style=long-iso | cut -d" " -f6)

Thanks to both of you!!!

norobro 07-09-2009 02:22 PM

Won't the following command give you the same result?
Code:

$: date +%A -r file.txt
Thursday


mattp 07-09-2009 03:31 PM

I like it! Simple. It seems I over-complicated it unnecessarily, however I did get to learn about the $() syntax.

mattp 07-09-2009 03:34 PM

Any idea why the following is producing a syntax error?

#!/bin/bash

if [ date +%A -r ingram* = "Wednesday" ]; then
echo "HURAH"
else
echo "NO GOOD"
fi

The error is
: command not found
./testif: line 7: syntax error near unexpected token `fi'
./testif: line 7: `fi'

Tinkster 07-09-2009 03:52 PM

You need to wrap the command up ... and I don't think you can have several reference files
Code:

#!/bin/bash
if [ "$(date +%A -r my_file)" = "Wednesday" ]; then
  echo "HURAH"
else
  echo "NO GOOD"
fi


mattp 07-09-2009 04:29 PM

Thanks! That "*" is only because depending on the day, the file will have a different suffix. There will only be 1 file.

norobro 07-09-2009 04:32 PM

There's probably a way to get the "-r" option of "date" to do expansion but I couldn't get it to work.

How about something like this:
Code:

#!/bin/bash

FILES=$(\ls ingram*)

for filename in $FILES
do
if [ "$(date +%A -r $filename)" = "Wednesday" ]; then
  echo "HURAH"
else
  echo "NO GOOD"
fi
done


mattp 07-13-2009 09:23 AM

I get the same syntax error, this is a copy/paste of what I am using:

#!/bin/bash

if [ "$(date +%A -r my_file)" = "Wednesday" ]; then
echo "HURAH";
else
echo "NO GOOD";
fi

The error is
: command not found
./testif: line 7: syntax error near unexpected token `fi'
./testif: line 7: `fi'

mattp 07-13-2009 02:53 PM

I had to run dos2unix. Problem solved. Thanks!


All times are GMT -5. The time now is 04:12 PM.