小编Aus*_*dog的帖子

如果用户不正确地调用脚本,则使Bash脚本退出并打印错误消息

所需的脚本是

#!/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"
Run Code Online (Sandbox Code Playgroud)

编辑,脚本已更改为

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

  echo 'Usage: ' …
Run Code Online (Sandbox Code Playgroud)

error-handling bash multiplicity

5
推荐指数
1
解决办法
2万
查看次数

标签 统计

bash ×1

error-handling ×1

multiplicity ×1