如何在bash脚本中使用变量参数号?

前端之家收集整理的这篇文章主要介绍了如何在bash脚本中使用变量参数号?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
#!/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

我只想迭代所有不包括第一个的参数,应该很简单,但是我很难想出如何使这项工作

从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.

所以你的循环看起来像这样:

#loop through additional filetypes and append
while [ $# -gt 0 ]
do
  types=$types' -o -name *.'$1
  shift
done
原文链接:https://www.f2er.com/bash/386119.html

猜你在找的Bash相关文章