if [ "$#" -ne 1 ]; then
flag=1;
elif ! [[ "$1" == "arg1" || "$1" == "arg2" || "$1" == "arg3" || ...... ]]; then
echo "Invalid"
flag=1;
fi
if [ "$flag" == "1" ]; then
echo "Usage of script...."
exit
fi
count="$(ls *.mov | wc -l)"
if [[ "$count" -eq 0 ]]
then
echo there are 0 .mov files in this path
elif [[ "$count" -eq 1 ]]
then
echo there is 1 .mov file in this path
vlc *.mov
elif [[ $1 = "arg1" ]] ; then
echo entered the tough part....coz its not entering`enter code here`
elif [[ "$1" == "arg2" ]] || [[ "$1" == "arg10" ]] ; then
echo entered here atleast...but not entering
else
script continues
代码不会输入涉及命令行参数的elif条件。尝试=,==, - eq,双方括号,单方括号。但它没有进入,请帮忙
答案 0 :(得分:1)
I tried your script and it does get into the elif. the code I used is:
if [ "$#" -ne 1 ]; then
echo "not 1 arg"
flag=1;
elif ! [[ "$1" == "arg1" || "$1" == "arg2" || "$1" == "arg3" ]]; then
echo "Invalid"
flag=1;
else
echo "else"
fi
and the input/output are:
$ . script.sh 1 2
not 1 arg
$ . script.sh 1
Invalid
$ . script.sh arg1
else
I tried the second part and it is also working:
count=$2
if [[ "$count" -eq 0 ]] ;then
echo "there are 0 .mov files in this path"
elif [[ "$count" -eq 1 ]] ;then
echo "there is 1 .mov file in this path"
vlc *.mov
elif [[ $1 = "arg1" ]] ; then
echo "arg1 "
elif [[ "$1" == "arg2" ]] || [[ "$1" == "arg10" ]] ; then
echo "arg2 or arg10 "
else
echo "else"
fi
and tested it (second argument is "count"): $ . script.sh arg1 0 there are 0 .mov files in this path $ . script.sh 1 0 there are 0 .mov files in this path $ . script.sh arg10 0 there are 0 .mov files in this path $ . script.sh arg1 1 there is 1 .mov file in this path The program 'vlc' is currently not installed. To run 'vlc' please ask your administrator to install the package 'vlc-nox' $ . script.sh arg10 1 there is 1 .mov file in this path The program 'vlc' is currently not installed. To run 'vlc' please ask your administrator to install the package 'vlc-nox' $ . script.sh 1 1 there is 1 .mov file in this path The program 'vlc' is currently not installed. To run 'vlc' please ask your administrator to install the package 'vlc-nox' $ . script.sh arg1 2 arg1 $ . script.sh 1 2 else $ . script.sh arg10 2 arg2 or arg10
答案 1 :(得分:0)
You should look into the bash case statement http://mywiki.wooledge.org/BashGuide/TestsAndConditionals#Choices_.28case_and_select.29
For this particular script, it will make your script easier to read and your branching problem will most likely go away..
答案 2 :(得分:0)
if your purpose is to handle the arguments passed to the script by command line it's better to use the getopts
bash
this is just an example that you can adapt to your scope:
#!/bin/bash
function usage {
echo "usage: ..."
}
while getopts f:o:h opt; do
case $opt in
f)
fileName=$OPTARG
echo "filename[$fileName]"
;;
o)
otherargs=$OPTARG
echo "otherargs[$otherargs]"
;;
h)
usage && exit 0
;;
?)
usage && exit 2
;;
esac
done
~
output
[myShell] ➤ ./n -h
usage: ...
[myShell] ➤ ./n -f myfilename
filename[myfilename]
[myShell] ➤ ./n -o other
otherargs[other]
[myShell] ➤ ./n -l
./n: illegal option -- l
usage: ...