我编写的 Shell 脚本非常冗长,而且效率可能很低。救命!

我编写的 Shell 脚本非常冗长,而且效率可能很低。救命!

我是 Ubuntu 的新手,到目前为止我已经做了几件让我感到自豪的事情:

用我自己的 html 制作了一个 apache 网络服务器(一周前我从未碰过它)制作了一个 stratum 挖掘服务器 +titcoin 节点

我最近的项目是制作一个 shell 脚本,将我的整个(没错)硬盘备份到一个巨大的外部硬盘上。说到重点,我知道还有很多可以改进的地方(比如它会告诉你它所记录的所有内容?),我想听听你的看法:

代码:

#!/bin/bash
read -n1 -p “Would you like to back up your system? Uno for Si, and Dos for No.” 
echo
case $input in
1) fdisk -l;
echo;
read -p “Please enter the name of your backup disk that was displayed in the fdisk window: ” disk ;
echo;
echo “Backups now in progress! Please be aware that backups are saved to a Backups directory which this script creates, and backups are tarred and labelled by date!”;
echo “THIS WILL TAKE A VERY VERY LONG TIME IF YOU HAVE A LARGE FILESYSTEM! PLEASE BE PATIENT!!!”;
cd /;
sudo mkdir -p /mnt/$disk && sudo mount /dev/$disk /mnt/$disk && sudo mkdir -p /mnt/$disk/Backups && sudo tar -cp $(date +%Y%m%d).tar --directory=/ --exclude=proc --exclude=sys --exclude=dev/pts --exclude=backups . && sudo cp $(date +%Y%m%d-%H).tar /mnt/$disk/Backups;
echo “Okay, all done! To restore your system, you will have to untar and manually do it yourself. If you have any issues, email [email protected]!”;; 
2) echo “Then why are you running this script?”;;
*) echo “I'm sorry, but I can’t do that…”

esac

说实话,我不知道自己在做什么。有什么建议吗?以下是大约 3 小时的输出摘录...实际上,文本移动得太快了,我甚至无法复制/粘贴它...

答案1

国际问题:

  • 使用缩进. 它使您的代码更具可读性(对您和我们而言)。
  • 在 bash 中仅使用直引号. 其他类型的引号可能会破坏命令。
  • 不要;在单个命令的末尾附加。该;字符具有特定含义,如果附加到单个命令则毫无用处。

具体问题:

fdisk -l;

您需要fdisk以以下身份运行sudosudo fdisk -l

read -n1 -p “Would you like to back up your system? Uno for Si, and Dos for No.”

您没有将此输入存储在任何地方。查看以下代码,您遗漏了input结尾部分(请注意双引号后的空格):read -n1 -p "Would you like to back up your system? Uno for Si, and Dos for No." input

sudo mkdir -p /mnt/$disk && sudo mount /dev/$disk /mnt/$disk && sudo mkdir -p /mnt/$disk/Backups && sudo tar -cp $(date +%Y%m%d).tar --directory=/ --exclude=proc --exclude=sys --exclude=dev/pts --exclude=backups . && sudo cp $(date +%Y%m%d-%H).tar /mnt/$disk/Backups;

tar命令缺少一个-f选项:如果没有选项,它将tar输出到stdout指定文件而不是指定文件;此外,文件名应该与$(date +%Y%m%d-%H)预期的文件名相匹配cpsudo mkdir -p /mnt/$disk && sudo mount /dev/$disk /mnt/$disk && sudo mkdir -p /mnt/$disk/Backups && sudo tar -cpf $(date +%Y%m%d-%H).tar --directory=/ --exclude=proc --exclude=sys --exclude=dev/pts --exclude=backups . && sudo cp $(date +%Y%m%d-%H).tar /mnt/$disk/Backups;

最后说明:这样做,您将保留所有备份/,但您还将/backups/从备份中排除一个目录,因此您的目标似乎是将每个备份的副本存储在这样的文件夹中。确保这样的文件夹存在。根据此命令,最后一个命令的更正版本是:

sudo mkdir -p /mnt/$disk && sudo mount /dev/$disk /mnt/$disk && sudo mkdir -p /mnt/$disk/Backups && sudo tar -cpf backups/$(date +%Y%m%d-%H).tar --directory=/ --exclude=proc --exclude=sys --exclude=dev/pts --exclude=backups . && sudo cp backups/$(date +%Y%m%d-%H).tar /mnt/$disk/Backups;

因此,缩进的、直引用的、无用的;、固定版本的脚本是:

#!/bin/bash

read -n1 -p "Would you like to back up your system? Uno for Si, and Dos for No." input
echo
case $input in
    1)
        sudo fdisk -l
        echo
        read -p "Please enter the name of your backup disk that was displayed in the fdisk window: " disk
        echo
        echo "Backups now in progress! Please be aware that backups are saved to a Backups directory which this script creates, and backups are tarred and labelled by date!"
        echo "THIS WILL TAKE A VERY VERY LONG TIME IF YOU HAVE A LARGE FILESYSTEM! PLEASE BE PATIENT!!!"
        cd /
        sudo mkdir -p /mnt/$disk && sudo mount /dev/$disk /mnt/$disk && sudo mkdir -p /mnt/$disk/Backups && sudo tar -cpf backups/$(date +%Y%m%d-%H).tar --directory=/ --exclude=proc --exclude=sys --exclude=dev/pts --exclude=backups . && sudo cp backups/$(date +%Y%m%d-%H).tar /mnt/$disk/Backups;
        echo "Okay, all done! To restore your system, you will have to untar and manually do it yourself. If you have any issues, email [email protected]!"
        ;;
    2)
        echo "Then why are you running this script?"
        ;;
    *)
        echo "I'm sorry, but I can’t do that…"
        ;;
esac

最后一件事:永远不要在网络上留下你的电子邮件地址。垃圾邮件发送者会将其删除。我已将其替换为[email protected]我的答案、您的答案和您的问题。

答案2

好的,明白了。

#!/bin/bash
read -n1 -p "Would you like to back up verbosely and with minimal directories?? Uno for Si, and Dos for No." input
echo
case $input in
1) fdisk -l
echo
cd /
mkdir TempBackup && cd TempBackup
read -p "Please enter the name of your backup disk that was displayed in the fdisk window: " disk 
echo
echo "Tarring your /etc/ folder, be patient!" && sudo tar -cpf etc.tar /etc && echo "etc is tarred, copying now!" && sudo cp etc.tar /mnt/$disk/Backups && echo "Tarring your /bin/ folder!" && sudo tar -cpf bin.tar /bin && echo "bin is tarred, copying now!" && sudo cp bin.tar /mnt/$disk/Backups && echo "Tarring your /home/ folder, if you use bitcoind or litecoind it will be a bit!" && sudo tar -cpf home.tar --exclude=.cache --exclude=.gvfs /home && echo "home is tarred, copying now!" && sudo cp home.tar /mnt/$disk/Backups && sudo tar -cpf var.tar /var && echo "var is tarred, copying now!" && sudo cp etc.tar /mnt/$disk/Backups && cd / && echo "Tarring usr, IT IS UNLIKELY THAT THIS FILE CAN BE COPIED ON A FAT32 FS!!! AUTOSPLIT HAS BEEN ACTIVATED!!!" && sudo tar -cpf usr.tar /usr && echo "usr is tarred, copying/splitting now!" && mkdir split-files && cd split-files && split --bytes=1G /usr.tar split.tar && sudo cp * /mnt/$disk/Backups && cat split*.tar > usr.tar && cd /TempBackup && echo "Okay, finished! Let me clean up..."  && sleep 5 && echo "Cleaning all files...." && sudo rm -rf *.tar && echo "All Done!"
2) fdisk -l
echo
cd /
mkdir TempBackup && cd TempBackup
read -p "Please enter the name of your backup disk that was displayed in the fdisk window: " disk 
echo
echo "Backups now in progress! Please be aware that backups are saved to a Backups directory which this script creates, and backups are tarred and labelled by date!"
echo "THIS WILL TAKE A VERY VERY LONG TIME IF YOU HAVE A LARGE FILESYSTEM! PLEASE BE PATIENT!!!"
echo "THIS IS NOT THE SAME AS THE VERBOSE MODE! THIS COPIES ALL FILES TO DISK, NOT JUST THE COMMON ONES! YOU STILL HAVE TIME TO QUIT!!!"
sleep 10
cd /;
sudo mkdir -p /mnt/$disk && sudo mount /dev/$disk /mnt/$disk && sudo mkdir -p /mnt/$disk/Backups && sudo tar -cpf $(date +%Y%m%d).tar --directory=/ --exclude=proc --exclude=sys --exclude=dev pts --exclude=backups . && sudo cp $(date +%Y%m%d-%H).tar /mnt/$disk/Backups
echo "Okay, all done! To restore your system, you will have to untar and manually do it yourself. If you have any issues, email [email protected]!"
*) echo "I'm sorry, but I can't do that..."
esac

请随意使用!

相关内容