bash - 如果用户错误地调用脚本,使 Bash 脚本退出并打印错误消息

标签 bash error-handling multiplicity

需要的脚本是

#!/bin/bash

# Check if there are two arguments
if [ $# -eq 2 ]; then
   # Check if the input file actually exists.
   if ! [[ -f "$1" ]]; then
     echo "The input file $1 does not exist."
     exit 1
   fi
else
   echo "Usage: $0 [inputfile] [outputfile]"
   exit 1
fi

# Run the command on the input file
grep -P "^[\s]*[0-9A-Za-z-]+.?[\s]*$" "$1" > "$2"

编辑,脚本改成了

grep -P "^[\s]*[0-9A-Za-z-]+.?[\s]*$" $*
if [ ! -f "$1" ]; then

  echo 'Usage: '
  echo
  echo './Scriptname inputfile > outputfile'
  exit 0

fi

调用不带参数的脚本不会出现错误并且为空白

Usage: 

./Scriptname inputfile > outputfile

我有一些代码

grep -P "^[\s]*[0-9A-Za-z-]+.?[\s]*$" $*

这段代码拉取上面只有一个单词的行,并将输出泵到一个新文件中,例如

This is a multi word line
this
the above line is not
now
once again wrong

输出为

This
now

代码有效,用户使用 ./scriptname file > newfile 调用代码

但是,我正在尝试扩展代码,以便在用户错误地调用脚本时向他们提供错误消息。

对于错误消息,我正在考虑回显类似 scriptname file_to_process > output_file 的内容。

我试过了

if [incorrectly invoted unsure what to type]
echo $usage

exit 1
Usage="usage [inputfile] [>] [outputfile]

然而我运气不佳。如果我只使用脚本名称调用,代码会运行但什么也不做。此外,如果我仅使用脚本名称和输入文件调用脚本,它将输出结果而不是退出并显示错误消息。

其他我试过的有

if [ ! -n $1 ]; then

  echo 'Usage: '
  echo 
  echo './Scriptname inputfile > outputfile'
  exit 0

fi

鉴于我目前收到的回复,我现在的代码是

#!/bin/bash
grep -P "^[\s]*[0-9A-Za-z-]+.?[\s]*$" $*
if [ ! -f "$1" ]; then

  echo 'Usage: '
  echo 
  echo './Scriptname inputfile > outputfile'
  exit 0

fi

当在没有输入文件的情况下调用脚本时,脚本什么都不做,必须用 ctrl+c 中止,仍然试图获得调用消息的回显。

最佳答案

当您调用 ./scriptname file > newfile 之类的脚本时,shell 会将 file 解释为 ./scriptname 的唯一参数.这是因为 > 是标准输出重定向运算符。

我想提出 2 种可能的选择:


备选方案 1: 也许您可以尝试像这样将其作为 1 个参数传递?

./scriptname 'file > newfile'

在这种情况下,一种检查格式的方法是

#!/bin/bash

# Check if the format is correct
if [[ $1 =~ (.+)' > '(.+) ]]; 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: $0 \"[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]}"

注意:如果您正在检查参数是否有效,通常最好仅在检查完成后运行命令。


备选方案 2: 传递 2 个参数,如

./scriptname file newfile

脚本是这样的

#!/bin/bash

# Check if there are two arguments
if [ $# -eq 2 ]; then
   # Check if the input file actually exists.
   if ! [[ -f "$1" ]]; then
     echo "The input file $1 does not exist."
     exit 1
   fi
else
   echo "Usage: $0 [inputfile] [outputfile]"
   exit 1
fi

# Run the command on the input file
grep -P "^[\s]*[0-9A-Za-z-]+.?[\s]*$" "$1" > "$2"

关于bash - 如果用户错误地调用脚本,使 Bash 脚本退出并打印错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12966622/

相关文章:

python - 当我通过终端使用 VIM 时,我不断收到重复的文件,并在末尾附加了 ~。这些是什么?

linux - BASH脚本检查linux服务器是否为 "up"

linux - 如何使用另一个文件中已经存在的完成来扩展 bash-completion?

swift - 如何通过物化处理永无止境的链上的错误?

ios - Swift Firebase 通知 "libc++abi.dylib: terminating with uncaught exception of type NSException"

c# - 为什么SqlException严重性类与规范所说的不同?

uml - 限制类图中的子对象

bash - 列出linux中的所有叶子目录