-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnoextname
executable file
·36 lines (31 loc) · 1.19 KB
/
noextname
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/bin/bash
# Get the names right
for fullpath in "$@"
do
filename="${fullpath##*/}" # Strip longest match of */ from start
dir="${fullpath:0:${#fullpath} - ${#filename}}" # Substring from 0 thru pos of filename
base="${filename%.[^.]*}" # Strip shortest match of . plus at least one non-dot char from end
ext="${filename:${#base} + 1}" # Substring from len of base thru end
if [[ -z "$base" && -n "$ext" ]]; then # If we have an extension and no base, it's really the base
base=".$ext"
ext=""
fi
#echo -e "$fullpath:\n\tdir = \"$dir\"\n\tbase = \"$base\"\n\text = \"$ext\""
# echo "$ext"
help_opt() {
printf "noextname [OPTIONS] FILE"
printf "OPTIONS:"
printf "\t-f Print the full path of the file"
printf "\t-d Print the directory the file is in"
printf "\t-b Print the name of the file without the file extension"
printf "\t-h Print this Help."
printf "By default it prints basename of the file without the extension"
}
done
case "$1" in
-f) printf '%s' "$fullpath" ;;
-d) printf '%s' "$dir" ;;
-e) printf '%s' "$ext" ;;
-h) help_opt && exit 0 ;;
*) printf '%s' "$base" ;;
esac