Skip to Content
Fedora Linux
book

Fedora Linux

by Chris Tyler
October 2006
Beginner
658 pages
17h 17m
English
O'Reilly Media, Inc.
Content preview from Fedora Linux

Writing Simple Scripts

bash command lines can get to be very long, especially when pipes are used. A script is a text file that contains shell commands that may itself be executed as a command, providing an easy way to reuse complex sequences of commands. In fact, bash provides a complete programming language for use in scripts.

How Do I Do That?

To create a script, simply place commands in a text file. For example, this script will display the ten largest files in the current directory:

ls -lS | tail -n +2 | head -10

Save this file as topten. In order to run the script, you will need to set read and execute permission:

$ chmod a+rx 
               
                  topten
               

The script can be executed by specifying the directory and filename (or an absolute pathname):

$ ./topten
-rw-r--r--  1 root root    807103 Jul 12 21:18 termcap
-rw-r--r--  1 root root    499861 Jul 17 08:08 prelink.cache
-rw-r--r--  1 root root    362031 Feb 23 08:09 services
-rw-r--r--  1 root root     97966 Jul 15 11:19 ld.so.cache
-rw-r--r--  1 root root     92794 Jul 12 12:46 Muttrc
-rw-r--r--  1 root root     83607 Mar 23 07:23 readahead.files
-rw-r--r--  1 root root     73946 Jul 13 02:23 sensors.conf
-rw-r--r--  1 root root     45083 Jul 12 18:33 php.ini
-rw-r--r--  1 root root     30460 Jul 13 20:36 jwhois.conf
-rw-r--r--  1 root root     26137 Mar 23 07:23 readahead.early.files

The directory name is required because the current directory (.) is not in the list of directories normally searched for commands (called the PATH). To make your script accessible to all users, move it to the ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

CentOS Quick Start Guide

CentOS Quick Start Guide

Shiwang Kalkhanda
Mastering FreeBSD and OpenBSD Security

Mastering FreeBSD and OpenBSD Security

Paco Hope, Bruce Potter, Yanek Korff
Linux Server Hacks

Linux Server Hacks

Rob Flickenger

Publisher Resources

ISBN: 0596526822Errata Page