Commands take arguments: grep -c ERROR register.log. So can your scripts. Inside the script, they are numbered variables:
learner@reef:~$ cat need.sh
#!/bin/bash
# Usage: need.sh ITEM BAGS ← tells the next reader what to give, in which order
item="$1" ← name the arguments at the top, in quotes
bags="$2"
echo "buy $bags bags of $item"
learner@reef:~$ ./need.sh flour 3
buy 3 bags of flour
learner@reef:~$ ./need.sh "brown sugar" 2
buy 2 bags of brown sugar ← the quotes made one argument| Variable | Holds |
|---|---|
$1, $2, … $9 | The first, second, … ninth argument. One you didn't give is empty |
$# | How many arguments there are |
-
Write
~/count.sh, with#!/bin/bashon top. It takes a word and a file, and writes the word, a colon, and how many lines of the file have the word:textlearner@reef:~$ ./count.sh ERROR ~/logs/register.log ERROR: 3 -
Make it runnable with
chmod u+x ~/count.sh, and run it as above. Then try another word:./count.sh WARN ~/logs/register.log. -
Write
~/args.shthe same way. It says how many arguments it got, and the first one:textlearner@reef:~$ ./args.sh "east side" mon tue 3 arguments, the first is east side
Objectives
./count.sh ERROR ~/logs/register.logwritesERROR: 3.- It works for another word.
- Write
~/args.sh, which says how many arguments it got and what the first one is.