#!/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
我遇到的问题就在这里:
#loop through additional filetypes and append
num=1
while [ $num -lt $# ]
do
(( num++ ))
types=$types' -o -name *.'>>$$num<<
done
我只是想迭代所有不包括第一个的论点,应该很容易,但我很难搞清楚如何使这个工作
答案 0 :(得分:15)
:
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.
所以你的循环看起来像这样:
#loop through additional filetypes and append
while [ $# -gt 0 ]
do
types=$types' -o -name *.'$1
shift
done
答案 1 :(得分:5)
如果您要做的就是循环参数,请尝试以下方法:
for type in "$@"; do
types="$types -o -name *.$type"
done
要使代码正常工作,请尝试以下方法:
#loop through additional filetypes and append
num=1
while [ $num -le $# ]
do
(( num++ ))
types=$types' -o -name *.'${!num}
done
答案 2 :(得分:1)
如果你不想包含第一个,那么这样做的方法就是使用shift。或者你可以试试这个。想象变量s
是您传入的参数。
$ s="one two three"
$ echo ${s#* }
two three
当然,这假设你不会传递一个单词的字符串。