如果用户错误地调用脚本,则使 Bash 脚本退出并打印错误消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12966622/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Make Bash script exit and print error message if users invoke the script incorrectly
提问by Ausghostdog
Script needed was
需要的脚本是
#!/bin/bash
# Check if there are two arguments
if [ $# -eq 2 ]; then
# Check if the input file actually exists.
if ! [[ -f "" ]]; then
echo "The input file does not exist."
exit 1
fi
else
echo "Usage: grep -P "^[\s]*[0-9A-Za-z-]+.?[\s]*$" $*
if [ ! -f "" ]; then
echo 'Usage: '
echo
echo './Scriptname inputfile > outputfile'
exit 0
fi
[inputfile] [outputfile]"
exit 1
fi
# Run the command on the input file
grep -P "^[\s]*[0-9A-Za-z-]+.?[\s]*$" "" > ""
Edit, the script has changed to
编辑,脚本已更改为
Usage:
./Scriptname inputfile > outputfile
invoking the script with no parameters gives no erros and sits blank
不带参数调用脚本不会产生错误并且为空白
grep -P "^[\s]*[0-9A-Za-z-]+.?[\s]*$" $*
I have bit of code
我有一些代码
This is a multi word line
this
the above line is not
now
once again wrong
This code pulls lines that have a single word on them and pumps the output to a new file, so for example
这段代码拉出有一个单词的行并将输出泵送到一个新文件,例如
This is a multi word line this the above line is not now once again wrong
This now
The output would be
输出将是
if [incorrectly invoted unsure what to type]
echo $usage
exit 1
Usage="usage [inputfile] [>] [outputfile]
The code works, users invoke the code using ./scriptname file > newfile
代码有效,用户调用代码使用 ./scriptname file > newfile
However, I am trying to expand the code to give users an error message if they invoke the script incorrectly.
但是,我正在尝试扩展代码,以便在用户错误地调用脚本时向用户提供错误消息。
For the error messange, I'm thinking of echoing something back like scriptname file_to_process > output_file
.
对于错误消息,我正在考虑回显类似scriptname file_to_process > output_file
.
I did try
我确实尝试过
if [ ! -n ]; then
echo 'Usage: '
echo
echo './Scriptname inputfile > outputfile'
exit 0
fi
However I have had little luck. The code runs but does nothing if I invoke with just the script name. Also, if I invoke the script with just the scriptname and the input file, it will output the results instead of exiting with the error message.
然而,我的运气并不好。如果我只使用脚本名称调用,代码会运行但不执行任何操作。此外,如果我仅使用脚本名称和输入文件调用脚本,它将输出结果而不是退出并显示错误消息。
Other ones I have tried are
我尝试过的其他方法是
#!/bin/bash
grep -P "^[\s]*[0-9A-Za-z-]+.?[\s]*$" $*
if [ ! -f "" ]; then
echo 'Usage: '
echo
echo './Scriptname inputfile > outputfile'
exit 0
fi
Given replies I have received so far, my code now is
鉴于到目前为止我收到的答复,我现在的代码是
./scriptname 'file > newfile'
When invoking the script without an input file the script does nothing and has to be aborted with ctrl+c, still trying to get the echo of the invoke message.
当在没有输入文件的情况下调用脚本时,脚本什么都不做,必须用 ctrl+c 中止,仍然试图获得调用消息的回声。
采纳答案by doubleDown
When you are invoking the script like ./scriptname file > newfile
, the shell interprets file
as the only argument to ./scriptname
. This is because >
is the standard output redirection operator.
当您像 一样调用脚本时./scriptname file > newfile
,shell 将解释file
为./scriptname
. 这是因为>
是标准输出重定向操作符。
I would like to propose 2 possible alternatives:
我想提出两种可能的选择:
Alternative 1备选方案 1:也许您可以尝试将其作为 1 个参数传递?
#!/bin/bash
# Check if the format is correct
if [[ =~ (.+)' > '(.+) ]]; then
# Check if the input file actually exists.
if ! [[ -f "${BASH_REMATCH[1]}" ]]; then
echo "The input file ${BASH_REMATCH[1]} does not exist!"
exit 1
fi
else
echo "Usage: ./scriptname file newfile
\"[inputfile] [>] [outputfile]\""
exit 1
fi
# Redirect standard output to the output file
exec > "${BASH_REMATCH[2]}"
# Run the command on the input file
grep -P "^[\s]*[0-9A-Za-z-]+.?[\s]*$" "${BASH_REMATCH[1]}"
In that case one way to check the format would be
在这种情况下,检查格式的一种方法是
#!/bin/bash
# Check if there are two arguments
if [ $# -eq 2 ]; then
# Check if the input file actually exists.
if ! [[ -f "" ]]; then
echo "The input file does not exist."
exit 1
fi
else
echo "Usage: inputfile=${1:?Usage: $(basename if [ ! -f "" ]; then
echo 'Usage: '
echo
echo './Scriptname inputfile > outputfile'
exit 0
fi
) inputfile > outputfile}
[inputfile] [outputfile]"
exit 1
fi
# Run the command on the input file
grep -P "^[\s]*[0-9A-Za-z-]+.?[\s]*$" "" > ""
Note: If you are checking whether the arguments are valid or not, it's generally better to run commands only after the checking is done.
注意:如果您正在检查参数是否有效,通常最好仅在检查完成后运行命令。
Alternative 2: Passing 2 arguments like
备选方案 2:传递 2 个参数,例如
if [ ! $# -eq 1 ]; then
cat << EOF
Usage:
##代码## 'input_file' > output_file
EOF
exit 1
fi
The script looks like this
脚本看起来像这样
##代码##回答by Ansgar Wiechers
I'd use parameter expansionfor this:
我会为此使用参数扩展:
##代码##If the script is called without arguments (i.e. $1
is unset) the ${var:?error message}
expansion causes the shell to display an error with the given message and exit. Otherwise the first argument is assigned to $inputfile
.
如果在没有参数的情况下调用脚本(即$1
未设置),${var:?error message}
扩展会导致 shell 显示带有给定消息的错误并退出。否则,第一个参数被分配给$inputfile
。
回答by Alvin Wong
Try to add double quotes around $1
and use -f
to check for exists and is normal file:
尝试在周围添加双引号$1
并用于-f
检查是否存在并且是正常文件:
回答by higuaro
Also you can check for the param count with $#
and cat
an usage message:
您还可以检查参数计数$#
和cat
使用消息: