简体   繁体   English

在Shell脚本中检查文件是否存在错误

[英]Error while checking existence of a file in shell script

I wrote a script to check if a given file exists or not ( i am considering only regular files ) It displays opposite, that is, if file exists then "not found" and "file found" if file doesn't exists. 我编写了一个脚本来检查给定文件是否存在(我只考虑常规文件),它显示相反的内容,即如果文件存在,则“未找到”,如果文件不存在,则“找到文件”。 Have i messed up the if else ? 我是否搞砸了否则? Please tell me how to rectify it. 请告诉我如何纠正它。

#! /bin/bash
is_file_exists(){
 local file="$1"
 if  [[ -f "$file" ]]
 then
  return 1
 else
 return 0
 fi
}

if [[ "$#" -eq 0 ]]
then
 echo "enter a file name" 
 exit
fi

if ( is_file_exists "$1" )
then
  echo "file found"
else
  echo "not found"
fi

As my comment said, you have the return 0 and return 1 around the wrong way in your is_file_exists function, and you should use: if is_file_exists "$1" , ie no parentheses. 如我的评论所述,在is_file_exists函数中, return 0return 1的方式错误,应该使用: if is_file_exists "$1" ,即不带括号。

Shell if statements test success or failure of the given command. 外壳if语句测试给定命令的成功或失败。 Success is defined as a returned value of zero. 成功定义为返回值为零。 So maybe you had the returns around that way because you might have come from C, Java, or a similar language where zero is false . 因此,也许您获得了这种回报,因为您可能来自C,Java或类似的语言(零为false) Think in terms of success or failure rather than true or false. 从成功或失败而不是正确或错误的角度来思考。

I have suggested a few other tweaks, including consistent indentation: 我还建议了其他一些调整,包括一致的缩进:

#! /bin/bash
is_file_exists(){

    local file="$1"

    # [ (test) could have been used here
    # Is the name given a regular file?        
    if  [[ -f $file ]]     # quotes not required with [[ (but are with [ )
    then
        return 0
    else
        return 1
    fi
}

# Use (( )) for arithmetic comparisons
if (( $# == 0 ))
then
    echo "enter a file name" >&2       # error messages should go to stderr
    exit
fi

# Delimit filenames on display, shows unintended whitespace
if is_file_exists "$1"
then
    echo "'$1' file found"
else
    echo "'$1' not found"
fi

Well, firstly, you're not checking for existence ( -e ); 好吧,首先,您不需要检查是否存在( -e ); you're checking that it exists and that it's a regular file. 您正在检查它是否存在并且它是常规文件。

Secondly, you're returning 0 (success) if it doesn't exist. 其次,如果存在则返回0(成功)。

But since the return value of a function is that of the last command, there's no need for the if / else at all: 但是由于函数的返回值是最后一个命令的返回值,因此根本不需要if / else

is_file_exists() {
   test -f "$1"
}

Re-written program (as portable shell, since it doesn't need anything Bash-specific): 重新编写程序(作为便携式shell,因为它不需要任何特定于Bash的东西):

#!/bin/sh

regular_file_exists() {
   test -f "$1"
}

# Is it used correctly?
if [ $# -ne 1 ]
then
  echo "enter a file name" >&2
  exit 1
fi

if regular_file_exists "$1"
then
  echo "file found"
else
  echo "not found"
fi

If you still want to keep the "inverse" logic of your is_file_exists() function then you can do the following: 如果仍然要保持is_file_exists()函数的“逆”逻辑,则可以执行以下操作:

#! /bin/bash
is_file_exists(){
 local file="$1"
 if  [[ -f "$file" ]]
 then
  return 1
 else
  return 0
 fi
}

if [[ "$#" -eq 0 ]]
then
 echo "enter a file name" 
 exit
fi

is_file_exists "$1"
retVal=$?

if [ $retVal -ne 0 ]; then
  echo "file found"
else
  echo "not found"
fi

This stores the return value of the function into the retVal variable and checks its value to provide the desired output. 这会将函数的返回值存储到retVal变量中,并检查其值以提供所需的输出。

Also, as suggested by @Toby, another solution would be (skipping the fuction): 另外,如@Toby所建议,另一种解决方案是(跳过功能):

if [[ "$#" -eq 0 ]]
then
 echo "enter a file name" 
 exit
fi

if ! is_file_exists "$1"; then
  echo "file found"
else
  echo "not found"
fi

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM