Session Ready
Exercise

How can I pass filenames to scripts?

A script that processes specific files is useful as a record of what you did, but one that allows you to process any files you want is more useful. To support this, you can use the special expression [email protected] (dollar sign immediately followed by at-sign) to mean "all of the command-line parameters given to the script".

For example, if unique-lines.sh contains sort [email protected] | uniq, when you run:

bash unique-lines.sh seasonal/summer.csv

the shell replaces [email protected] with seasonal/summer.csv and processes one file. If you run this:

bash unique-lines.sh seasonal/summer.csv seasonal/autumn.csv

it processes two data files, and so on.

As a reminder, to save what you have written in Nano, type Ctrl + O to write the file out, then Enter to confirm the filename, then Ctrl + X to exit the editor.

Instructions 1/2
undefined XP
  • 1

    Edit the script count-records.sh with Nano and fill in the two ____ placeholders with [email protected] and -l (the letter) respectively so that it counts the number of lines in one or more files, excluding the first line of each.

    • 2

      Run count-records.sh on seasonal/*.csv and redirect the output to num-records.out using >.