Created
January 17, 2013 14:33
-
-
Save vvondra/4556312 to your computer and use it in GitHub Desktop.
Zpracovani parametru v bashiu
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
adresare="" | |
cesty=true | |
while [ $# -gt 0 ] | |
do | |
# nacteni adresaru, ktere se budou prohledavat | |
if [ ${1:0:1} != "-" ] ; then | |
if $cesty ; then | |
adresare="$adresare $1" | |
shift | |
continue | |
else | |
echo "Cesty musi predchazet filtrum" | |
exit 1 | |
fi | |
fi | |
# nacteni filtru | |
cesty=false | |
case $1 in | |
-name) | |
shift | |
if [ -n "$1" ]; then | |
name=$1 | |
else | |
echo "Chybi parametr pro \"-name\"" | |
exit 1 | |
fi | |
;; | |
-iname) | |
shift | |
if [ -n "$1" ]; then | |
iname=$1 | |
else | |
echo "Chybi parametr pro \"-iname\"" | |
exit 1 | |
fi | |
;; | |
-type) | |
shift | |
# detekce typu, v pripade ze neznam typ souboru, konec | |
case $1 in | |
f|d|l) | |
type=$1 | |
;; | |
*) | |
echo "Neznamy nebo chybejici typ souboru \"$1\", povolene je f, d, l." | |
exit 1 | |
;; | |
esac | |
;; | |
*) | |
echo "Neznamy filtr \"$1\", povolene jsou -name, -iname a -type" | |
exit 1 | |
;; | |
esac | |
shift | |
done | |
iname=`echo "$iname" | tr [:upper:] [:lower:]` | |
# pokud nebyl v parametrech adresar, vychozi je soucasny | |
if [ ! -n "$adresare" ] ; then | |
adresare="." | |
fi | |
# kontrola existence adresaru v parametrech | |
for file in $adresare | |
do | |
if [ ! -d "$file" ] ; then | |
echo "Neexistujici adresar $file" | |
exit 1 | |
fi | |
done | |
ls -R1 $adresare | while read line | |
do | |
if [[ "$line" == total* ]] ; then | |
continue | |
fi | |
if [ ! -n "$line" ] ; then | |
readpath=true | |
continue; | |
fi | |
if $readpath ; then | |
currentpath=${line/:/} | |
readpath=false | |
continue | |
fi | |
match=true | |
# nyni uz mame jednotlive soubory a muzeme je otestovat, jestli vyhovuji nasemu findu | |
if [ -n "$name" ] ; then | |
if [[ $line != $name ]] ; then | |
match=false | |
fi | |
fi | |
if [ -n "$iname" ] ; then | |
if [[ `echo "$line" | tr [:upper:] [:lower:]` != $iname ]]; then | |
match=false | |
fi | |
fi | |
if [ -n "$type" ] ; then | |
case $type in | |
f) | |
if [ ! -f "$currentpath/$line" ] ; then | |
match=false | |
fi | |
;; | |
d) | |
if [ ! -d "$currentpath/$line" ] ; then | |
match=false | |
fi | |
;; | |
l) | |
if [ ! -h "$currentpath/$line" ] ; then | |
match=false | |
fi | |
;; | |
esac | |
fi | |
if $match ; then | |
echo "$currentpath/$line" | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment