Sign inSign up

marlabsdocker/lesson12

By marlabsdocker

•Updated over 8 years ago

Introduction to Linux. Lesson 12. Introduction to Shell Scripting.

0

0

marlabsdocker/lesson12 repository overview

⁠Introduction to Shell Scripting.

First off, a shell script is simply a file that contains a series of Linux commands and shell statements. When a shell script is executed, it in turn executes the commands listed in the script. It starts at the top and executes the commands on each line, one line a time, until the end of the file.

By the way, the script's filename doesn't matter. You may be used to a different operating system where files have to have a specific file extension before they will be treated as an executable. (I'm talking about ".exe" files in Windows, for example.) That's not the case with Linux shell scripts.

By convention, shell scripts end in ".sh", but it is not a requirement. The following are all valid shell script names: script42.sh do_or_do_not_there_is_no_try ThisIsSomeRescue What IS important is that your shell script has execute permissions. (We'll get to that in just a second.)

The goal for this shell script is to collect and display some basic information about the Linux system.

If you want to follow along, open up a terminal on a Linux system and create a new file named "sysinfo.sh".

#vi sysinfo.sh

The very first line of every script you write should begin with "#!" followed by the path to an interpreter. We're going to write a bash shell script, so we'll use "#!/bin/bash". If you were going to write a Python script this would be "#!/usr/bin/python"; a ruby script would be "#!/usr/bin/ruby" and so on. One very common task you'll perform in your shell scripts is displaying messages on the screen. To do this, use the echo command followed by whatever you want to display to the screen and enclose it in quotes. Like this:

#!/bin/bash echo " System info scripts."

The next step is to add executable permissions to your script. You do this with the chmod command, which stands for "change mode". There are different ways to add executable permissions to a file, but the simplest is chmod +x FILE_NAME.

#chmod +x sysinfo.sh

Now you're ready to execute your shell script. You do this by typing "./" followed by the script name:

#./sysinfo.sh

You'll see the text you supplied to the echo command displayed on your screen.

Now that you have a working shell script with the proper permissions, you can continue writing your script. Let's add some commands that will display information about the Linux system.

First, display the current date and time when this information was collected.

#!/bin/bash echo " System info scripts." date

Next, display the hostname of the system.

#!/bin/bash echo " System info scripts." date hostname

Next, display the kernel release followed by the architecture. You'll need to use the uname command to do this.

#!/bin/bash echo " System info scripts." date hostname uname -r uname -m

Display the disk usage in a human readable format.

#!/bin/bash echo " System info scripts." date hostname uname -r uname -m df -h

Finally, end the script by letting the user know that it's done.

#!/bin/bash echo " System info scripts." date hostname uname -r uname -m df -h echo " End of sysinfo script."

It's time to see what this little script does! Save your changes and execute your script.

#./sysinfo.sh Now you have a script that displays some basic information about the system you're on. You could easily expand this to show information about the CPUs, memory, networking configuration and more.

Tag Summary

No tags have been pushed to this repository yet.