0

我需要制作这个 bash 脚本:

#!bin/bash
echo "Checking if root…."
if [[ $(/usr/bin/id -u) -ne 0 ]]; then
    echo "Not running as root"
    exit
fi
echo -ne "Downloading NEW hosts blocking file: "
wget -qO /tmp/hosts.txt http://deathsrepo.webege.com/hosts.txt
echo "DONE!"
mv /tmp/hosts.text -b etc/
wget -qO /tmp/hosts.bak http://deathsrepo.webege.com/hosts.bak
mv /tmp/hosts.bak -b etc/
rename .txt . *.text

能够将 hosts.text 重命名为 hosts。此外,当这个包被删除时,我需要以 hosts.bak 命名。

4

2 回答 2

0

1 - 删除原始主机:

rm /etc/hosts

2 - 重命名hosts.text:

mv /tmp/hosts.text /tmp/hosts

3 - 将hosts.text移动到/etc:

mv /tmp/hosts.text /etc

您可以使用mv同时移动重命名。我还建议进行备份而不是删除原始主机:

mv /etc/hosts /etc/hosts.bak   # Make a backup
mv /tmp/hosts.text /etc/hosts  # Move and rename

我并没有真正明白你想要做什么,但我认为你的完整脚本会变成这样:

#!bin/bash
echo "Checking if root…."
if [[ $(/usr/bin/id -u) -ne 0 ]]; then
    echo "Not running as root"
    exit
fi
echo -ne "Downloading NEW hosts blocking file: "
wget -qO /tmp/hosts.txt http://deathsrepo.webege.com/hosts.txt
echo "DONE!"
mv /tmp/hosts.txt /etc/hosts  # replace original hosts with downloaded hosts.txt
wget -qO /tmp/hosts.bak http://deathsrepo.webege.com/hosts.bak
mv /tmp/hosts.bak /etc        # move downloaded hosts.bak to /etc
于 2013-02-15T04:44:42.317 回答
0

要重命名文件,只需移动它:

mv hosts.text /etc/hosts

如果您的目标是一个目录,您的文件将被移入其中,名称不变:

mv hosts.text /etc/  # file is moved to /etc/hosts.text

如果目标是文件,并且目标文件存在,则源将移动到指定的目标文件,并覆盖该目标的文件:

mv hosts.text /etc/hosts   # file is moved and renamed to /etc/hosts

如果目标不存在,并且缺少尾部斜杠,您的文件将被重命名为目标:

mv hosts.text foobar.txt  # file is renamed to foobar.txt
于 2013-02-15T03:10:48.233 回答