Bash 脚本 - If 文件不存在的嵌套 If 语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20532980/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 08:58:04  来源:igfitidea点击:

Bash script - Nested If Statement for If File Doesn't Exist

bashif-statementnested

提问by Syler

I'm trying to compile a script that will read user input, and check if the file after the y/n statement. Then it will make files executable. I think the problem with my script is conditional ordering but check it out yourself:

我正在尝试编译一个脚本来读取用户输入,并在 y/n 语句之后检查文件。然后它将使文件可执行。我认为我的脚本的问题是条件排序,但请自行检查:

target=/home/user/bin/

cd $target
read -p "This will make the command executable. Are you sure? (y/n)" CONT
if [ "$CONT" == "y" ];
then
  chmod +x 
  echo "File  is now executable."
else 
  if [ "$(ls -A /home/user/bin/)" ];
  then
    echo "File not found."
  else 
    echo "Terminating..."
  fi 
fi

As I said, I need the script to scan for the file after the y/n statement is printed. The script works fine how it is but still gives the "file is now executable" even if the argument file doesn't exist (but just gives the standard system "cannot find file" message after the echo'd text).

正如我所说,在打印 y/n 语句后,我需要脚本来扫描文件。该脚本工作正常,但即使参数文件不存在,它仍然给出“文件现在可执行”(但只是在回显文本后给出标准系统“找不到文件”消息)。

回答by Reinstate Monica Please

Your script is mostly correct, you just need to check if the file exists first. Also, it's not the best practice to use cdin shell scripts and not needed here.

你的脚本大部分是正确的,你只需要先检查文件是否存在。此外,这不是cd在 shell 脚本中使用的最佳实践,这里也不需要。

So re-writing it

所以重新写了

#!/bin/bash
target="/home/user/bin/"

if [[ ! -f $target ]]; then 
    echo "File not found."
else 
    read -p "This will make the command executable. Are you sure? (y/n) " CONT
    if [[ $CONT == "y" ]]; then
        chmod +x "$target"
        echo "File  is now executable."
    else
        echo "Terminating..."
    fi
fi

回答by David W.

To get an understanding:

要了解:

  • Your script will take one argument (a name of a file).
  • You ask if you want to make that file executable.
  • If the answer is 'yes', you make the file executable.
  • Otherwise, you don't.
  • 您的脚本将采用一个参数(文件名)。
  • 您询问是否要使该文件可执行。
  • 如果答案为“是”,则使文件可执行。
  • 否则,你不会。

You want to verify that the file exists too?

您也想验证该文件是否存在?

I'm trying to understand your logic. What does this:

我试图理解你的逻辑。这是做什么的:

if [ "$(ls -A /home/user/bin/)" ];

suppose to do. The [ ... ]syntax is a test. And, it has to be one of the valid tests you see here. For example, There's a test:

假设做。该[ ... ]语法是一个考验。而且,它必须是您在此处看到的有效测试之一。例如,有一个测试:

  • -e file: True if file exists.
  • -e 文件:如果文件存在则为真。

That mean, I can see if your file is under /home/user/bin:

这意味着,我可以查看您的文件是否位于/home/user/bin

target="/home/user/bin"
if [ -e "$target/$file" ]  # The "-e" test for existence
then
    echo "Hey! $file exists in the $target directory. I can make it executable."
else
    echo "Sorry, $file is not in the $target directory. Can't touch it."
fi

Your $(ls -A /home/user/bin/)will produce a file listing. It's not a valid test like -eunless it just so happens that the first file in your listing is something like -eor -d.

$(ls -A /home/user/bin/)将生成一个文件列表。这不是一个有效的测试,-e除非您的列表中的第一个文件碰巧类似于-e-d

Try to clarify what you want to do. I think this is something more along the lines you want:

试着澄清你想做什么。我认为这更符合你想要的:

#! /bin/bash

target="/home/user/bin"
if [ -z "" ] # Did the user give you a parameter
then
    echo "No file name given"
    exit 2
fi

# File given, see if it exists in $target directory
if [ ! -e "$target/" ]
then
    echo "File '$target/' does not exist."
    exit 2
fi

# File was given and exists in the $target directory

read -p"Do you want $target/ to be executable? (y/n)" continue
if [ "y" = "$continue" ]
then
    chmod +x "$target/"
fi

Note how I'm using the testing, and if the testing fails, I simply exit the program. This way, I don't have to keep embedding if/thenstatements in if/thenstatements.

请注意我如何使用测试,如果测试失败,我只需退出程序。这样,我就不必if/thenif/then语句中嵌入语句。