0

Two questions about the same thing I think...

Question one:

Is it possible to have a bash script run with default parameters/options? ...in the sense if someone were to run the script:

./somescript.sh

it would actually run with ./somescript.sh | tee /tmp/build.txt?

Question two:

Would it also possible to prepend the script with defaults? For example, if you were to run the script ./somescript.sh

it would actually run

script -q -c "./somescript.sh" /tmp/build.txt | aha > /tmp/build.html?

Any help or guidance is very much appreciated.

TryTryAgain
  • 7,632
  • 11
  • 46
  • 82
  • Is it possible? Sure. You can always check the arguments in `$@` and take lots of actions based on the contents of lack thereof, including reinvoking yourself or redirecting your own output or simulating lots of other effects or setting default values for variables or. . . well, you get the point I trust – Eric Renouf Jun 01 '16 at 21:59
  • Sorry, how would I check arguments in `$@`? – TryTryAgain Jun 01 '16 at 22:01
  • You could access the argumens in `$@` or use `$#` to get the number of arguments (see http://stackoverflow.com/questions/18568706/checking-number-of-arguments-bash-script) – Eric Renouf Jun 01 '16 at 23:45
  • Sweet, looks like what I want for question one, thank you. – TryTryAgain Jun 02 '16 at 00:04
  • For part 2, you can use much of the same technique as part one, but for both have you considered possibly using aliases? – Eric Renouf Jun 02 '16 at 00:06
  • I had not, but I'm not sure how I would construct aliases to act as a preceding command to the tab script. – TryTryAgain Jun 02 '16 at 00:08
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/113570/discussion-between-eric-renouf-and-trytryagain). – Eric Renouf Jun 02 '16 at 00:10

2 Answers2

0

You need a wrapper script that handles all such scenario for you.

For example, your wrapper script can take parameters that helps you decide.

./wrapper_script.sh --input /tmp/build.txt --ouput /tmp/build.html

By default, --input and --output can be set to values you want when they are empty.

ronakg
  • 4,038
  • 21
  • 46
  • I'm looking for a solution that is contained within a single script. And I don't follow the input/output example. I'm not looking to do that sort of thing. – TryTryAgain Jun 01 '16 at 22:12
0

You can use the builtin $# to know how many arguments you have and take action based on that. If you want to do your second part, for example, you could do something like

if [[ $# -eq 0 ]]; then
    script -q -c "$0 /tmp/build.txt | aha /tmp/build.html
    exit
fi

# do everything if you have at least one argument

Though this will have problems if your script/path have spaces, so you're probably better putting the real path to your script in the script command instead of $0

You can also use exec instead of running the command and exiting, but make sure you have your quotes in the right place:

if [[ $# -eq 0 ]]; then
    exec script -q -c "$0 /tmp/build.txt | aha /tmp/build.html"
fi

# do everything when you have at least 1 argument
Eric Renouf
  • 13,950
  • 3
  • 45
  • 67
  • Unfortunately, this isn't working for me as I'd hoped. I'm having issues with recursion getting caught in a loop. So bad my shell is getting fork bombed. I've tried finagling the syntax etc, read a bunch on the topic...still stuck. :( – TryTryAgain Jun 02 '16 at 16:25
  • Well for this to work you better make sure you are passing an argument when you invoke the script from within. My simple example worked for me running it just a single time. The location of the quotes is important, note that I put mine around `$0 /tmp/build.txt` and your original had it only around the script name, and that might be the problem – Eric Renouf Jun 02 '16 at 17:02