0

I made a basic bash script piping fortune and cowsay to make a wise cow, and titled it appropriately as wisecow Is there a way I can enable the flags associated with cowsay so that I can input

wisecow -s

and get the stoner cow, or -b for the borg cow, or what have you?

Currently when I do

wisecow -d

or whatever, the cow looks the same as if I hadn't used the flag

the code lol:

#!/bin/bash
fortune | cowsay
gwyns
  • 3
  • 2
  • You can use the bash builtin command `getopts` so that your script can process its arguments and you can pass them on to whichever command. Search here and Stack Overflow for getopts, there are loads of examples. – glenn jackman Oct 25 '19 at 02:04
  • thank you. would I just put that in a new line on the script? – gwyns Oct 25 '19 at 02:11
  • Related: [Pass command line parameters to a program inside the shell script](https://unix.stackexchange.com/questions/304895/pass-command-line-parameters-to-a-program-inside-the-shell-script) – steeldriver Oct 25 '19 at 02:12
  • Ahhh, thank you. I forgot to put it in when I rewrote the post, I'll pop it in. That fixed it, thank you very much @steeldriver – gwyns Oct 25 '19 at 03:44

1 Answers1

2

You give some option to your script, but you never pass it on to cowsay.

To do that, use

fortune | cowsay "$@"

in your script. The "$@" will expand to the list of command line arguments that your script was given, for example the option -d, if that was what you invoked your script with.

Kusalananda
  • 320,670
  • 36
  • 633
  • 936