Write a shell script, named grepdir.sh, that searches for a pattern in a directo
ID: 3688390 • Letter: W
Question
Write a shell script, named grepdir.sh, that searches for a pattern in a directory, and all of its subdirectories. The starting directory is the first argument, the pattern is the second parameter, and the options for grep are all succeeding parameter(s). Options will start with a hyphen. The script should produce a usage statement if the script is misused.
[ssdavis@lect1 private]$ ls cpdirs.sh grepdir.sh makemake.sh temp temp2 temp3 uncomp.sh
[ssdavis@lect1 private]$ grepdir.sh cpdirs.sh bin usage: grepdir.sh directory pattern[-grep option]*
[ssdavis@lect1 private]$ grepdir.sh . usage: grepdir.sh directory pattern [-grep option]*
[ssdavis@lect1 private]$ grepdir.sh cpdirs.sh bin usage: grepdir.sh directory pattern [-grep option]* [ssdavis@lect1 private]$ grepdir.sh . usage: grepdir.sh directory pattern [-grep option]*
[ssdavis@lect1 private]$ grepdir.sh . bin #! /bin/bash #! /bin/bash #! /bin/bash bin in file 1 #! /pkg/bin/bash
[ssdavis@lect1 private]$ grepdir.sh . bin -l ./grepdir.sh ./cpdirs.sh ./makemake.sh ./temp2/1 ./uncomp.sh
[ssdavis@lect1 private]$ grepdir.sh . BIN -l
[ssdavis@lect1 private]$ grepdir.sh . BIN -li ./grepdir.sh ./cpdirs.sh ./makemake.sh ./temp2/1 ./uncomp.sh
[ssdavis@lect1 private]$ grepdir.sh . BIN li usage: grepdir.sh directory pattern [-grep option]*
[ssdavis@lect1 private]$ grepdir.sh . BIN -n -i 1:#! /bin/bash 1:#! /bin/bash 1:#! /bin/bash 1:bin in file 1 1:#! /pkg/bin/bash
[ssdavis@lect1 private]$
Explanation / Answer
[rong@home private]$ ls
cpdirs.sh grepdir.sh makemake.sh temp temp2 temp3 uncomp.sh [davis@lect1 private]$ grepdir.sh cpdirs.sh bin
usage: grepdir.sh directory pattern [-grep option]*
[rong@home private]$ grepdir.sh .
usage: grepdir.sh directory pattern [-grep option]*
[rong@home private]$ grepdir.sh cpdirs.sh bin
usage: grepdir.sh directory pattern [-grep option]*
[rong@home private]$ grepdir.sh .
usage: grepdir.sh directory pattern [-grep option]*
[rong@home private]$ grepdir.sh . bin
#! /bin/bash
#! /bin/bash
#! /bin/bash
bin in file 1
#! /pkg/bin/bash
[rong@home private]$ grepdir.sh . bin -l
./grepdir.sh
./cpdirs.sh
./makemake.sh
./temp2/1
./uncomp.sh
[rong@home private]$ grepdir.sh . BIN -l
[rong@home private]$ grepdir.sh . BIN -li
./grepdir.sh
./cpdirs.sh
./makemake.sh
./temp2/1
./uncomp.sh
[rong@home private]$ grepdir.sh . BIN li
usage: grepdir.sh directory pattern [-grep option]*
[rong@home private]$ grepdir.sh . BIN -n -i
1:#! /bin/bash
1:#! /bin/bash
1:#! /bin/bash
1:bin in file 1
1:#! /pkg/bin/bash
[rong@home private]$
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.