Search code examples
linuxshellgrepcut

How to use grep in a shell script?


  • I am trying to make a shell script which prints out the last modification dates of the following files.
  • Somehow the script just prints out an empty line
  • "modified" is a file which contains the names and the modification dates of the files in the following format(delimiter='@'):
>modified
for i in file1 file2 file3
do
    echo $i@`stat --printf='%y\n' $i`>>modified
done

Having created that file, I'm trying to search it like:

for i in file1 file2 file3
do
    var=`grep -w "$i" modified | cut -d'@' -f2`
    echo $var
done

Solution

  • As mentioned by Charles, there's no reason to create that modified file for that (unless you are planning to use that file for another purpose).

    Also, you can give different arguments to your stat command, as in:

    stat --printf='%y\n' file1 file2 file3
    

    This gives exactly the same output as what you're aiming for.