Answer the question
In order to leave comments, you need to log in
How to specify multiple conditions with logical OR for if that contain *?
There is a script which should accept the arguments entered by the user. In it, you need to specify several variables in the if in the if then else loop so that their beginning is like qwerty*, asddfg*, etc., that is, after a certain specified set of characters, you can substitute any and if will consider this a positive answer.
In general, something like this:
if
then
echo error
else
echo success
fi
Answer the question
In order to leave comments, you need to log in
|| separates expressions, $1 != video* is an expression, but just audio* is not.
But I would solve this problem through case:
case $1 in
video*|audio*|pic*) echo ok;;
*) echo error;;
esac
#!/bin/bash
test_parameter()
{
echo "$1" | grep -q '\(video\|audio\|pic\)\*'
}
if ! test_parameter "$1"; then
echo error
else
echo success
fi
exit 0
[[email protected] sh]$ ./t.sh video
error
[[email protected] sh]$ ./t.sh video*
success
[[email protected] sh]$ ./t.sh audio
error
[[email protected] sh]$ ./t.sh audio*
success
[[email protected] sh]$ ./t.sh x
error
[[email protected] sh]$
#!/bin/bash
error()
{
echo "error: $1" 1>&2
}
ok()
{
echo "success: $1" 1>&2
}
test_parameter()
{
echo "$1" | grep -q '\(video\|audio\|pic\)\*'
}
main()
{
if ! test_parameter "$1"; then
error "incorrect parameter: \"$1\""
else
ok "parameter is correct"
fi
}
main "[email protected]" || exit 1
exit 0
[[email protected] sh]$ ./t.sh
error: incorrect parameter: ""
[[email protected] sh]$ ./t.sh video
error: incorrect parameter: "video"
[[email protected] sh]$ ./t.sh video*
success: parameter is correct
[[email protected] sh]$ ./t.sh audio
error: incorrect parameter: "audio"
[[email protected] sh]$ ./t.sh audio*
success: parameter is correct
[[email protected] sh]$ ./t.sh pic
error: incorrect parameter: "pic"
[[email protected] sh]$ ./t.sh pic*
success: parameter is correct
[[email protected] sh]$ ./t.sh x
error: incorrect parameter: "x"
[[email protected] sh]$
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question