Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use a variable argument number in a bash script?

Tags:

bash

shell

#!/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

The problem I'm having is right here:

 #loop through additional filetypes and append
    num=1
    while [ $num -lt $# ]
    do
      (( num++ ))
      types=$types' -o -name *.'>>$$num<<
    done

I simply want to iterate over all the arguments not including the first one, should be easy enough, but I'm having a difficult time figuring out how to make this work

like image 960
Corbin Tarrant Avatar asked Apr 05 '10 23:04

Corbin Tarrant


People also ask

How do you pass a number argument in shell script?

Arguments can be passed to the script when it is executed, by writing them as a space-delimited list following the script file name. Inside the script, the $1 variable references the first argument in the command line, $2 the second argument and so forth. The variable $0 references to the current script.

What variable gives the number of arguments given in a bash script?

The $# special variable always holds the total number of arguments passed to any specific Bash script.

How do I reference a variable in bash?

Indirect referencing in Bash is a multi-step process. First, take the name of a variable: varname. Then, reference it: $varname. Then, reference the reference: $$varname.


2 Answers

from the bash man page:

  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.

So your loop is going to look something like this:

#loop through additional filetypes and append
while [ $# -gt 0 ]
do
  types=$types' -o -name *.'$1
  shift
done
like image 186
Zac Thompson Avatar answered Sep 17 '22 11:09

Zac Thompson


If all you're trying to do is loop over the arguments, try something like this:

for type in "$@"; do
    types="$types -o -name *.$type"
done

To get your code working though, try this:

#loop through additional filetypes and append
num=1
while [ $num -le $# ]
do
    (( num++ ))
    types=$types' -o -name *.'${!num}
done
like image 29
amertune Avatar answered Sep 17 '22 11:09

amertune