User Tools

Site Tools

Return to Home page

Example 1

Extraction of some statistics regarding files (if and for statements)

Implement a bash script with the following interface:

  • 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:

  • if the element is a file: the name of the file and the first 10 characters (bytes) must be printed in the ./summary.out file
  • if the element is a directory: the name of the directory and the number of files or subdirectory it contains must be printed in the summary.out file.

An example of the summary.out file should be:

ex: 23 elementi
lez5.txt: content of #!/bin/bas

if [ $# -ne 1 ] ; then
    echo "Usage $0 <dir>"
    exit 1
if [ ! -d "$1" ]
    echo "$1 is not a directory"
    exit 1
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
exit 0

If you found any error, or if you want to partecipate to the editing of this wiki, please contact: admin [at]

You can reuse, distribute or modify the content of this page, but you must cite in any document (or webpage) this url:
/web/htdocs/ · Last modified: 2020/11/26 23:18 (external edit)

Privacy Policy