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.

3
  • 1
    What doesn't work about your current script? Commented Jan 30, 2013 at 20:03
  • 1
    there should not be white spaces around the assigment =. In other words, filename=$1 Commented Jan 30, 2013 at 20:34
  • Unless you want linecount to include the filename, use linecount=$( wc -l < $1 ) Commented Jan 31, 2013 at 9:15

2 Answers 2

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)
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.