fun with bash 2

Posted on

I’m reverse engineering a make file my team uses to something I can put together for my own use later … just in case. Here’s something I found which I didn’t understand which might be unique:

${variable-benji}

From this site ( http://www.grymoire.com/Unix/Sh.html#uh-30 ) i find this table:

Form Meaning
${variable?word} Complain if undefined
${variable-word} Use new value if undefined
${variable+word} Opposite of the above
${variable=word} Use new value if undefined, and redefine

So what we have is like a default value for a variable that we can also use to do stuff with. In my case I do something like this:

#!/bin/sh
test_val_three=${1-benji}
... 
function check_parameters {
	if [ $test_val_three = 'benji' ]; then
		test_val_three='パラメータは正解'
	else
		test_val_three='パラメータは間違っている'
	fi
	echo $test_val_three
}

..incase the user hasn’t entered anything as a parameter, or, they did and the keyword matches the default value. If the value is different from the default, then it prints out a different message like so:
パラメータは間違っている

else if it the keyword matches it prints this
パラメータは正解

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s