Förenkling av "find"
Postat: tor jun 29, 2023 3:09 pm
I bash så är "find" ett alldeles utmärkt kommando som jag ofta använder och jag är rätt så hemma med syntax för det.
Men ibland vill men inte skriva så mycket, eller också tycker man att det är för bökigt...
Så här är en liten förenkling av det hela, som kan underlätta:
Men ibland vill men inte skriva så mycket, eller också tycker man att det är för bökigt...
Så här är en liten förenkling av det hela, som kan underlätta:
- Kod: Markera allt
#! /bin/bash
#
function usage {
cat << EOD
usage: fnd [-b] [-c] [-e] [-r] name
-b|--begin
The filename must begin with name, i.e. '^name*'
-c|--case
Search case sensitive
-e|--end
The filename must end with name, i.e. '*name$'
-r|--rec|--recurs|--recursive
Search recursive
If both -b and -e is specified, the filename must exactly match the specified name, if
none are specified, the script searches for '*name*'.
EOD
exit
}
#
#####################################################################
#
TEMP=`getopt -obcehr --long begin,case,end,help,rec,recurs,recursive -n $(basename $0) -- "$@"`
if [[ $? -ne 0 ]]; then
usage
fi
eval set -- "$TEMP"
BASE="."
MAXD=1
CASE="-iname"
BEGIN=false
END=false
RECURS=false
CMDNAME=$(basename $0)
while true; do
case $1 in
-b|--begin)
BEGIN=true
shift
;;
-c|--case)
CASE="-name"
shift
;;
-e|--end)
END=true
shift
;;
-h|--help)
usage
exit
;;
-r|--rec|--recurs|--recursive)
RECURS=true
MAXD=999
shift
;;
--)
shift
break
;;
*)
echo "-Got a star... [$1]"
exit
;;
esac
done
P1=$1
P2=$2
P3=$3
P4=$4
#
if [[ -z $P1 ]]; then
P1=""
while [[ -z $P1 ]]; do
read -p "_Name: " P1
done
fi
if [[ ! -z $P2 ]]; then
BASE="$P2"
fi
#
#####################################################################
#
if [[ $BEGIN == true && $END == true ]]; then
FND=$(find $BASE -maxdepth $MAXD $CASE $P1)
elif [[ $BEGIN == true ]]; then
FND=$(find .$BASE -maxdepth $MAXD $CASE $P1\*)
elif [[ $END == true ]]; then
FND=$(find $BASE -maxdepth $MAXD $CASE \*$P1)
else
FND=$(find $BASE -maxdepth $MAXD $CASE \*$P1\*)
fi
if [[ ! -z $FND ]]; then
echo
if [[ $BEGIN == true && $END == true ]]; then
NUM=$(find $BASE -maxdepth $MAXD $CASE $P1 | sort | wc -l)
find $BASE -maxdepth $MAXD $CASE $P1 | sort
elif [[ $BEGIN == true ]]; then
NUM=$(find $BASE -maxdepth $MAXD $CASE $P1\* | sort | wc -l)
find $BASE -maxdepth $MAXD $CASE $P1\* | sort
elif [[ $END == true ]]; then
NUM=$(find $BASE -maxdepth $MAXD $CASE \*$P1 | sort | wc -l)
find $BASE -maxdepth $MAXD $CASE \*$P1 | sort
else
NUM=$(find $BASE -maxdepth $MAXD $CASE \*$P1\* | sort | wc -l)
find $BASE -maxdepth $MAXD $CASE \*$P1\* | sort
fi
echo
echo " $NUM found..."
fi
sleep 0.2
echo