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. |
Take a look at this:
Code:
tred@vaio:~$ touch file.txt |
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. |
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 What shell are you running? Mine's bash Code:
tred@vaio:~$ echo $SHELL Code:
tred@vaio:~$ env Code:
−−time−style=STYLE Have you an alias for ls that is giving you this behaviour? Code:
tred@vaio:~$ alias |
Quote:
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 |
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!!! |
Won't the following command give you the same result?
Code:
$: date +%A -r file.txt |
I like it! Simple. It seems I over-complicated it unnecessarily, however I did get to learn about the $() syntax.
|
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' |
You need to wrap the command up ... and I don't think you can have several reference files
Code:
#!/bin/bash |
Thanks! That "*" is only because depending on the day, the file will have a different suffix. There will only be 1 file.
|
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 |
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' |
I had to run dos2unix. Problem solved. Thanks!
|
All times are GMT -5. The time now is 04:12 PM. |