LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-09-2009, 01:01 PM   #1
mattp
Member
 
Registered: Mar 2004
Location: Chicago, USA
Distribution: Slackware 10.2
Posts: 368

Rep: Reputation: 30
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.
 
Old 07-09-2009, 01:13 PM   #2
tredegar
LQ 5k Club
 
Registered: May 2003
Location: London, UK
Distribution: Debian "Jessie"
Posts: 6,085

Rep: Reputation: 398Reputation: 398Reputation: 398Reputation: 398
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.

Last edited by tredegar; 07-09-2009 at 01:19 PM.
 
Old 07-09-2009, 01:23 PM   #3
mattp
Member
 
Registered: Mar 2004
Location: Chicago, USA
Distribution: Slackware 10.2
Posts: 368

Original Poster
Rep: Reputation: 30
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.
 
Old 07-09-2009, 01:51 PM   #4
tredegar
LQ 5k Club
 
Registered: May 2003
Location: London, UK
Distribution: Debian "Jessie"
Posts: 6,085

Rep: Reputation: 398Reputation: 398Reputation: 398Reputation: 398
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]

Last edited by tredegar; 07-09-2009 at 01:54 PM.
 
Old 07-09-2009, 02:16 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by mattp View Post
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
 
Old 07-09-2009, 03:01 PM   #6
mattp
Member
 
Registered: Mar 2004
Location: Chicago, USA
Distribution: Slackware 10.2
Posts: 368

Original Poster
Rep: Reputation: 30
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!!!
 
Old 07-09-2009, 03:22 PM   #7
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 517

Rep: Reputation: 177Reputation: 177
Won't the following command give you the same result?
Code:
$: date +%A -r file.txt
Thursday
 
Old 07-09-2009, 04:31 PM   #8
mattp
Member
 
Registered: Mar 2004
Location: Chicago, USA
Distribution: Slackware 10.2
Posts: 368

Original Poster
Rep: Reputation: 30
I like it! Simple. It seems I over-complicated it unnecessarily, however I did get to learn about the $() syntax.
 
Old 07-09-2009, 04:34 PM   #9
mattp
Member
 
Registered: Mar 2004
Location: Chicago, USA
Distribution: Slackware 10.2
Posts: 368

Original Poster
Rep: Reputation: 30
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'
 
Old 07-09-2009, 04:52 PM   #10
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
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
 
Old 07-09-2009, 05:29 PM   #11
mattp
Member
 
Registered: Mar 2004
Location: Chicago, USA
Distribution: Slackware 10.2
Posts: 368

Original Poster
Rep: Reputation: 30
Thanks! That "*" is only because depending on the day, the file will have a different suffix. There will only be 1 file.
 
Old 07-09-2009, 05:32 PM   #12
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 517

Rep: Reputation: 177Reputation: 177
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

Last edited by norobro; 07-09-2009 at 07:43 PM. Reason: Got rid of back ticks
 
Old 07-13-2009, 10:23 AM   #13
mattp
Member
 
Registered: Mar 2004
Location: Chicago, USA
Distribution: Slackware 10.2
Posts: 368

Original Poster
Rep: Reputation: 30
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'

Last edited by mattp; 07-13-2009 at 11:26 AM. Reason: Reposting the error so people don't have to find it in the thread.
 
Old 07-13-2009, 03:53 PM   #14
mattp
Member
 
Registered: Mar 2004
Location: Chicago, USA
Distribution: Slackware 10.2
Posts: 368

Original Poster
Rep: Reputation: 30
I had to run dos2unix. Problem solved. Thanks!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 7 12-19-2015 02:03 AM
Shell Script: Find "Word" Run "Command" granatica Linux - Software 5 07-25-2007 08:42 AM
How to write a bash script to replace all "KH" to "K" in file ABC??? cqmyg5 Slackware 4 07-24-2007 10:00 AM
Can't install "glibmm" library. "configure" script can't find "sigc++-2.0&q kornerr Linux - General 4 05-10-2005 03:32 PM
Monthly Archiving Script... help with "date" & "cron" Supp0rtLinux Linux - Software 3 01-03-2003 10:29 PM


All times are GMT -5. The time now is 04:40 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration