Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        5年前关闭。
                                                                                            
                
        
我正在练习脚本,但我不知道我的脚本出了什么问题。这就是我现在所拥有的。

#!/bin/bash

read -p "Continue? y/n" y

y=0

if (y eq- 0 )then;
    y=1
fi
if (y eq- 0 )then;
    read-p "Thank you for running this script"
    exit
else
    read-p "You will now exit"
    exit
fi


任何建议或想法都会有所帮助。

最佳答案

一些注意事项:


要在bash中使用变量,请在前面加上$
的;如果放在同一行,则在then之前

#!/bin/bash -x

read -p "Continue? y/n" y
#y=0
# Why set y to 0 when you read it ?
# Why check y equal to 0 if you just set it ?
#if [[ $y -eq 0 ]] ; then
#  y=1
#fi

if [[ "$y" = "y" ]]; then
   read -p "Thank you for running this script"
   exit
else
   read -p "You will now exit"
   exit
fi



您还可以使用正则表达式来检查大小写是否为“ y”:

if [[ $y =~ [yY] ]]; then
   read -p "Thank you for running this script"
fi

关于linux - Bash脚本,如果语句,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23022817/

10-14 14:20
查看更多