I have a bash script that I want invoked using a require -p
argument that is of type string/alphanumeric. For example:
sh myscript.sh -p abc123
My best attempt thus far:
#!/usr/bin/bash
mypassword=${p:}
echo $mypassword
if [ -z mypassword ]
then
echo "error"
fi
Since this is a required argument, I don't have a good meaningful default to apply (hence my attempt with using ${p:}
to set mypassword
to empty/null, and then attempt the null check conditional right afterwards).
When I run it via sh myscript.sh -p abc123
I get:
myscript.sh: line 46: ${p:}: bad substitution
All I'm trying to accomplish is this:
If -p
wasn't specified, or if it was specified, but no value for it was given (e.g. -p abc123
), I want to echo "error"
. In other words, -p
must not only be provided, but a value for it must be supplied as well.
Any ideas as to how I could accomplish this?