0

From my workbook, a function to display filename created on the user specified date:

function lsd
{
     date=$1
     ls -l | grep -i "^.\{42\}$date" | cut -c55-
 }

The workbook notes the columns that need to be specified will probably differ on different terminals, operating systems. When I use ls -l in my terminal, the first character for the date starts at column 35 (in DD Month format) and the first character for the filename column starts at 48. So, what I wrote:

1 #!/bin/bash
2 
3 lsd () 
4 { 
5   date=$1
6   ls -l | grep -i "^.\{35\}$date" | cut -c48- 
7 }

If I do

8 $lsd 

and run the script like this:

./lsd "22 aug"

I get a list of all the filenames in the directory no matter what their creation date is. Not what I want. I don't know why this happens.

If I change the function call line (someone suggested I do this) to

8 lsd $1

and enter ./lsd "22 aug" in terminal again, I get a list of all the filenames that were created on the 22 day of any month, not just August.

I don't know why 8 lsd brings up all the file names. I don't know why changing that to 8 lsd $1 brings up only the files created on the 22nd day irrespective of specified month. I'm not sure what I'm doing wrong here, or if there is some mistake in the work book or it is outdated.

  • 2
    Do you want really want parse `ls` output like this? There could be better ways to do this. See https://stackoverflow.com/questions/158044/how-to-use-find-to-search-for-files-created-on-a-specific-date. – Nishant Nov 17 '21 at 09:42
  • 1
    Ok, thank you. I'll try to find a solution to the problem with a method other than the one in the book. I think there's something wrong with the grep portion. – user16711485 Nov 17 '21 at 09:44
  • 1
    A couple of notes: 1.) If you know the output looks different depending on what the OS is, it might benefit you to wrap that information in an `if` statement (e.g. if [[ `uname -s` == 'Darwin' ]]; then echo "parse it this way" else echo "otherwise do this or output a message saying it's not supported" fi 2. When you call `lsd`, you're not passing a value to be set for date variable. When you call `lsd $1` (and pass "22 aug") your grep call is using the value of 22 in its call – ThatsWhatSheCoded Nov 17 '21 at 09:54
  • 2
    I'd recommend adding the command `set -x` at the beginning of the script (right after the shebang line), to make bash print an execution trace as it runs; this'll give you a better idea what's actually being executed (although I should warn that it tends to quote things a little weirdly, for example, the pattern arg to `grep` will almost certainly be printed in single-quotes because it contains funny characters). Also, use [shellcheck.net](https://www.shellcheck.net/) to find common mistakes, like not double-quoting `$1`. – Gordon Davisson Nov 17 '21 at 10:57
  • Thanks everyone, I'll try these suggestions. – user16711485 Nov 17 '21 at 11:30
  • First you should double-quote all parameter expansions (`date="$1"`, `lsd "$1"`). Else, if the parameter contains spaces, the bash word splitting will interfere with the expected behavior. Second, if the first date character is at column 35 your should use `grep -i "^.\{34\}$date"`, not `grep -i "^.\{35\}$date"`. – Renaud Pacalet Nov 17 '21 at 11:33
  • Do you want to _specifically filter ls output_ or do you want to find all files modified at 22 day of august? Is this XY question? `of any month, not just August.` Where do you write `8 lsd $1` and what is that file format? I guess do `lsd "$1"` – KamilCuk Nov 17 '21 at 11:37
  • [Why you shouldn't parse the output of `ls`](https://mywiki.wooledge.org/ParsingLs) – Charles Duffy Nov 17 '21 at 11:47
  • 1
    Time to get another workbook; this one is obviously full of nonsense. (1) Do not parse `ls` output, ever. (2) Use `local` variables in functions. (3) Use locale-independent date formats (e.g. epoch or one of the ISO standards) when passing date or time arguments. (4) Never rely on an exact number of whitespace characters in text. – Andrej Podzimek Nov 17 '21 at 20:45
  • I had a feeling this book I'm using is not that great. For the record it's O'Reilly media's "Learning the bash shell", C Newham, 3rd ed. – user16711485 Nov 17 '21 at 22:29
  • I called function by using lsd "$1" and it worked. Thanks everyone for the help and each of the suggestions regarding why doing it the way the book lays out isn't good, this has all been supremely helpful. Very much appreciated for everyone's input. – user16711485 Nov 17 '21 at 22:32

0 Answers0