Ubuntu Unleashed 2019 Edition: Covering 18.04, 18.10, 19.04

(singke) #1
command,    used    to  start   an  X   Window  session from    the text    console,    is  a
shell script used every day by most users. To learn more about shell
scripting with bash, see the “Advanced Bash-Scripting Guide,” listed in
the “References” section at the end of this chapter. You’ll also find Teach
Yourself Shell Programming in 24 Hours a helpful guide to learning more
about using the shell to build your own commands.

When you are learning to write and execute your first shell scripts, start with
scripts for simple but useful tasks. Begin with short examples and then
expand the scripts as you build on your experience and knowledge. Make
liberal use of comments (lines preceded with a pound sign, #) to document
each section of your script. Include an author statement and an overview of
the script as additional help, along with a creation date or version number.
Write shell scripts using a text editor such as vi because it does not
automatically wrap lines of text. Line wrapping can break script syntax and
cause problems. If you use the nano editor, include its -w flag to disable line
wrapping.


In this section, you learn how to write a simple shell script to set up a number
of aliases (command synonyms) whenever you log on. Instead of typing all
the aliases every time you log on, you can put them in a file by using a text
editor, such as vi, and then execute the file. Normally these changes are
saved in system-wide shell configuration files under the /etc directory to
make the changes active for all users or in your .bashrc, .cshrc (if you
use tcsh), or .bash_profile files in your home directory.


Here is what is contained in myenv, a sample shell script created for this
purpose (for bash):


Click here to view code image
#!/bin/sh
alias ll='ls –L'
alias ldir='ls –aF'
alias copy='cp'


This simple script creates command aliases, or convenient shorthand forms of
commands, for the ls and cp commands. The ll alias provides a long
directory listing: The ldir alias is the ls command but prints indicators (for
directories or executable files) in listings. The copy alias is the same as the
cp command. You can experiment and add your own options or create aliases
of other commands with options you frequently use.


You can execute myenv in a variety of ways under Linux. As shown in this
example, you can make myenv executable by using the chmod command

Free download pdf