How do i check for the correct number of arguments (one argument). If somebody tries to invoke the script without passing in the correct number of arguments, and checking to make sure the command line argument actually exists and is a directory.
我如何检查正确数量的参数(一个参数)。如果有人试图在不传递正确数量的参数的情况下调用脚本,并检查以确保命令行参数实际存在并且是目录。
3 个解决方案
#1
159
#!/bin/sh
if [ "$#" -ne 1 ] || ! [ -d "$1" ]; then
echo "Usage: $0 DIRECTORY" >&2
exit 1
fi
Translation: If number of arguments is not (numerically) equal to 1 or the first argument is not a directory, output usage to stderr and exit with a failure status code.
转换:如果参数数量(数值)不等于1或第一个参数不是目录,则输出使用到stderr并退出并显示失败状态代码。
More friendly error reporting:
更友好的错误报告:
#!/bin/sh
if [ "$#" -ne 1 ]; then
echo "Usage: $0 DIRECTORY" >&2
exit 1
fi
if ! [ -e "$1" ]; then
echo "$1 not found" >&2
exit 1
fi
if ! [ -d "$1" ]; then
echo "$1 not a directory" >&2
exit 1
fi
#2
11
cat script.sh
cat script.sh
var1=$1
var2=$2
if [ "$#" -eq 2 ]
then
if [ -d $var1 ]
then
echo directory ${var1} exist
else
echo Directory ${var1} Does not exists
fi
if [ -d $var2 ]
then
echo directory ${var2} exist
else
echo Directory ${var2} Does not exists
fi
else
echo "Arguments are not equals to 2"
exit 1
fi
execute it like below -
像下面一样执行 -
./script.sh directory1 directory2
Output will be like -
输出将像 -
directory1 exit
directory2 Does not exists
#3
5
You can check the total number of arguments which are passed in command line with "$#
" Say for Example my shell script name is hello.sh
您可以检查在命令行中使用“$#”传递的参数总数。例如,我的shell脚本名称是hello.sh
sh hello.sh hello-world
# I am passing hello-world as argument in command line which will b considered as 1 argument
if [ $# -eq 1 ]
then
echo $1
else
echo "invalid argument please pass only one argument "
fi
Output will be hello-world
输出将是hello-world
#1
159
#!/bin/sh
if [ "$#" -ne 1 ] || ! [ -d "$1" ]; then
echo "Usage: $0 DIRECTORY" >&2
exit 1
fi
Translation: If number of arguments is not (numerically) equal to 1 or the first argument is not a directory, output usage to stderr and exit with a failure status code.
转换:如果参数数量(数值)不等于1或第一个参数不是目录,则输出使用到stderr并退出并显示失败状态代码。
More friendly error reporting:
更友好的错误报告:
#!/bin/sh
if [ "$#" -ne 1 ]; then
echo "Usage: $0 DIRECTORY" >&2
exit 1
fi
if ! [ -e "$1" ]; then
echo "$1 not found" >&2
exit 1
fi
if ! [ -d "$1" ]; then
echo "$1 not a directory" >&2
exit 1
fi
#2
11
cat script.sh
cat script.sh
var1=$1
var2=$2
if [ "$#" -eq 2 ]
then
if [ -d $var1 ]
then
echo directory ${var1} exist
else
echo Directory ${var1} Does not exists
fi
if [ -d $var2 ]
then
echo directory ${var2} exist
else
echo Directory ${var2} Does not exists
fi
else
echo "Arguments are not equals to 2"
exit 1
fi
execute it like below -
像下面一样执行 -
./script.sh directory1 directory2
Output will be like -
输出将像 -
directory1 exit
directory2 Does not exists
#3
5
You can check the total number of arguments which are passed in command line with "$#
" Say for Example my shell script name is hello.sh
您可以检查在命令行中使用“$#”传递的参数总数。例如,我的shell脚本名称是hello.sh
sh hello.sh hello-world
# I am passing hello-world as argument in command line which will b considered as 1 argument
if [ $# -eq 1 ]
then
echo $1
else
echo "invalid argument please pass only one argument "
fi
Output will be hello-world
输出将是hello-world