Bash脚本if语句不生效,输入yes无法重新展示case内病毒详情如何解决
Bash脚本修复方案
问题原因
你当前的脚本仅执行了一次病毒选择、详情展示的流程,输入yes后的逻辑仅打印了之前存储的旧病毒编号,没有重新运行选择、展示的完整流程,因此无法实现重复查看的需求。
修复后的完整代码
#!/bin/bash # 展示全球TOP5最具破坏性计算机病毒的脚本 # 颜色定义 RED="\033[31m" BROWN="\033[33m" NORMAL="\033[0m" BLACK="\033[30m" BLUE="\033[34m" GREEN="\033[32m" PURPLE="\033[35m" CYAN="\033[36m" GREY="\033[37m" # 标题 echo -e "${BLUE}Top 5 Most ${RED}Destructive${BLUE}Computer Viruses${NORMAL}" | cowsay # 密码校验部分(已注释) #read -s -p "Due to the Sensitive Nature of this information, Please enter your password:" pass #if echo $pass | sha256sum -c Hash.txt #check the password via sha256sum #then echo -e "${GREEN}Access Granted ${NORMAL}" #else exit 0 #fi # 主循环,支持重复查看 while true do # 展示病毒列表 echo -e "${BLUE} Please Select A Virus to Learn About" echo -e "${CYAN} 5. WannaCry -- ${RED}4 Billion: " echo -e "${CYAN} 4. ILoveYou -- ${RED}15 Billion: " echo -e "${CYAN} 3. Klez -- ${RED}19.8 Billion: " echo -e "${CYAN} 2. SoBig -- ${RED}30 Billion: " echo -e "${CYAN} 1. MyDoom -- ${RED}38 Billion: " echo -e "${CYAN}Type the number of the virus you wish to learn about:${NORMAL} " read virus # 匹配输出对应病毒详情 case $virus in 5) echo -e "The 2017 WannaCry computer virus is ransomware, a virus that takes over your computer (or cloud files) and holds them hostage.\nThe WannaCry ransomware ripped through computers in 150 countries, causing massive productivity losses as businesses, hospitals, and government organizations that didn’t pay were forced to rebuild systems from scratch. The malware raged like wildfire through 200,000 computers worldwide.\nIt stopped when a 22-year-old security researcher in the U.K. found a way to turn it off. Computers with out-of-date operating systems were hit especially hard. \n That’s why security experts always recommend updating your systems frequently.";; 4) echo -e "The year 2000’s ILOVEYOU virus worked by sending a bogus “love letter” that looked like a harmless text file. Like Mydoom, this attacker sent copies of itself to every email address in the infected machine’s contact list. Shortly after its May 4 release, it had spread to more than 10 million PCs. The virus was created by a college student in the Philippines named Onel de Guzman. Lacking funds, he wrote the virus to steal passwords so he could log into online services he wanted to use for free. He reportedly had no idea how far his creation would spread. This virus is also known as Loveletter.";; 3) echo -e "Klez is a close third on the list of the worst computer viruses ever created. With nearly 20 billion in estimated damages, it infected about 7.2% of all computers in 2001, or 7 million PCs. The Klez worm sent fake emails, spoofed recognized senders and, among other things, attempted to deactivate other viruses. As with other viruses and worms, Klez was released in several variants. It infected files, copied itself, and spread throughout each victim’s network. It hung around for years, with each version more destructive than the last. Windows has come a long way since most of the computer viruses on this list hit the web. Thankfully, built-in protection with Microsoft Defender is always on the watch.";; 2) echo -e "The 2003 Sobig computer virus is actually another worm. It is second only to the Mydoom virus in its scope. The $30 billion figure is a worldwide total, including Canada, the U.K., the U.S., mainland Europe, and Asia. Several versions of the worm were released in quick succession, named Sobig.A through Sobig.F, with Sobig.F being the most damaging. This cybercriminal program masqueraded as legitimate computer software attached to emails. It disrupted ticketing at Air Canada and interfered with countless other businesses. Despite its widespread damage, the creator of the successful bug was never caught.";; 1) echo -e "The worst computer virus outbreak in history, Mydoom caused estimated damage of $38 billion in 2004, but its inflation-adjusted cost is actually $52.2 billion. Also known as Novarg, this malware is technically a “worm,” spread by mass emailing. At one point, the Mydoom virus was responsible for 25% of all emails sent. Mydoom scraped addresses from infected machines, then sent copies of itself to those addresses. It also roped those infected machines into a web of computers called a botnet that performed distributed denial of service (DDoS) attacks. These attacks were intended to shut down a target website or server. Mydoom is still around today, generating 1% of all phishing emails. That’s no small feat considering the 3.4 billion phishing emails sent each day. By that figure, Mydoom has taken on a life of its own, infecting enough poorly-protected machines to send 1.2 billion copies of itself per year, 16 years after its creation. Though a $250,000 reward was offered, the developer of this dangerous computer worm was never caught.";; *) echo "${RED}Please select a number between 1 and 5${NORMAL}";; esac sleep 2 # 询问是否继续查看 read -p "Would you like to learn about another virus? yes/no : " y if [ "$y" != "yes" ]; then echo -e "Good-bye" exit 0 fi done
修改说明
- 新增了
while true死循环包裹核心逻辑,每次用户输入yes后会自动回到病毒列表选择页,支持重复选择 - 删除了原来输入
yes后打印旧病毒编号的无效逻辑,改为输入非yes值时直接退出脚本 - 给变量
$y加上了双引号包裹,避免用户输入空值时报语法错误
内容的提问来源于stack exchange,提问作者Morpheyous
相关产品推荐
相关产品推荐

