Concepts:
Extraction of some statistics regarding files (if
and for
statements)
Text:
Implement a bash script with the following interface:
summary.sh dir
The script must scan the content of the dir
directory and it must print a summary of the its content in a file named summary.out
.
Particularly, for each element of dir
:
10
characters (bytes) must be printed in the ./summary.out
filesummary.out
file.
An example of the summary.out
file should be:
ex: 23 elementi lez5.txt: content of example.sh: #!/bin/bas
Solution:
#!/bin/bash if [ $# -ne 1 ] ; then echo "Usage $0 <dir>" exit 1 fi if [ ! -d "$1" ] then echo "$1 is not a directory" exit 1 fi cd "$1" rm -f summary.out for i in * ; do # for i in $(ls) ; do if [ -d "$i" ] ; then echo "$i": $[`ls -l "$i" | wc -l`-1] elements >> summary.out elif [ -f "$i" ] ; then echo "$i": $(head -c 10 "$i") >> summary.out fi done exit 0