0

I am trying to write a bash script which takes a file as a command line argument and appends the name of that file, the number of lines and the last modified date to the file. I am confused over how to access that file from within the bash script and how the command line arguments behave within the script.

Here's my script so far:

#!/bin/bash

filename = $1
linecount = $(wc -l $1)
lastmod = $(date -r $1)
echo "$filename $linecount $lastmod" >> $1

I think I'm doing something wrong with the $1 references. Generally confused about how to manipulate a command line argument that is a file.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
user1730099
  • 81
  • 1
  • 3
  • 6

2 Answers2

3

Remove the spaces around the equal signs. Assignments in shell scripts have to be mashed together like so:

filename=$1
linecount=$(wc -l $1)
lastmod=$(date -r $1)
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
0

The positional arguments ($1, $2, ...) are the right method. You might run into problems with special characters (like space) and escaping, but otherwise your script should run fine.

thiton
  • 35,651
  • 4
  • 70
  • 100