#!/bin/bash
# Script to output the total size of requested filetype recursively
# Error out if no file types were provided
if [ $# -lt 1 ]
then
echo "Syntax Error, Please provide at least one type, ex: sizeofTypes {filetype1} {filetype2}"
exit 0
fi
#set first filetype
types="-name *."$1
#loop through additional filetypes and append
num=1
while [ $num -lt $# ]
do
(( num++ ))
types=$types' -o -name *.'$$num
done
echo "TYPES="$types
find . -name '*.'$1 | xargs du -ch *.$1 | grep total
Run Code Online (Sandbox Code Playgroud)
我遇到的问题就在这里:
#loop through additional filetypes and append
num=1
while [ $num -lt $# ]
do
(( num++ ))
types=$types' -o -name *.'>>$$num<<
done
Run Code Online (Sandbox Code Playgroud)
我只是想迭代所有的参数,不包括第一个,应该很容易,但我很难搞清楚如何使这项工作
Zac*_*son 15
来自bash手册页:
shift [n]
The positional parameters from n+1 ... are renamed to $1 ....
Parameters represented by the numbers $# down to $#-n+1 are
unset. n must be a non-negative number less than or equal to
$#. If n is 0, no parameters are changed. If n is not given,
it is assumed to be 1. If n is greater than $#, the positional
parameters are not changed. The return status is greater than
zero if n is greater than $# or less than zero; otherwise 0.
Run Code Online (Sandbox Code Playgroud)
所以你的循环看起来像这样:
#loop through additional filetypes and append
while [ $# -gt 0 ]
do
types=$types' -o -name *.'$1
shift
done
Run Code Online (Sandbox Code Playgroud)
如果您要做的就是遍历参数,请尝试以下方法:
for type in "$@"; do
types="$types -o -name *.$type"
done
Run Code Online (Sandbox Code Playgroud)
要使代码正常工作,请尝试以下方法:
#loop through additional filetypes and append
num=1
while [ $num -le $# ]
do
(( num++ ))
types=$types' -o -name *.'${!num}
done
Run Code Online (Sandbox Code Playgroud)