How do I automate updates and file edits in a StackScript?

Linode Staff

Hello, I am very stumped on how to do these operations despite taking a browse though 7 pages of the Community's Q&A section that the "stackscripts" keyword provided me.

Below is what I am trying to do…

I'm using Ubuntu 18.04. Will these commands work in my StackScript even if there should be a Grub Update or something? If not, how do I account for that?

apt-get update
apt-get upgrade -y

I'm also wondering how I can both create and edit a file automatically from within my StackScript? Normally I would just use nano when I'm manually creating and editing. I want my script to create an empty configuration file and input the required text for it to "work".

1 Reply

In my own scripts, I've been using the following commands to automate running updates on Debian/Ubuntu systems without issue:

DEBIAN_FRONTEND=noninteractive apt-get update
DEBIAN_FRONTEND=noninteractive apt-get -y upgrade

You can also use debconf-set-selections to pre-select options for packages that normally use an ncurses menu during installation. For example, I have a script which installs mysql with the following selections:

echo "mysql-server mysql-server/root_password password ${db_root_password}" | debconf-set-selections
echo "mysql-server mysql-server/root_password_again password ${db_root_password}" | debconf-set-selections

As for creating and editing a file from a script, you have a few tools that you can use. If you just need to create a blank file and do nothing else with it, you can use one of the following commands:

touch file.txt
echo "" > file.txt

To add a line of text to a file:

echo "line of text" >> file.txt

To overwrite a whole file with new text:

cat "new text" > file.txt

To write multiple lines into a file, you can use a Here Document:

text="$(cat <<EOF
line 1
line 2
line 3
...
EOF
)"
# add new lines to end of file
echo "" >> file.txt

# or, overwrite the file entirely with the new lines
echo "" > file.txt

You can also use sed to edit files that already have data in them. sed is basically a text editor that you can use from the command line or in a script. There is far too much to learn about sed for a post, but here are two of the more useful commands:

# Replace a line of text in a file:
sed -i 's/old text/new text/g' file.txt

# Delete a line from a file:
sed -i '/text from line to delete/' file.txt

It's important to note that sed will act on every instance of old text that it encounters, so make sure your search strings are as specific as possible to ensure that you do not accidentally overwrite the wrong line(s) in your file.

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct