设置ubuntu bash_如何仅使用Dotfile和Bash脚本设置新的Ubuntu桌面

设置ubuntu bash

One of my most favourite things about open source files on GitHub is the ability to see how others do (what some people might call) mundane things, like set up their .bashrc and other dotfiles. While I’m not as enthusiastic about ricing as I was when I first came to the Linux side, I still get pretty excited when I find a config setting that makes things prettier and faster, and thus, better.

关于GitHub上的开源文件,我最喜欢的事情之一就是能够查看其他人的行为(有人称其为平凡的事情),例如设置他们的.bashrc和其他dotfile。 虽然我不像刚接触Linux时那样热衷于调音,但是当我发现使事情变得更漂亮,更快,从而更好的配置设置时,我仍然感到非常兴奋。

I recently came across a few such things, particularly in Tom Hudson’s dotfiles. Tom seems to like to script things, and some of those things include automatically setting up symlinks, and installing Ubuntu repository applications and other programs. This got me thinking. Could I automate the set up of a new machine to replicate my current one?

我最近遇到了一些这样的事情,特别是在汤姆·哈德森(Tom Hudson)的 dotfile中。 Tom似乎喜欢编写脚本,其中包括自动设置符号链接,安装Ubuntu存储库应用程序和其他程序。 这让我开始思考。 我可以自动设置新机器以复制当前机器吗?

Being someone generally inclined to take things apart in order to see how they work, I know I’ve messed up my laptop on occasion. (Usually when I’m away from home, and my backup harddrive isn’t.) On those rare but really inconvenient situations when my computer becomes a shell of its former self, (ba-dum-ching) it’d be quite nice to have a fast, simple way of putting Humpty Dumpty back together again, just the way I like.

作为通常倾向于分解事物以了解其工作原理的人,我知道我有时会弄乱笔记本电脑。 (通常在我不在家的时候,而我的备份硬盘驱动器不在。)在那些罕见但确实不方便的情况下,当我的计算机成为其前身的外壳时,(ba-dum-ching)会非常好以一种快速,简单的方式将“矮胖子”重新组合在一起,就像我喜欢的方式一样。

In contrast to creating a disk image and restoring it later, a collection of bash scripts is easier to create, maintain, and move around. They require no special utilities, only an external transportation method. It’s like passing along the recipe, instead of the whole bundt cake. (Mmm, cake.)

与创建磁盘映像并在以后还原相比 ,bash脚本的集合更易于创建,维护和移动。 他们不需要特殊的工具,只需一种外部运输方式。 这就像传递食谱,而不是整个蛋糕。 (嗯,蛋糕。)

Additionally, functionality like this would be super useful when setting up a virtual machine, or VM, or even just a virtual private server, or VPS. (Both of which, now that I write this, would probably make more forgiving targets for my more destructive experimentations… live and learn!)

此外,在设置虚拟机或VM或什至只是虚拟专用服务器或VPS时,此类功能将非常有用。 (由于我现在写的是这两种方式,它们可能会为我更具破坏性的实验提供更多宽容的目标……生活和学习!)

Well, after some grepping and Googling and digging around, I now have a suite of scripts that can do this:

好了,经过一番摸索,谷歌搜索和深入研究之后,我现在有了一套可以做到这一点的脚本:

See a video of the setup here.

在此处观看有关设置的视频

This is the tail end of a test run of the set up scripts on a fresh Ubuntu desktop, loaded off a bootable USB. It had all my programs and settings restored in under three minutes!

这是在可启动USB上加载的全新Ubuntu桌面上对设置脚本进行测试的尾声。 在不到三分钟的时间内恢复了我所有的程序和设置!

This post will cover how to achieve the automatic set up of a computer running Ubuntu Desktop (in my case, Ubuntu LTS 18.04) using bash scripts. The majority of the information covered is applicable to all the Linux desktop flavours, though some syntax may differ. The bash scripts cover three main areas: linking dotfiles, installing software from Ubuntu and elsewhere, and setting up the desktop environment. We’ll cover each of these areas and go over the important bits so that you can begin to craft your own scripts.

这篇文章将介绍如何使用bash脚本自动设置运行Ubuntu Desktop(在我的情况下为Ubuntu LTS 18.04)的计算机。 涵盖的大部分信息适用于所有Linux桌面版本,尽管某些语法可能有所不同。 bash脚本涵盖三个主要领域:链接点文件,从Ubuntu和其他地方安装软件以及设置桌面环境。 我们将覆盖所有这些领域,并介绍重要的部分,以便您可以开始编写自己的脚本。

点文件 (Dotfiles)

Dotfiles are what most Linux enthusiasts call configuration files. They typically live in the user’s home directory (denoted in bash scripts with the builtin variable $HOME) and control the appearance and behaviour of all kinds of programs. The file names begin with ., which denotes hidden files in Linux (hence “dot” files). Here are some common dotfiles and ways in which they’re useful.

点文件是大多数Linux爱好者称为配置文件的文件。 它们通常位于用户的主目录(在带有内置变量$HOME bash脚本中表示)中,并控制各种程序的外​​观和行为。 文件名以开头. ,表示Linux中的隐藏文件(因此称为“点”文件)。 以下是一些常见的点文件及其使用方式。

.bashrc (.bashrc)

The .bashrc file is a list of commands executed at startup by interactive, non-login shells. Interactive vs non-interactive shells can be a little confusing, but aren’t necessary for us to worry about here. For our purposes, any time you open a new terminal, see a prompt, and can type commands into it, your .bashrc was executed.

.bashrc文件是在启动时由交互式非登录外壳程序执行的命令的列表。 交互式和非交互式shell可能会有些混乱,但是我们不必为此担心。 出于我们的目的,每当您打开新终端,看到提示并可以在其中键入命令时, .bashrc便会执行。

Lines in this file can help improve your workflow by creating aliases that reduce keystrokes, or by displaying a helpful prompt with useful information. It can even run user-created programs, like Eddie. For more ideas, you can have a look at my .bashrc file on GitHub.

该文件中的行可以通过创建减少击键次数的别名或显示有用信息的有用提示来帮助改善您的工作流程。 它甚至可以运行用户创建的程序,例如Eddie 。 有关更多想法,您可以查看我在GitHub上的.bashrc文件

.vimrc (.vimrc)

The .vimrc dotfile configures the champion of all text editors, Vim. (If you haven’t yet wielded the powers of the keyboard shortcuts, I highly recommend a fun game to learn Vim with.)

.vimrc文件配置所有文本编辑器Vim的冠军。 (如果您尚未使用键盘快捷键的功能,我强烈建议您使用一个有趣的游戏来学习Vim 。)

In .vimrc, we can set editor preferences such as display settings, colours, and custom keyboard shortcuts. You can take a look at my .vimrc on GitHub.

.vimrc ,我们可以设置编辑器首选项,例如显示设置,颜色和自定义键盘快捷键。 您可以在GitHub上查看我的.vimrc

Other dotfiles may be useful depending on the programs you use, such as .gitconfig or .tmux.conf. Exploring dotfiles on GitHub is a great way to get a sense of what’s available and useful to you!

根据您使用的程序,其他点文件可能也有用,例如.gitconfig.tmux.conf 。 在GitHub上探索dotfile是一种了解对您有用和有用的好方法!

链接点文件 (Linking dotfiles)

We can use a script to create symbolic links, or symlinks for all our dotfiles. This allows us to keep all the files in a central repository, where they can easily be managed, while also providing a sort of placeholder in the spot that our programs expect the configuration file to be found. This is typically, but not always, the user home directory. For example, since I store my dotfiles on GitHub, I keep them in a directory with a path like ~/github/dotfiles/ while the files themselves are symlinked, resulting in a path like ~/.vimrc.

我们可以使用脚本为所有点文件创建符号链接或符号链接 。 这使我们可以将所有文件都保存在一个中央存储库中,可以轻松地对其进行管理,同时还可以在程序希望找到配置文件的位置提供某种占位符。 这通常是(但不总是)用户主目录。 例如,由于我将点文件存储在GitHub上,因此我将它们保存在目录中,该目录的路径为~/github/dotfiles/而文件本身是~/github/dotfiles/链接的,因此生成的路径为~/.vimrc

To programmatically check for and handle any existing files and symlinks, then create new ones, we can use this elegant shell script. I compliment it only because I blatantly stole the core of it from Tom’s setup script, so I can’t take the credit for how lovely it is.

要以编程方式检查和处理任何现有文件和符号链接,然后创建新文件和符号链接,我们可以使用此精美的Shell脚本 。 我之所以称赞它,是因为我从Tom的安装脚本中偷走了它的核心,所以我不能因为它的可爱而功劳。

The symlink.sh script works by attempting to create symlinks for each dotfile in our $HOME. It first checks to see if a symlink already exists, or if a regular file or directory with the same name exists. In the former case, the symlink is removed and remade; in the latter, the file or directory is renamed, then the symlink is made.

symlink.sh脚本通过尝试为$HOME每个点文件创建符号链接来工作。 它首先检查符号链接是否已经存在,或者是否存在具有相同名称的常规文件或目录。 在前一种情况下,符号链接被删除并重新制作; 在后者中,文件或目录被重命名,然后建立符号链接。

安装软件 (Installing software)

One of the beautiful things about exploring shell scripts is discovering how much can be achieved using only the command line. As someone whose first exposure to computers was through a graphical operating system, I find working in the terminal to be refreshingly fast.

探索shell脚本的美丽之处之一就是发现仅使用命令行就可以实现多少。 作为最初通过图形操作系统接触计算机的人,我发现在终端上的工作非常快。

With Ubuntu, most programs we likely require are available through the default Ubuntu software repositories. We typically search for these with the command apt search <program> and install them with sudo apt install <program>. Some software we’d like may not be in the default repositories, or may not be offered there in the most current version. In these cases, we can still install these programs in Ubuntu using a PPA, or Personal Package Archive. We’ll just have to be careful that the PPAs we choose are from the official sources.

对于Ubuntu,我们可能需要的大多数程序都可以通过默认的Ubuntu软件存储库获得。 我们通常使用命令apt search <program>搜索这些,并使用sudo apt install <program> 。 我们想要的某些软件可能不在默认存储库中,或者可能没有在最新版本中提供。 在这些情况下,我们仍然可以使用PPA或Personal Package Archive在Ubuntu中安装这些程序。 我们只需要注意,我们选择的PPA来自官方资源。

If a program we’d like doesn’t appear in the default repositories or doesn’t seem to have a PPA, we may still be able to install it via command line. A quick search for “installation command line” should get some answers.

如果我们想要的程序没有出现在默认存储库中或似乎没有PPA,我们仍然可以通过命令行来安装它。 快速搜索“安装命令行”应该会得到一些答案。

Since bash scripts are just a collection of commands that we could run individually in the terminal, creating a script to install all our desired programs is as straightforward as putting all the commands into a script file. I chose to organize my installation scripts between the default repositories, which are installed by my aptinstall.sh script, and programs that involve external sources, handled with my programs.sh script.

由于bash脚本只是我们可以在终端中单独运行的命令的集合,因此创建脚本来安装我们所需的所有程序就像将所有命令放入脚本文件一样简单。 我选择了组织的默认存储库,这是由安装之间我的安装脚本aptinstall.sh脚本涉及到外部资源,以处理和程序programs.sh脚本。

设置桌面环境 (Setting up the desktop environment)

On the recent occasions when I’ve gotten a fresh desktop (intentionally or otherwise) I always seem to forget how long it takes to remember, find, and then change all the desktop environment settings. Keyboard shortcuts, workspaces, sound settings, night mode… it adds up!

在最近获得新桌面的情况下(有意或无意),我似乎总是忘记记住,查找然后更改所有桌面环境设置所花的时间。 键盘快捷键,工作区,声音设置,夜间模式……总之!

Thankfully, all these settings have to be stored somewhere in a non-graphical format, which means that if we can discover how that’s done, we can likely find a way to easily manipulate the settings with a bash script. Lo and behold the terminal command, gsettings list-recursively.

值得庆幸的是,所有这些设置都必须以非图形格式存储在某处,这意味着,如果我们能够发现这样做的方式,我们很可能会找到一种使用bash脚本轻松操纵设置的方法。 gsettings list-recursively命令和终端命令。

There are a heck of a lot of settings for GNOME desktop environment. We can make the list easier to scroll through (if, like me, you’re sometimes the type of person to say “Just let me look at everything and figure out what I want!”) by piping to less: gsettings list-recursively | less. Alternatively, if we have an inkling as to what we might be looking for, we can use grep: gsettings list-recursively | grep 'keyboard'.

GNOME桌面环境有很多设置。 我们可以通过less gsettings list-recursively | less滚动操作(如果像我一样,如果您有时像是说“只是让我看看一切并弄清楚我想要的东西!”的人),可以通过less gsettings list-recursively | lessgsettings list-recursively | less gsettings list-recursively | less 。 另外,如果我们有一个暗示,以什么我们可能会寻找,我们可以使用grepgsettings list-recursively | grep 'keyboard' gsettings list-recursively | grep 'keyboard'

We can manipulate our settings with the gsettings set command. It can sometimes be difficult to find the syntax for the setting we want, so when we’re first building our script, I recommend using the GUI to make the changes, then finding the gsettings line we changed and recording its value.

我们可以使用gsettings set命令来操纵我们的设置。 有时可能很难找到所需设置的语法,因此,当我们第一次构建脚本时,我建议使用GUI进行更改,然后找到我们更改的gsettings行并记录其值。

For some inspiration, you can view my desktop.sh settings script on GitHub.

为了获得一些启发,您可以在GitHub上查看我的desktop.sh设置脚本

全部放在一起 (Putting it all together)

Having modular scripts (one for symlinks, two for installing programs, another for desktop settings) is useful for both keeping things organized and for being able to run some but not all of the automated set up. For instance, if I were to set up a VPS in which I only use the command line, I wouldn’t need to bother with installing graphical programs or desktop settings.

拥有模块化脚本(一个用于符号链接,两个用于安装程序,另一个用于桌面设置)对于使事情井井有条,并且能够运行某些(但不是全部)自动设置非常有用。 例如,如果要设置仅使用命令行的VPS,则无需安装图形程序或桌面设置。

In cases where I do want to run all the scripts, however, doing so one-by-one is a little tedious. Thankfully, since bash scripts can themselves be run by terminal commands, we can simply write another master script to run them all!

但是,在确实要运行所有脚本的情况下,一个接一个地执行会有点乏味。 值得庆幸的是,由于bash脚本本身可以通过终端命令运行,因此我们只需编写另一个主脚本即可运行它们!

Here’s my master script to handle the set up of a new Ubuntu desktop machine:

这是我的主脚本,用于处理新的Ubuntu台式机的设置:

#!/bin/bash

./symlink.sh
./aptinstall.sh
./programs.sh
./desktop.sh

# Get all upgrades
sudo apt upgrade -y

# See our bash changes
source ~/.bashrc

# Fun hello
figlet "... and we're back!" | lolcat

I threw in the upgrade line for good measure. It will make sure that the programs installed on our fresh desktop have the latest updates. Now a simple, single bash command will take care of everything!

我投入了升级线,以取得良好的效果。 这将确保在我们全新的桌面上安装的程序具有最新更新。 现在,一个简单的单bash命令将处理所有事情!

You may have noticed that, while our desktop now looks and runs familiarly, these scripts don’t cover one very important area: our files. Hopefully, you have a back up method for those that involves some form of reliable external hardware. If not, and if you tend to put your work in external repository hosts like GitHub or GitLab, I do have a way to automatically clone and back up your GitHub repositories with bash one-liners.

您可能已经注意到,尽管我们的桌面现在看起来和运行起来很熟悉,但这些脚本并未涵盖一个非常重要的领域:我们的文件。 希望您为那些涉及某种形式的可靠外部硬件的用户提供了一种备份方法。 如果不是这样,并且如果您倾向于将工作放在GitHub或GitLab之类的外部存储库主机中,那么我确实可以使用bash one-liners自动克隆和备份GitHub存储库

Relying on external repository hosts doesn’t offer 100% coverage, however. Files that you wouldn’t put in an externally hosted repository (private or otherwise) consequently can’t be pulled. Git ignored objects that can’t be generated from included files, like private keys and secrets, will not be recreated. Those files, however, are likely small enough that you could fit a whole bunch on a couple encrypted USB flash drives (and if you don’t have private key backups, maybe you ought to do that first?).

但是,依靠外部存储库主机无法提供100%的覆盖率。 因此,无法将您不会放入外部托管存储库(私有或其他方式)中的文件提取。 Git忽略的对象无法从包含的文件生成,例如私钥和秘密,将不会重新创建。 但是,这些文件可能足够小,可以将整个文件包容纳在一对加密的USB闪存驱动器上(如果没有私钥备份,也许您应该首先这样做?)。

That said, I hope this post has given you at least some inspiration as to how dotfiles and bash scripts can help to automate setting up a fresh desktop. If you come up with some settings you find useful, please help others discover them by sharing your dotfiles, too!

就是说,我希望本文至少对点文件和bash脚本如何帮助自动设置新的桌面有所启发。 如果您想出一些有用的设置,请也通过共享您的点文件来帮助其他人发现它们!

翻译自: https://www.freecodecamp.org/news/how-to-set-up-a-fresh-ubuntu-desktop-using-only-dotfiles-and-bash-scripts/

设置ubuntu bash

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值