【问题标题】:Linux Shell Scripting: Script CheckLinux Shell 脚本:脚本检查
【发布时间】:2018-08-05 12:10:17
【问题描述】:

我是 Linux bash 脚本的新手,我似乎找不到我做错了什么。这是我的代码。输入数字 2 和 3,在提示我询问用户我的代码停止后,它不会继续执行 IF ELSE 语句。感谢那些愿意提供帮助的人!

#!/bin/bash

while true
do
    clear
    echo "Please enter one of the following options"
    echo "1. Move empty files"
    echo "2. Check file size"
    echo "3. Which file is newer"
    echo "4. File check rwx"
    echo "5. Exit".
    echo -e "Enter Choice:"
    read answer 
    case "$answer" in
        1) ./move_empty 
        exit 55 ;;
        2) echo "Enter a filename" 
           read filename
           if [ -f $filename ];
           then ./file_size 
           fi 
               ;;
        3) echo "Enter first file:"
               read filename
           echo "Enter second file:"
           read filename2
               if [ ! -f "$filename" ];
               then
                  echo "Supplied file name" $filename "does not exist";
                  if [ $filename" -nt $filename" ]; then
                  echo $filename "is newer"
                  exit 1fi
                  fi    ;;    

        5) exit ;;
    esac
done

【问题讨论】:

  • 如果每行缩进4个空格,它应该像源代码一样格式并且可读。
  • 将第一行从!/bin/bash改为#!/bin/bash
  • 它已经改变了,感谢 fang!
  • 请看:shellcheck.net
  • 你好,我查过了。提示用户输入后,我的脚本仍然无法工作。我的 if-else 语句不起作用

标签: linux bash shell if-statement scripting


【解决方案1】:

如果您在ShellCheck.net 完成了检查,那么您应该已经收到:

$ shellcheck myscript
No issues detected!

如果你没有把它做到这一点,你就没有完成。您的脚本中有多个引用问题,并且您比较了$filename -nt $filename(这总是错误的)。小的“注意细节”问题会产生很大的不同。 ShellCheck.net 做了一个彻底的工作,但不会发现逻辑问题,这些都留给你。引用的清理看起来类似于:

#!/bin/bash

while true
do
    clear
    echo "Please enter one of the following options"
    echo "1. Move empty files"
    echo "2. Check file size"
    echo "3. Which file is newer"
    echo "4. File check rwx"
    echo "5. Exit".
    echo -n "Enter Choice: "
    read -r answer 
    case "$answer" in
        1)  ./move_empty 
            exit 55
            ;;
        2)  echo -n "Enter a filename: " 
            read -r filename
            if [ -f "$filename" ]
            then 
                ./file_size 
            fi 
            ;;
        3)  echo -n "Enter first file: "
            read -r filename
            echo -n "Enter second file: "
            read -r filename2
            if [ ! -f "$filename2" ]
            then
                echo "Supplied file name $filename does not exist";
                if [ "$filename" -nt "$filename2" ]; then
                    echo "$filename is newer"
                    exit 1
                fi
            fi
            ;;    
        5)  exit
            ;;
    esac
done

(注意:您不需要echo -e,因为您的提示中没有要处理的反斜杠转义字符,您可能希望-n 防止在末尾添加换行符提示)

另请注意:使用clear虽然对某些终端很好,但会导致其他终端出现问题。请注意潜在问题。)

如果您的 then 与您的条件表达式在同一行,例如if [ "$filename" -nt "$filename2" ]; then 那么';' 在结束']' 之后需要一个';' 来表示一个换行符,否则就不需要一个';'


逻辑问题

如前所述,ShellCheck 不会捕获逻辑问题,您必须通过代码进行处理。看起来您打算执行以下操作:

        3)  echo -n "Enter first file: "
            read -r filename
            echo -n "Enter second file: "
            read -r filename2
            if [ ! -f "$filename" ] || [ ! -f "$filename2" ]
            then
                echo "Supplied file '$filename' or '$filename2' does not exist";
                exit 1
            fi
            if [ "$filename" -nt "$filename2" ]; then
                echo "$filename is newer"
            else
                echo "$filename2 is newer"
            fi
            ;;    

你只需要一行一行地看...

查看一下,如果您还有其他问题,请告诉我。

【讨论】:

  • 谢谢你!我只是不明白为什么在要求 file1 和 2 之后它没有转到下一行,即 if-then。什么时候对我来说我没有看到任何问题?用我的脚本
  • 可以,但只有在"$filename" -nt "$filename2" 时才会显示输出。如果"$filename2" -nt "$filename",你没有得到任何输出,它似乎跳过了if :) 还有注意:你的逻辑是如何构造的。只有if [ ! -f "$filename2" ] 才能到达那里——这是“逻辑”部分。
  • 因为“CLEAR”
  • 可以的。我附上了关于clear 问题的注释,但是您还有其他逻辑问题,我更新了答案以指出一个问题。坚持下去。你正在取得良好的进展。
  • 非常感谢大卫爵士的宝贵时间和帮助!我很感激!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-07
  • 2014-03-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多