cs:bash_language:exam_20140908
Return to Home page
If you found any error, or if you want to partecipate to the editing of this wiki, please contact: admin [at] skenz.it
You can reuse, distribute or modify the content of this page, but you must cite in any document (or webpage) this url: https://www.skenz.it/cs/bash_language/exam_20140908?do=diff
no way to compare when less than two revisions
Differences
This shows you the differences between two versions of the page.
— | cs:bash_language:exam_20140908 [2024/04/08 22:35] (current) – created - external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Exam 2014/09/08 ====== | ||
+ | **Text:**\\ | ||
+ | The comman '' | ||
+ | <code bash> | ||
+ | total 56 | ||
+ | drwxr-xr-x | ||
+ | drwxrwxrwt 26 root root 20480 dic 6 18:39 .. | ||
+ | drwxr-xr-x | ||
+ | drwxr-xr-x | ||
+ | -rw-r--r-- | ||
+ | -rw-r--r-- | ||
+ | lrwxrwxrwx | ||
+ | </ | ||
+ | Write a bash script that receives in input a list of directory names in the command line, and that for each directory (given the list of its content obtained through the '' | ||
+ | * if the //directory entry// is a // | ||
+ | * if the //directory entry// is a //regular file//, it prints the name of the owner, its dimension and its name. | ||
+ | * if the //directory entry// is a //symbolic link//, it prints the creation date, its name and the name referred by the link (without the symbol '' | ||
+ | **Solution: | ||
+ | <code bash 20140908.sh> | ||
+ | #!/bin/bash | ||
+ | |||
+ | # Control if the number of parameters is correct (i.e., at least one parameter is needed) | ||
+ | if [ $# -eq 0 ] | ||
+ | then | ||
+ | echo " | ||
+ | exit 1 | ||
+ | fi | ||
+ | |||
+ | # Control that all the parameters are effectivelly directories | ||
+ | for i in $* | ||
+ | do | ||
+ | if [ ! -d $i ] | ||
+ | then | ||
+ | echo "$i is not a directly" | ||
+ | exit 2 | ||
+ | fi | ||
+ | done | ||
+ | |||
+ | |||
+ | for dir in $* # Cycle on all the directories | ||
+ | do | ||
+ | |||
+ | ls -la $dir > .tmp_$$ # Output of ls -al in the hidden file named .tmp_$$ | ||
+ | while read line #Ciclo su tutte le linee del file | ||
+ | do | ||
+ | name=$(echo $line | cut -d " " -f 9) | ||
+ | if [ " | ||
+ | then | ||
+ | continue | ||
+ | fi | ||
+ | |||
+ | f1=$(echo $line | cut -d " " -f 1) # Extraction of the first column | ||
+ | # Caso directory | ||
+ | # Si fa notare che il comando grep se fa il match almeno di un' | ||
+ | echo $f1 | grep " | ||
+ | if [ $? -eq 0 ] | ||
+ | then | ||
+ | dname=$(echo $line | cut -d " " -f 9) | ||
+ | nsubdirs=$(find $dir/$dname -type d | wc -l) | ||
+ | echo $dir/$dname is a directory having $nsubdirs subdirs | ||
+ | fi | ||
+ | |||
+ | # Caso file | ||
+ | echo $f1 | grep " | ||
+ | if [ $? -eq 0 ] | ||
+ | then | ||
+ | name=$(echo $line | cut -d " " -f 9) | ||
+ | owner=$(echo $line | cut -d " " -f 3) | ||
+ | size=$(echo $line | cut -d " " -f 5) | ||
+ | echo $name is a regular file, its owner is $owner and its size is $size bytes | ||
+ | fi | ||
+ | |||
+ | # Caso link simbolico | ||
+ | echo $f1 | grep " | ||
+ | if [ $? -eq 0 ] | ||
+ | then | ||
+ | name=$(echo $line | cut -d " " -f 9) | ||
+ | date=$(echo $line | cut -d " " -f 6,7,8) | ||
+ | reference=$(echo $line | cut -d " " -f 11) | ||
+ | echo $name is a link created on $date, it refers to $reference | ||
+ | fi | ||
+ | done < .tmp_$$ | ||
+ | done | ||
+ | |||
+ | rm .tmp_$$ | ||
+ | |||
+ | exit 0 | ||
+ | </ | ||
+ | |||
+ | **Output: | ||
+ | <code bash> | ||
+ | $ ./ | ||
+ | x/current is a directory having 1 subdirs | ||
+ | x/documents is a directory having 1 subdirs | ||
+ | .emacs is a regular file, its owner is scanzio and its size is 11887 bytes | ||
+ | examples.desktop is a regular file, its owner is scanzio and its size is 8980 bytes | ||
+ | so is a link created on dic 6 18:39, it refers to current/so | ||
+ | </ |
If you found any error, or if you want to partecipate to the editing of this wiki, please contact: admin [at] skenz.it
You can reuse, distribute or modify the content of this page, but you must cite in any document (or webpage) this url: https://www.skenz.it/cs/bash_language/exam_20140908?do=diff
/web/htdocs/www.skenz.it/home/data/pages/cs/bash_language/exam_20140908.txt · Last modified: 2024/04/08 22:35 by 127.0.0.1