Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learning Linux Shell Scripting

You're reading from   Learning Linux Shell Scripting Unleash the power of shell scripts to solve real-world problems by breaking through the practice of writing tedious code

Arrow left icon
Product type Paperback
Published in Dec 2015
Publisher
ISBN-13 9781785286216
Length 306 pages
Edition 1st Edition
Arrow right icon
Author (1):
Arrow left icon
Ganesh Sanjiv Naik Ganesh Sanjiv Naik
Author Profile Icon Ganesh Sanjiv Naik
Ganesh Sanjiv Naik
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. Getting Started and Working with Shell Scripting FREE CHAPTER 2. Drilling Deep into Process Management, Job Control, and Automation 3. Using Text Processing and Filters in Your Scripts 4. Working with Commands 5. Exploring Expressions and Variables 6. Neat Tricks with Shell Scripting 7. Performing Arithmetic Operations in Shell Scripts 8. Automating Decision Making in Scripts 9. Working with Functions 10. Using Advanced Functionality in Scripts 11. System Startup and Customizing a Linux System 12. Pattern Matching and Regular Expressions with sed and awk Index

Our first script – Hello World

Since we learned basic commands to use Linux OS, we will now write our first Shell script called hello.sh. You can use any editor of your choice such as vi, gedit, nano, and other similar editors. I prefer to use the vi editor.

  1. Create a new hello.sh file as follows:
    #!/bin/bash
    # This is comment line
    echo "Hello World"
    ls
    date
    
  2. Save the newly created file.

The #!/bin/bash line is called the shebang line. The combination of the characters # and ! is called the magic number. The shell uses this to call the intended shell such as /bin/bash in this case. This should always be the first line in a Shell script.

The next few lines in the Shell script are self explanatory.

  • Any line starting with #, will be treated as a comment line. An exception to this would be the first line with #!/bin/bash
  • The echo command will print Hello World on the screen
  • The ls command will display directory content on the console
  • The date command will show the current date and time

We can execute the newly created file by the following commands:

  • Technique one:
    $ bash hello.sh
    
  • Technique two:
    $ chmod +x hello.sh
    

By running any of the preceding commands, we are adding executable permission to our newly created file. You will learn more about file permissions in later in this same chapter.

$ ./hello.sh

By running the preceding command, we are executing hello.sh as the executable file. By technique one, we passed filename as an argument to Bash shell.

The output of executing hello.sh will be as follows:

Hello World
hello.sh
Sun Jan 18 22:53:06 IST 2015

Since we have successfully executed our first script, we will proceed to develop a more advanced script, hello1.sh. Please create the new script hello.sh as follows:

#!/bin/bash
# This is the first Bash shell
# Scriptname : Hello1.sh
# Written by:  Ganesh Naik
echo "Hello $LOGNAME, Have a nice day !"
echo "Your are working in directory `pwd`."
echo "You are working on a machine called `uname -n`."
echo "List of files in your directory is."
ls      # List files in the present working directory
echo  "Bye for now $LOGNAME. The time is `date +%T`!"

The output of executing hello.sh will be as follows:

Hello student, Have a nice day !.
Your are working in directory /home/student/work.
You are working on a machine called ubuntu.
List of files in your directory is.
hello1.sh  hello.sh
Bye for now student. The time is 22:59:03!

You will learn about the LOGNAME, uname, and other similar commands as we go on with the book.

You have been reading a chapter from
Learning Linux Shell Scripting
Published in: Dec 2015
Publisher:
ISBN-13: 9781785286216
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime