创建一个简单的自定义 apt-get/config 脚本

创建一个简单的自定义 apt-get/config 脚本

随着我越来越多地使用 Ubuntu,我开始明白如何自动执行每次重新安装操作系统时执行的大部分重复命令。到目前为止,它只是一个巨大由数十个 && 符号连接的 apt-get 脚本/终端命令列表。

我以前对 Python 还算熟练,但现在已经忘记了很多。这是我现在的 apt-get 命令(我只需将其粘贴进去并按 Enter 键即可):

sudo add-apt-repository -y ppa:numix/ppa && sudo add-apt-repository -y ppa:gwendal-lebihan-dev/hexchat-stable && sudo add-apt-repository -y ppa:webupd8team/nemo && sudo apt-get update && sudo apt-get install -y numix-icon-theme numix-gtk-theme docky hexchat nemo nemo-fileroller dconf-tools vlc unity-tweak-tool gnome-tweak-tool python-pip && sudo pip install speedtest-cli && gsettings set org.gnome.desktop.background show-desktop-icons false && xdg-mime default nemo.desktop inode/directory application/x-gnome-saved-search && gsettings set org.gnome.desktop.interface gtk-theme "Numix" && gsettings set org.gnome.desktop.interface icon-theme 'Numix-Circle' && gsettings set com.canonical.desktop.interface scrollbar-mode normal

我知道,这并不完全干净。我不想继续这样做,我想把它变成一种脚本,这样我就可以更轻松地更新、维护和阅读它。以下是我输入的一些伪代码,说明了我想要做的事情。

#Install/Config Script

#Add Repos
print ("Adding Numix, Hexchat, and Webupd8 Repositories...")
enter.line ('sudo add-apt-repository -y ppa:numix/ppa && sudo add-apt-repository -y ppa:gwendal-lebihan-dev/hexchat-stable && sudo add-apt-repository -y ppa:webupd8team/nemo')

#Update Repos
print ("Updating repostiory lists...")
enter.line ('sudo apt-get update')

#Install apps
print ("Installing Numix Icon Theme...")
enter.line ('sudo apt-get install -y numix-icon-theme')
print ("Installing etc etc...")

#Configure Nemo File Explorer
print ("Configuring Nemo as default file manager")
enter.line ('gsettings set org.gnome.desktop.background show-desktop-icons false')
enter.line ('xdg-mime default nemo.desktop inode/directory application/x-gnome-saved-search')

#Set Numix Theme and fix scrollbars
print ("Setting Numix GTK and Icon Set...")
enter.line ('gsettings set org.gnome.desktop.interface gtk-theme "Numix"')
enter.line ('gsettings set org.gnome.desktop.interface icon-theme 'Numix-Circle'')
enter.line ('gsettings set com.canonical.desktop.interface scrollbar-mode normal
')

本质上,我希望能够将其保存为某种可执行脚本,然后简单地在终端中运行它并让它完成工作。我熟悉 Java、C++ 和 Python,但我已经很久没有编写代码了。

对于想要重返游戏的人,任何建议都将非常感激!请注意,我并不是要求任何人为我做所有的工作。我期待着自己尝试并失败,我只是希望得到一些关于如何开始、使用什么语言、组织布局和其他有用的入门技巧的建议。

答案1

使用 shell 脚本(bash):

#!/bin/bash
#above line ensures it runs with bash because of the '#!'
sudo apt-get update
sudo apt-get install -y numix-icon-theme package2 package3 package4
sudo apt-get install -y package5 package6 package7 etc

gsettings set org.gnome.desktop.interface gtk-theme "Numix"

或者,如果您知道您的偏好将会改变,您可以编写一个读取输入文件的脚本:

#newserver.in
install,numix-icon-theme,package2,package3
install,package4
custom,gsettings,"set org.gnome.desktop.interface gtk-theme \"Numix\""

读取输入文件

#!/bin/bash
infile=foobar.in
thisserver=""

cat $infile | while read line
do
   case $line in
      "#"*) [ ];; #matches commented lines
      "") [ ]  ;; #matches empty lines
      *)          #matches the rest
         #echo $line 
         while IFS=, read val1 val2 val3 val4 val5 val6
         do
            #echo "val1=$val1   val2=$val2   val3=$val3   val4=$val4"
            #echo "$thisserver    $line"
            if [[ "$thisserver" == author* ]] && [ "$val1" != "SERVER" ];
            then
               echo "$val1 $val2 wrote during the $val3"
            elif [[ "$thisserver" == movies* ]] && [ "$val1" != "SERVER" ];
            then
               echo "$val1 ($val2) rates $val3 on imdb.com"
            fi
            if [ "$val1" = "SERVER" ];
            then
               echo "$val2"
               thisserver=$val2
            fi

         done <<< $line
         ;;
   esac

done

foob​​ar.in

#foobar.in

# example comment
SERVER,authors.example.com,
alexandre,dumas,1700s
robert,heinlein,1900s,science fiction, nothing to see here, extra properties

timothy,zahn,1900s,
mark,twain,1800s,
SERVER,movies.example.com,
The Avengers,2012,8,
Star Wars,1997,9,
Ratatouille,2007,8,

答案2

只需将该行保存在名为的文件中do_stuff.sh。然后像这样运行它. path/to/do_stuff.sh

如果您确实想正确地执行此操作,只需#!/bin/bash在第一行添加即可chmod +x path/to/do_stuff.sh。然后您可以将其作为可执行文件运行。

无需(重新)学习 Python - Bash一种脚本语言。

相关内容