How to check parameters in Bash?
by oldpink from LinuxQuestions.org on (#6HDN7)
I tried to find the answer for this with searches here and everywhere else that I could, but it's hard to even know how to phrase the search to do that, so I'm asking here directly, where I can clarify to someone much more knowledgeable than I am.
What I'm trying to do is to check that each parameter in a Bash script is correct, including if each parameter is wildcarded, then halt the entire script if any of the parameters is incorrect.
The problem is that parameter expansion before I can even run the script is interfering with this.
For example, I start the script with these lines:
for x in $@ ; do
case $x in
*.jpg|*.mp4)
echo OKAY
;;
*)
echo NOT OKAY
exit
;;
esac
done
What I'm trying to do is to get it to stop the script entirely if even one of the parameters ($@) does not match the choices in the first case strings.
For example, if I use *.j?g *.mp? together as the parameters, it should continue with the rest of the script, but if *.jpg *.jp are the parameters, I need the script to detect the incorrect *,jp parameter and exit.
In fact, if I pass one of the parameters using a wildcard that I want to stop the script, it only stops if a file matching that file spec exists to match the wildcard and expand it.
Obviously, I need it to halt whether that file exists or not.
I've tried everything that I can think of, including "set -f" to turn off expansion, but the problem is that only works if I have "set -f" switched on before the script even starts, obviously because otherwise the parameters are expanded on the command line before the script executes.
I'm essentially in a chicken-and-the-egg situation with parameter string checking on this one.
My apologies if the way that I'm asking my question is overly verbose or poorly worded, but I'm still hoping that someone can help with the answer on this one.
EDIT: I found that I actually have no problem accomplishing what I want with this script if I execute it from the bash shell first, but I use TCSH as my regular shell, and that is when it fails.
I know that's unconventional for most people, but I started on TCSH nearly thirty years ago and find that more comfortable than bash for my login shell.
Is there something that can work around that?
What I'm trying to do is to check that each parameter in a Bash script is correct, including if each parameter is wildcarded, then halt the entire script if any of the parameters is incorrect.
The problem is that parameter expansion before I can even run the script is interfering with this.
For example, I start the script with these lines:
for x in $@ ; do
case $x in
*.jpg|*.mp4)
echo OKAY
;;
*)
echo NOT OKAY
exit
;;
esac
done
What I'm trying to do is to get it to stop the script entirely if even one of the parameters ($@) does not match the choices in the first case strings.
For example, if I use *.j?g *.mp? together as the parameters, it should continue with the rest of the script, but if *.jpg *.jp are the parameters, I need the script to detect the incorrect *,jp parameter and exit.
In fact, if I pass one of the parameters using a wildcard that I want to stop the script, it only stops if a file matching that file spec exists to match the wildcard and expand it.
Obviously, I need it to halt whether that file exists or not.
I've tried everything that I can think of, including "set -f" to turn off expansion, but the problem is that only works if I have "set -f" switched on before the script even starts, obviously because otherwise the parameters are expanded on the command line before the script executes.
I'm essentially in a chicken-and-the-egg situation with parameter string checking on this one.
My apologies if the way that I'm asking my question is overly verbose or poorly worded, but I'm still hoping that someone can help with the answer on this one.
EDIT: I found that I actually have no problem accomplishing what I want with this script if I execute it from the bash shell first, but I use TCSH as my regular shell, and that is when it fails.
I know that's unconventional for most people, but I started on TCSH nearly thirty years ago and find that more comfortable than bash for my login shell.
Is there something that can work around that?