2

I'm scripting a sequence of commands I used to enter by hand. The rough outline goes something like this

$ echo ${FILENAME_ARGS} | some | big | pipeline | sort -options | etc >temp
$ gs -OptionsThatNeverChange `cat temp`

Basically, the only thing that changes between runs are the options passed to sort and the files contained in FILENAME_ARGS.

What I want to do is be able to type something like

$ do-thing -some -options *.pdf

and have all of the -whatever go to the options for sort, while all of the *.pdf go into FILENAME_ARGS. To do this I need a way to pick out all of the things that start with a - from $@. Is there some simple way to pull options out of the arguments passed to a shell script?

slm
  • 369,824
Dan
  • 9,492
  • 2
    http://mywiki.wooledge.org/BashFAQ/035 -- How can I handle command-line arguments (options) to my script easily? – jordanm Dec 11 '13 at 21:49
  • Option parsing is a very FAQ. The wooledge link from @jordanm is a great resource. See also this related question. – jw013 Dec 11 '13 at 21:52
  • @jw013: This isn't about option parsing so much as option extraction. I just want to pass the options to sort and put the rest of the arguments somewhere else. – Dan Dec 11 '13 at 22:13

2 Answers2

3
echo ${FILENAME_ARGS} | grep -e - | sort >options
echo ${FILENAME_ARGS} | grep -v -e - >files
hildred
  • 5,829
  • 3
  • 31
  • 43
2

In your shoes, I'd write a script that takes all of your arguments and assigns them as variables, then execute the | very | long | pipe with the argument variables dropped in where necessary, i.e.

$ ./wrapper.sh -a firstoption -b secondoption -c thirdoption

$ cat wrapper.sh

#!/bin/bash

#trap variables with case statement here
for arg in "$@" ; do
    case "$arg" in
      -a)
        alpha=$2
        shift
        ;;
      -b)
        bravo=$2
        shift
        ;;
      -c)
        charlie=$2
        shift
        ;;
     esac
done
# at this point: $alpha=firstoption
#                $bravo=secondoption
#                $charlie=thirdoption

echo "Better grab a coffee man, this could take a while...."
echo "STUFF" | some $alpha $bravo | big -c $bravo -Z $charlie | pipeline -yx $charlie $alpha $bravo| sort -N $charlie -options | etc.${bravo}.${alpha} >temp.${charlie}
Stephan
  • 2,911