I am trying to write a script that can accept multiple options. The issue that I'm having is some of the potential input will be search criteria.

The syntax would be:

script

  • [-t seconds] [-n count]
  • All of the arguments are optional.
  • So far, my thoughts were to have the script write the "list of patterns" to an array and then process the arguments. My problem is I can't figure out how to make the script differentiate between a pattern and an argument.
  • This is what I have so far that will read every argument into an array:
if [ $# -ne $NO_ARGS ]; then
  GREPARRAY="$@"
  INDEX2="$#"
fi
echo $INDEX2
for PATTERN in ${GREPARRAY[@]}; do
  echo $PATTERN
done

Any assistance would be appreciated.

Thanks.

Recommended Answers

All 4 Replies

I am trying to write a script that can accept multiple options. The issue that I'm having is some of the potential input will be search criteria.

The syntax would be:

script

  • [-t seconds] [-n count]


Always put options before other arguments:

script [-t seconds] [-n count] [list of patterns]

That way, you can use getopts to parse the options.

All of the arguments are optional.

So far, my thoughts were to have the script write the "list of patterns" to an array and then process the arguments.


The arguments are already in an array: $1, $2, $3, etc..

My problem is I can't figure out how to make the script differentiate between a pattern and an argument.


Use getopts.

This is what I have so far that will read every argument into an array:

if [ $# -ne $NO_ARGS ]; then
  GREPARRAY="$@"


You haven't put anything into an array; you put all the options into a scalar variable.

INDEX2="$#"
fi
echo $INDEX2
for PATTERN in ${GREPARRAY[@]}; do


If you in fact had an array, that would break if any of the elements contained whitespace.

You need to quote the expansion:

for PATTERN in "${GREPARRAY[@]}"; do
echo $PATTERN
done


Hi,

Im trying to make a script:

#/bin/bash
teste="{33,351}";
ls -laht /tmp/interface*_${teste}_*.xml

but it gives an error. If i change ${teste} for the static value itself it works.

Any idea? I've tried to evaluate with the $() but it doesnt work.

Thanks

Hi,

Im trying to make a script:

#/bin/bash
teste="{33,351}";
ls -laht /tmp/interface*_${teste}_*.xml

but it gives an error.


What error does it give?

If i change ${teste} for the static value itself it works.


Always quote variable references:

ls -laht /tmp/interface*_"$teste"_*.xml

Still not working.

error answer: "ls: cannot access /tmp/interface*_{33,351}_*.xml: No such file or directory"

The problem is that the script thinks that {33,351} is a string, which in fact is not what we pretend. I want that the script interpret it like a command (like it doest when i dont use a variable).

Any ideas?

Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.