ACC SHELL

Path : /usr/share/doc/manual/opensuse-manuals_en/manual/
File Upload :
Current File : //usr/share/doc/manual/opensuse-manuals_en/manual/cha.adm.shell.html

<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Chapter 20. Bash and Bash Scripts</title><link rel="stylesheet" href="susebooks.css" type="text/css"><meta name="generator" content="DocBook XSL Stylesheets V1.75.2"><link rel="home" href="index.html" title="Documentation"><link rel="up" href="part.reference.system.html" title="Part IV. System"><link rel="prev" href="cha.udev.html" title="Chapter 19. Dynamic Kernel Device Management with udev"><link rel="next" href="part.reference.services.html" title="Part V. Services"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header" border="0" class="bctable"><tr><td width="80%"><div class="breadcrumbs"><p><a href="index.html"> Documentation</a><span class="breadcrumbs-sep"> &gt; </span><a href="book.opensuse.reference.html">Reference</a><span class="breadcrumbs-sep"> &gt; </span><a href="part.reference.system.html">System</a><span class="breadcrumbs-sep"> &gt; </span><strong><a accesskey="p" title="Chapter 19. Dynamic Kernel Device Management with udev" href="cha.udev.html"><span>&#9664;</span></a> </strong></p></div></td></tr></table></div><div class="chapter" title="Chapter 20. Bash and Bash Scripts"><div class="titlepage"><div><div><h2 class="title"><a name="cha.adm.shell"></a>Chapter 20. Bash and Bash Scripts<span class="permalink"><a alt="Permalink" title="Copy Permalink" href="#cha.adm.shell">¶</a></span></h2></div></div></div><div class="toc"><p><b>Contents</b></p><dl><dt><span class="sect1"><a href="cha.adm.shell.html#sec.adm.whatistheshell">20.1. What is <span class="quote">&#8220;<span class="quote">The Shell</span>&#8221;</span>?</a></span></dt><dt><span class="sect1"><a href="cha.adm.shell.html#sec.adm.shellscripts">20.2. Writing Shell Scripts</a></span></dt><dt><span class="sect1"><a href="cha.adm.shell.html#sec.adm.shell.redirect">20.3. Redirecting Command Events</a></span></dt><dt><span class="sect1"><a href="cha.adm.shell.html#sec.adm.alias">20.4. Using Aliases</a></span></dt><dt><span class="sect1"><a href="cha.adm.shell.html#sec.adm.variables">20.5. Using Variables in Bash</a></span></dt><dt><span class="sect1"><a href="cha.adm.shell.html#sec.adm.shell.grouping">20.6. Grouping And Combining Commands</a></span></dt><dt><span class="sect1"><a href="cha.adm.shell.html#sec.adm.shell.commonconstructs">20.7. Working with Common Flow Constructs</a></span></dt><dt><span class="sect1"><a href="cha.adm.shell.html#sec.adm.shell.moreinfo">20.8. For More Information</a></span></dt></dl></div><a class="indexterm" name="id471244"></a><a class="indexterm" name="id471249"></a><div class="abstract" title="Abstract"><p class="title"><b>Abstract</b></p><p>
   These days many people use computers with a graphical user interface
   (GUI) like KDE or GNOME. Although they offer lots of features, their use
   is limited when it comes to the execution of automatical tasks. Shells
   are a good addition to GUIs and this chapter gives you an overview of
   some aspects of shells, in this case Bash.
  </p></div><div class="sect1" title="20.1. What is &#8220;The Shell&#8221;?"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="sec.adm.whatistheshell"></a>20.1. What is <span class="quote">&#8220;<span class="quote">The Shell</span>&#8221;</span>?<span class="permalink"><a alt="Permalink" title="Copy Permalink" href="#sec.adm.whatistheshell">¶</a></span></h2></div></div></div><p>
   Traditionally, <span class="emphasis"><em>the</em></span> shell is Bash (Bourne again
   Shell). When this chapter speaks about <span class="quote">&#8220;<span class="quote">the shell</span>&#8221;</span> it means
   Bash. There are actually more available shells than Bash (ash, csh, ksh,
   zsh, &#8230;), each employing different features and characteristics. If
   you need further information about other shells, search for
   <span class="emphasis"><em>shell</em></span> in YaST.
  </p><div class="sect2" title="20.1.1. Knowing The Bash Configuration Files"><div class="titlepage"><div><div><h3 class="title"><a name="sec.adm.configfiles"></a>20.1.1. Knowing The Bash Configuration Files<span class="permalink"><a alt="Permalink" title="Copy Permalink" href="#sec.adm.configfiles">¶</a></span></h3></div></div></div><a class="indexterm" name="id471308"></a><p>
    A shell can be invoked as an:
   </p><div class="orderedlist"><ol class="orderedlist" type="1"><li><p>
      interactive login shell. This is used when logging in to a machine,
      invoking Bash with the <code class="option">--login</code> option or when logging
      in to a remote machine with SSH.
     </p></li><li><p>
      <span class="quote">&#8220;<span class="quote">ordinary</span>&#8221;</span> interactive shell. This is normally the case
      when starting xterm, konsole, gnome-terminal or similar tools.
     </p></li><li><p>
      non-interactive shell. This is used when invoking a shell script in
      the commandline.
     </p></li></ol></div><p>
    Depending on which type of shell you use, different configuration files
    are being read. The following tables show the login and non-login shell
    configuration files.
   </p><div class="table"><a name="tab.adm.shell.config.loginshells"></a><p class="title"><b>Table 20.1. Bash Configuration Files for Login Shells</b><span class="permalink"><a alt="Permalink" title="Copy Permalink" href="#tab.adm.shell.config.loginshells">¶</a></span></p><div class="table-contents"><table summary="Bash Configuration Files for Login Shells" border="1"><colgroup><col><col></colgroup><thead><tr><th>
        <p>
         File
        </p>
       </th><th>
        <p>
         Description
        </p>
       </th></tr></thead><tbody><tr><td>
        <p>
         <code class="filename">/etc/profile</code>
        </p>
       </td><td>
        <p>
         Do not modify this file, otherwise your modifications can be
         destroyed during your next update!
        </p>
       </td></tr><tr><td>
        <p>
         <code class="filename">/etc/profile.local</code>
        </p>
       </td><td>
        <p>
         Use this file if you extend <code class="filename">/etc/profile</code>
        </p>
       </td></tr><tr><td>
        <p>
         <code class="filename">/etc/profile.d/</code>
        </p>
       </td><td>
        <p>
         Contains system-wide configuration files for specific programs
        </p>
       </td></tr><tr><td>
        <p>
         <code class="filename">~/.profile</code>
        </p>
       </td><td>
        <p>
         Insert user specific configuration for login shells here
        </p>
       </td></tr></tbody></table></div></div><br class="table-break"><div class="table"><a name="tab.adm.shell.configs.nonloginshells"></a><p class="title"><b>Table 20.2. Bash Configuration Files for Non-Login Shells</b><span class="permalink"><a alt="Permalink" title="Copy Permalink" href="#tab.adm.shell.configs.nonloginshells">¶</a></span></p><div class="table-contents"><table summary="Bash Configuration Files for Non-Login Shells" border="1"><colgroup><col><col></colgroup><tbody><tr><td>
        <p>
         <code class="filename">/etc/bash.bashrc</code>
        </p>
       </td><td>
        <p>
         Do not modify this file, otherwise your modifications can be
         destroyed during your next update!
        </p>
       </td></tr><tr><td>
        <p>
         <code class="filename">/etc/bash.bashrc.local</code>
        </p>
       </td><td>
        <p>
         Use this file to insert your system-wide modifications for Bash
         only
        </p>
       </td></tr><tr><td>
        <p>
         <code class="filename">~/.bashrc</code>
        </p>
       </td><td>
        <p>
         Insert user specific configuration here
        </p>
       </td></tr></tbody></table></div></div><br class="table-break"><p>
    Additionally, Bash uses some more files:
   </p><div class="table"><a name="id471600"></a><p class="title"><b>Table 20.3. Special Files for Bash</b></p><div class="table-contents"><table summary="Special Files for Bash" border="1"><colgroup><col><col></colgroup><thead><tr><th>
        <p>
         File
        </p>
       </th><th>
        <p>
         Description
        </p>
       </th></tr></thead><tbody><tr><td>
        <p>
         <code class="filename">~/.bash_history</code>
        </p>
       </td><td>
        <p>
         Contains a list of all commands you have been typing
        </p>
       </td></tr><tr><td>
        <p>
         <code class="filename">~/.bash_logout</code>
        </p>
       </td><td>
        <p>
         Executed when logging out
        </p>
       </td></tr></tbody></table></div></div><br class="table-break"></div><div class="sect2" title="20.1.2. The Directory Structure"><div class="titlepage"><div><div><h3 class="title"><a name="id471693"></a>20.1.2. The Directory Structure<span class="permalink"><a alt="Permalink" title="Copy Permalink" href="#id471693">¶</a></span></h3></div></div></div><a class="indexterm" name="id471700"></a><p>
  The following table provides a short overview of the most important
  higher-level directories that you find on a Linux system. Find more
  detailed information about the directories and important subdirectories in
  the following list.
 </p><div class="table"><a name="id471715"></a><p class="title"><b>Table 20.4. Overview of a Standard Directory Tree</b></p><div class="table-contents"><table summary="Overview of a Standard Directory Tree" border="1"><colgroup><col><col></colgroup><thead><tr><th>
      <p>
       Directory
      </p>
     </th><th>
      <p>
       Contents
      </p>
     </th></tr></thead><tbody><tr><td>
      <p>
       <code class="filename">/</code> <a class="indexterm" name="id471762"></a>
      </p>
     </td><td>
      <p>
       Root directory&#8212;the starting point of the directory tree.
      </p>
     </td></tr><tr><td>
      <p>
       <code class="filename">/bin</code> <a class="indexterm" name="id471796"></a>
      </p>
     </td><td>
      <p>
       Essential binary files, such as commands that are needed by both the
       system administrator and normal users. Usually also contains the
       shells, such as Bash.
      </p>
     </td></tr><tr><td>
      <p>
       <code class="filename">/boot</code> <a class="indexterm" name="id471831"></a>
      </p>
     </td><td>
      <p>
       Static files of the boot loader.
      </p>
     </td></tr><tr><td>
      <p>
       <code class="filename">/dev</code> <a class="indexterm" name="id471865"></a>
      </p>
     </td><td>
      <p>
       Files needed to access host-specific devices.
      </p>
     </td></tr><tr><td>
      <p>
       <code class="filename">/etc</code> <a class="indexterm" name="id471899"></a>
      </p>
     </td><td>
      <p>
       Host-specific system configuration files.
      </p>
     </td></tr><tr><td>
      <p>
       <code class="filename">/home</code> <a class="indexterm" name="id471933"></a>
      </p>
     </td><td>
      <p>
       Holds the home directories of all users who have accounts on the
       system. However, <code class="systemitem">root</code>'s home directory is not located in
       <code class="filename">/home</code> but in <code class="filename">/root</code>.
      </p>
     </td></tr><tr><td>
      <p>
       <code class="filename">/lib</code> <a class="indexterm" name="id471981"></a>
      </p>
     </td><td>
      <p>
       Essential shared libraries and kernel modules.
      </p>
     </td></tr><tr><td>
      <p>
       <code class="filename">/media</code> <a class="indexterm" name="id472015"></a>
      </p>
     </td><td>
      <p>
       Mount points for removable media.
      </p>
     </td></tr><tr><td>
      <p>
       <code class="filename">/mnt</code> <a class="indexterm" name="id472049"></a>
      </p>
     </td><td>
      <p>
       Mount point for temporarily mounting a file system.
      </p>
     </td></tr><tr><td>
      <p>
       <code class="filename">/opt</code> <a class="indexterm" name="id472083"></a>
      </p>
     </td><td>
      <p>
       Add-on application software packages.
      </p>
     </td></tr><tr><td>
      <p>
       <code class="filename">/root</code> <a class="indexterm" name="id472117"></a>
      </p>
     </td><td>
      <p>
       Home directory for the superuser <code class="systemitem">root</code>.
      </p>
     </td></tr><tr><td>
      <p>
       <code class="filename">/sbin</code> <a class="indexterm" name="id472157"></a>
      </p>
     </td><td>
      <p>
       Essential system binaries.
      </p>
     </td></tr><tr><td>
      <p>
       <code class="filename">/srv</code> <a class="indexterm" name="id472191"></a>
      </p>
     </td><td>
      <p>
       Data for services provided by the system.
      </p>
     </td></tr><tr><td>
      <p>
       <code class="filename">/tmp</code> <a class="indexterm" name="id472225"></a>
      </p>
     </td><td>
      <p>
       Temporary files.
      </p>
     </td></tr><tr><td>
      <p>
       <code class="filename">/usr</code> <a class="indexterm" name="id472258"></a>
      </p>
     </td><td>
      <p>
       Secondary hierarchy with read-only data.
      </p>
     </td></tr><tr><td>
      <p>
       <code class="filename">/var</code> <a class="indexterm" name="id472292"></a>
      </p>
     </td><td>
      <p>
       Variable data such as log files.
      </p>
     </td></tr><tr><td>
      <p>
       <code class="filename">/windows</code> <a class="indexterm" name="id472326"></a>
      </p>
     </td><td>
      <p>
       Only available if you have both Microsoft Windows* and Linux
       installed on your system. Contains the Windows data.
      </p>
     </td></tr></tbody></table></div></div><br class="table-break"><p>
  The following list provides more detailed information and gives some
  examples of which files and subdirectories can be found in the
  directories:
 </p><div class="variablelist"><dl><dt><span class="term"><code class="filename">/bin</code><a class="indexterm" name="id472367"></a>
   </span></dt><dd><p>
     Contains the basic shell commands that may be used both by <code class="systemitem">root</code>
     and by other users. These commands include <span class="command"><strong>ls</strong></span>,
     <span class="command"><strong>mkdir</strong></span>, <span class="command"><strong>cp</strong></span>, <span class="command"><strong>mv</strong></span>,
     <span class="command"><strong>rm</strong></span> and <span class="command"><strong>rmdir</strong></span>.
     <code class="filename">/bin</code> also contains Bash, the default shell in
     openSUSE.
    </p></dd><dt><span class="term"><code class="filename">/boot</code><a class="indexterm" name="id472428"></a>
   </span></dt><dd><p>
     Contains data required for booting, such as the boot loader, the
     kernel, and other data that is used before the kernel begins executing
     user-mode programs.
    </p></dd><dt><span class="term"><code class="filename">/dev</code><a class="indexterm" name="id472456"></a>
   </span></dt><dd><p>
     Holds device files that represent hardware components.
    </p></dd><dt><span class="term"><code class="filename">/etc</code><a class="indexterm" name="id472482"></a>
   </span></dt><dd><p>
     Contains local configuration files that control the operation of
     programs like the X Window System. The <code class="filename">/etc/init.d</code>
     subdirectory contains scripts that are executed during the boot
     process.
    </p></dd><dt><span class="term"><code class="filename">/home/<em class="replaceable"><code>username</code></em></code><a class="indexterm" name="id472515"></a>
   </span></dt><dd><p>
     Holds the private data of every user who has an account on the system.
     The files located here can only be modified by their owner or by the
     system administrator. By default, your e-mail directory and personal
     desktop configuration are located here in the form of hidden files and
     directories. KDE users find the personal configuration data for their
     desktop in <code class="filename">.kde4</code> and GNOME users find it in
     <code class="filename">.gconf</code>. <span>For information about
     hidden files, refer to Section &#8220;Key Features&#8221; (Chapter 6, <i>Basic Concepts</i>, &#8593;Start-Up).</span>
    </p><div class="note"><table border="0" cellpadding="3" cellspacing="0" width="100%" summary="Note: Home Directory in a Network Environment"><tr class="head"><td width="32"><img alt="[Note]" src="admon/note.png"></td><th align="left">Home Directory in a Network Environment</th></tr><tr><td colspan="2" align="left" valign="top"><p>
      If you are working in a network environment, your home directory may
      be mapped to a directory in the file system other than
      <code class="filename">/home</code>.
     </p></td></tr></table></div></dd><dt><span class="term"><code class="filename">/lib</code><a class="indexterm" name="id472579"></a>
   </span></dt><dd><p>
     Contains the essential shared libraries needed to boot the system and
     to run the commands in the root file system. The Windows equivalent for
     shared libraries are DLL files.
    </p></dd><dt><span class="term"><code class="filename">/media</code><a class="indexterm" name="id472606"></a>
   </span></dt><dd><p>
     Contains mount points for removable media, such as CD-ROMs, USB sticks
     and digital cameras (if they use USB). <code class="filename">/media</code>
     generally holds any type of drive except the hard drive of your system.
     As soon as your removable medium has been inserted or connected to the
     system and has been mounted, you can access it from here.
     
    </p></dd><dt><span class="term"><code class="filename">/mnt</code><a class="indexterm" name="id472639"></a>
   </span></dt><dd><p>
     This directory provides a mount point for a temporarily mounted file
     system. <code class="systemitem">root</code> may mount file systems here.
    </p></dd><dt><span class="term"><code class="filename">/opt</code><a class="indexterm" name="id472671"></a>
   </span></dt><dd><p>
     Reserved for the installation of third-party software. Optional
     software and larger add-on program packages can be found here.
    </p></dd><dt><span class="term"><code class="filename">/root</code><a class="indexterm" name="id472698"></a>
   </span></dt><dd><p>
     Home directory for the <code class="systemitem">root</code> user. The personal data of <code class="systemitem">root</code>
     is located here.
    </p></dd><dt><span class="term"><code class="filename">/sbin</code><a class="indexterm" name="id472737"></a>
   </span></dt><dd><p>
     As the <code class="literal">s</code> indicates, this directory holds utilities
     for the superuser. <code class="filename">/sbin</code> contains the binaries
     essential for booting, restoring and recovering the system in addition
     to the binaries in <code class="filename">/bin</code>.
    </p></dd><dt><span class="term"><code class="filename">/srv</code><a class="indexterm" name="id472775"></a>
   </span></dt><dd><p>
     Holds data for services provided by the system, such as FTP and HTTP.
    </p></dd><dt><span class="term"><code class="filename">/tmp</code><a class="indexterm" name="id472801"></a>
   </span></dt><dd><p>
     This directory is used by programs that require temporary storage of
     files.
    </p><div class="important"><table border="0" cellpadding="3" cellspacing="0" width="100%" summary="Important: Cleaning up /tmp at Boot Time"><tr class="head"><td width="32"><img alt="[Important]" src="admon/important.png"></td><th align="left">Cleaning up <code class="filename">/tmp</code> at Boot Time</th></tr><tr><td colspan="2" align="left" valign="top"><p>
      Data stored in <code class="filename">/tmp</code> are not guaranteed to survive
      a system reboot. It depends, for example, on settings in
      <code class="filename">/etc/sysconfig/cron</code>.
     </p></td></tr></table></div></dd><dt><span class="term"><code class="filename">/usr</code><a class="indexterm" name="id472851"></a>
   </span></dt><dd><p>
     <code class="literal">/usr</code> has nothing to do with users, but is the
     acronym for UNIX system resources. The data in
     <code class="filename">/usr</code> is static, read-only data that can be shared
     among various hosts compliant with the Filesystem Hierarchy Standard
     (FHS). This directory contains all application programs and establishes
     a secondary hierarchy in the file system. KDE4 and GNOME are also
     located here. <code class="filename">/usr</code> holds a number of
     subdirectories, such as <code class="filename">/usr/bin</code>,
     <code class="filename">/usr/sbin</code>, <code class="filename">/usr/local</code>, and
     <code class="filename">/usr/share/doc</code>.
    </p></dd><dt><span class="term"><code class="filename">/usr/bin</code>
   </span></dt><dd><p>
     Contains generally accessible programs.
    </p></dd><dt><span class="term"><code class="filename">/usr/sbin</code>
   </span></dt><dd><p>
     Contains system daemons and programs reserved for the system
     administrator that are not essential for booting the system.
    </p></dd><dt><span class="term"><code class="filename">/usr/local</code>
   </span></dt><dd><p>
     In this directory the system administrator can install local,
     distribution-independent extensions.
    </p></dd><dt><span class="term"><code class="filename">/usr/share/doc</code>
   </span></dt><dd><p>
     Holds various documentation files and the release notes for your
     system. In the <code class="filename">manual</code> subdirectory find an online
     version of this manual. If more than one language is installed, this
     directory may contain versions of the manuals for different languages.
    </p><p>
     Under <code class="filename">packages</code> find the documentation included in
     the software packages installed on your system. For every package, a
     subdirectory
     <code class="filename">/usr/share/doc/packages/<em class="replaceable"><code>packagename</code></em></code>
     is created that often holds README files for the package and sometimes
     examples, configuration files or additional scripts.
    </p><p>
     If HOWTOs are installed on your system
     <code class="filename">/usr/share/doc</code> also holds the
     <code class="filename">howto</code> subdirectory in which to find additional
     documentation on many tasks related to the setup and operation of Linux
     software.
    </p></dd><dt><span class="term"><code class="filename">/var</code><a class="indexterm" name="id473012"></a>
   </span></dt><dd><p>
     Whereas <code class="filename">/usr</code> holds static, read-only data,
     <code class="filename">/var</code> is for data which is written during system
     operation and thus is variable data, such as log files or spooling
     data. For an overview of the most important log files you can find
     under <code class="filename">/var/log/</code>, refer to
     Table &#8220;Log Files&#8221; (&#8593;Start-Up).
    </p></dd><dt><span class="term"><code class="filename">/windows</code><a class="indexterm" name="id473057"></a>
   </span></dt><dd><p>
     Only available if you have both Microsoft Windows and Linux installed
     on your system. Contains the Windows data available on the Windows
     partition of your system. Whether you can edit the data in this
     directory depends on the file system your Windows partition uses. If it
     is FAT32, you can open and edit the files in this directory. For NTFS,
     openSUSE also includes write access support. However, the driver
     for the NTFS-3g file system has limited functionality.
     <span>Learn more in
     <a class="xref" href="cha.filetrans.html#sec.filetrans.winlincomp" title="34.4. Accessing Files on Different OS on the Same Computer">Section 34.4, &#8220;Accessing Files on Different OS on the Same Computer&#8221;</a></span>.
    </p></dd></dl></div></div></div><div class="sect1" title="20.2. Writing Shell Scripts"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="sec.adm.shellscripts"></a>20.2. Writing Shell Scripts<span class="permalink"><a alt="Permalink" title="Copy Permalink" href="#sec.adm.shellscripts">¶</a></span></h2></div></div></div><a class="indexterm" name="id473107"></a><p>
   Shell scripts are a convenient way of doing all sorts of tasks:
   collecting data, searching for a word or phrase in a text and many other
   useful things. The following example shows a small shell script that
   prints a text:
  </p><div class="example"><a name="id473118"></a><p class="title"><b>Example 20.1. A Shell Script Printing a Text</b></p><div class="example-contents"><pre class="screen">#!/bin/sh <a name="co.adm.shell.shebang"></a><img src="callouts/1.png" alt="1" border="0">
# Output the following line: <a name="co.adm.shell.comment"></a><img src="callouts/2.png" alt="2" border="0">
echo "Hello World" <a name="co.adm.shell.echo"></a><img src="callouts/3.png" alt="3" border="0"></pre><div class="calloutlist"><table border="0" summary="Callout list"><tr><td width="5%" valign="top" align="left"><p><a href="#co.adm.shell.shebang"><img src="callouts/1.png" alt="1" border="0"></a> </p></td><td valign="top" align="left"><p>
      The first line begins with the <span class="emphasis"><em>Shebang</em></span>
      <a class="indexterm" name="id473158"></a> characters (<code class="literal">#!</code>) which is an indicator
      that this file is a script. The script is executed with the specified
      interpreter after the Shebang, in this case
      <span class="command"><strong>/bin/sh</strong></span>.
     </p></td></tr><tr><td width="5%" valign="top" align="left"><p><a href="#co.adm.shell.comment"><img src="callouts/2.png" alt="2" border="0"></a> </p></td><td valign="top" align="left"><p>
      The second line is a comment beginning with the hash sign. It is
      recommended to comment difficult lines to remember what they do.
     </p></td></tr><tr><td width="5%" valign="top" align="left"><p><a href="#co.adm.shell.echo"><img src="callouts/3.png" alt="3" border="0"></a> </p></td><td valign="top" align="left"><p>
      The third line uses the built-in command <span class="command"><strong>echo</strong></span> to
      print the corresponding text.
     </p></td></tr></table></div></div></div><br class="example-break"><p>
   Before you can run this script you need some prerequisites:
  </p><div class="orderedlist"><ol class="orderedlist" type="1"><li><p>
     Every script should contain a Shebang line (this is already the case
     with our example above). If a script does not have this line, you have
     to call the interpreter manually.
    </p></li><li><p>
     Save the script to a directory where the shell can find it. The search
     path in a shell is determined by the environment variable
     <code class="envar">PATH</code>. Usually a normal user does not have write access
     to <code class="filename">/usr/bin</code>. Therefore, it is recommended to save
     your scripts in the users' directory <code class="filename">~/bin/</code>. The
     above example gets the name <code class="filename">hello.sh</code>.
    </p></li><li><p>
     The script needs executable permissions. Set the permissions with the
     following command:
    </p><pre class="screen">chmod +x ~/bin/hello.sh</pre></li></ol></div><p>
   If you have fulfilled all of the above prerequisites, you can execute the
   script in the following ways:
  </p><div class="orderedlist"><ol class="orderedlist" type="1"><li><p title="As Absolute Path"><b>As Absolute Path. </b>
      The script can be executed with an absolute path. In our case, it is
      <span class="command"><strong>~/bin/hello.sh</strong></span>.
     </p></li><li><p title="Everywhere"><b>Everywhere. </b>
      If the <code class="envar">PATH</code> environment variable contains the directory
      where the script is located, you can execute the script just with
      <span class="command"><strong>hello.sh</strong></span>.
     </p></li></ol></div></div><div class="sect1" title="20.3. Redirecting Command Events"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="sec.adm.shell.redirect"></a>20.3. Redirecting Command Events<span class="permalink"><a alt="Permalink" title="Copy Permalink" href="#sec.adm.shell.redirect">¶</a></span></h2></div></div></div><p>
   Each command can use three channels, either for input or output:
  </p><div class="itemizedlist"><ul class="itemizedlist" type="bullet"><li class="listitem" style="list-style-type: disc"><p title="Standard Output"><b>Standard Output. </b>
      This is the default output channel. Whenever a command prints
      something, it uses the standard output channel.
     </p></li><li class="listitem" style="list-style-type: disc"><p title="Standard Input"><b>Standard Input. </b>
      If a command needs input from users or other commands, it uses this
      channel.
     </p></li><li class="listitem" style="list-style-type: disc"><p title="Standard Error"><b>Standard Error. </b>
      Commands use this channel for error reporting.
     </p></li></ul></div><p>
   To redirect these channels, there are the following possibilities:
  </p><div class="variablelist"><dl><dt><span class="term"><code class="literal">Command &gt; File</code>
    </span></dt><dd><p>
      Saves the output of the command into a file, an existing file will be
      deleted. For example, the <span class="command"><strong>ls</strong></span> command writes its
      output into the file <code class="filename">listing.txt</code>:
     </p><pre class="screen">ls &gt; listing.txt</pre></dd><dt><span class="term"><code class="literal">Command &gt;&gt; File</code>
    </span></dt><dd><p>
      Appends the output of the command to a file. For example, the
      <span class="command"><strong>ls</strong></span> command appends its output to the file
      <code class="filename">listing.txt</code>:
     </p><pre class="screen">ls &gt;&gt; listing.txt</pre></dd><dt><span class="term"><code class="literal">Command &lt; File</code>
    </span></dt><dd><p>
      Reads the file as input for the given command. For example, the
      <span class="command"><strong>read</strong></span> command reads in the content of the file to
      the variable <code class="literal">a</code>:
     </p><pre class="screen">read a &lt; foo</pre></dd><dt><span class="term"><code class="literal">Command1 | Command2</code>
    </span></dt><dd><p>
      Redirects the output of the left command as input for the right
      command. For example, the <span class="command"><strong>cat</strong></span> command outputs the
      content of the <code class="filename">/proc/cpuinfo</code> file. This output is
      used by <span class="command"><strong>grep</strong></span> to filter only those lines which
      contain <code class="literal">cpu</code>:
     </p><pre class="screen">cat /proc/cpuinfo | grep cpu</pre></dd></dl></div><p>
   Every channel has a <span class="emphasis"><em>file descriptor</em></span>: 0 (zero) for
   standard input, 1 for standard output and 2 for standard error. It is
   allowed to insert this file descriptor before a <code class="literal">&lt;</code>
   or <code class="literal">&gt;</code> character. For example, the following line
   searches for a file starting with <code class="filename">foo</code>, but
   suppresses its errors by redirecting it to
   <code class="filename">/dev/null</code>, the garbage bin:
  </p><pre class="screen">find / -name "foo*" 2&gt;/dev/null</pre></div><div class="sect1" title="20.4. Using Aliases"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="sec.adm.alias"></a>20.4. Using Aliases<span class="permalink"><a alt="Permalink" title="Copy Permalink" href="#sec.adm.alias">¶</a></span></h2></div></div></div><a class="indexterm" name="id473555"></a><p>
   An alias is a shortcut definition of one or more commands. This is useful
   if you have commands which are hard to remember or with lots of options.
   The syntax for an alias is:
  </p><pre class="screen">alias <em class="replaceable"><code>NAME</code></em>=<em class="replaceable"><code>DEFINITION</code></em></pre><p>
   For example, the following line defines an alias <span class="command"><strong>lt</strong></span>
   which outputs a long listing (option <code class="option">-l</code>), sorts it by
   modification time (<code class="option">-t</code>) and prints it in reverse order
   while sorting (<code class="option">-r</code>):
  </p><pre class="screen">alias lt='ls -ltr'</pre><p>
   To view all alias definitions, use <span class="command"><strong>alias</strong></span>. Remove your
   alias with <span class="command"><strong>unalias</strong></span>.
  </p></div><div class="sect1" title="20.5. Using Variables in Bash"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="sec.adm.variables"></a>20.5. Using Variables in Bash<span class="permalink"><a alt="Permalink" title="Copy Permalink" href="#sec.adm.variables">¶</a></span></h2></div></div></div><p>
   A shell variable can be global or local. Global variables, or environment
   variables, can be accessed in all shells. In contrast, local variables
   are visible in the current shell only.
  </p><p>
   To view all environment variables, use the <span class="command"><strong>printenv</strong></span>
   command. To see the value of a variable, insert the name of your variable
   as an argument:
  </p><pre class="screen">printenv PATH</pre><p>
   A variable, be it global or local, can also be viewed with
   <span class="command"><strong>echo</strong></span>:
  </p><pre class="screen">echo $PATH</pre><p>
   To set a local variable, use a variable name followed by the equal sign,
   followed by the value:
  </p><pre class="screen">PROJECT="SLED"</pre><p>
   Do not insert spaces around the equal sign, otherwise you get an error.
   To set an environment variable, use <span class="command"><strong>export</strong></span>:
  </p><pre class="screen">export NAME="tux"</pre><p>
   To remove a variable, use <span class="command"><strong>unset</strong></span>:
  </p><pre class="screen">unset NAME</pre><p>
   The following table contains some common environment variables which can
   be used in you shell scripts:
  </p><div class="table"><a name="tab.adm.envars"></a><p class="title"><b>Table 20.5. Useful Environment Variables</b><span class="permalink"><a alt="Permalink" title="Copy Permalink" href="#tab.adm.envars">¶</a></span></p><div class="table-contents"><table summary="Useful Environment Variables" border="1"><colgroup><col><col></colgroup><tbody><tr><td>
       <p>
        <code class="envar">HOME</code>
       </p>
      </td><td>
       <p>
        the home directory of the current user
       </p>
      </td></tr><tr><td>
       <p>
        <code class="envar">HOST</code>
       </p>
      </td><td>
       <p>
        the current hostname
       </p>
      </td></tr><tr><td>
       <p>
        <code class="envar">LANG</code>
       </p>
      </td><td>
       <p>
        when a tool is localized, it uses the language from this environment
        variable. English can also be set to <code class="literal">C</code>
       </p>
      </td></tr><tr><td>
       <p>
        <code class="envar">PATH</code>
       </p>
      </td><td>
       <p>
        the search path of the shell, a list of directories separated by
        colon
       </p>
      </td></tr><tr><td>
       <p>
        <code class="envar">PS1</code>
       </p>
      </td><td>
       <p>
        specifies the normal prompt printed before each command
       </p>
      </td></tr><tr><td>
       <p>
        <code class="envar">PS2</code>
       </p>
      </td><td>
       <p>
        specifies the secondary prompt printed when you execute a multi-line
        command
       </p>
      </td></tr><tr><td>
       <p>
        <code class="envar">PWD</code>
       </p>
      </td><td>
       <p>
        current working directory
       </p>
      </td></tr><tr><td>
       <p>
        <code class="envar">USER</code>
       </p>
      </td><td>
       <p>
        the current user
       </p>
      </td></tr></tbody></table></div></div><br class="table-break"><div class="sect2" title="20.5.1. Using Argument Variables"><div class="titlepage"><div><div><h3 class="title"><a name="id473895"></a>20.5.1. Using Argument Variables<span class="permalink"><a alt="Permalink" title="Copy Permalink" href="#id473895">¶</a></span></h3></div></div></div><p>
    For example, if you have the script <span class="command"><strong>foo.sh</strong></span> you can
    execute it like this:
   </p><pre class="screen">foo.sh "Tux Penguin" 2000 </pre><p>
    To access all the arguments which are passed to your script, you need
    positional parameters. These is <code class="envar">$1</code> for the first
    argument, <code class="envar">$2</code> for the second, and so on. You can have up
    to nine parameters. To get the script name, use <code class="envar">$0</code>.
   </p><p>
    The following script <span class="command"><strong>foo.sh</strong></span> prints all arguments from
    1 to 4:
   </p><pre class="screen">#!/bin/sh
echo \"$1\" \"$2\" \"$3\" \"$4\"</pre><p>
    If you execute this script with the above arguments, you get:
   </p><pre class="screen">"Tux Penguin" "2000" "" ""</pre></div><div class="sect2" title="20.5.2. Using Variable Substitution"><div class="titlepage"><div><div><h3 class="title"><a name="sec.adm.shell.varsubst"></a>20.5.2. Using Variable Substitution<span class="permalink"><a alt="Permalink" title="Copy Permalink" href="#sec.adm.shell.varsubst">¶</a></span></h3></div></div></div><p>
    Variable substitutions apply a pattern to the content of a variable
    either from the left or right side. The following list contains the
    possible syntax forms:
   </p><div class="variablelist"><dl><dt><span class="term"><code class="literal">${VAR#pattern}</code>
     </span></dt><dd><p>
       removes the shortest possible match from the left:
      </p><pre class="screen">file=/home/tux/book/book.tar.bz2
echo ${file#*/}
home/tux/book/book.tar.bz2</pre></dd><dt><span class="term"><code class="literal">${VAR##pattern}</code>
     </span></dt><dd><p>
       removes the longest possible match from the left:
      </p><pre class="screen">file=/home/tux/book/book.tar.bz2
echo ${file##*/}
book.tar.bz2</pre></dd><dt><span class="term"><code class="literal">${VAR%pattern}</code>
     </span></dt><dd><p>
       removes the shortest possible match from the right:
      </p><pre class="screen">file=/home/tux/book/book.tar.bz2
echo ${file%.*}
/home/tux/book/book.tar</pre></dd><dt><span class="term"><code class="literal">${VAR%%pattern}</code>
     </span></dt><dd><p>
       removes the longest possible match from the right:
      </p><pre class="screen">file=/home/tux/book/book.tar.bz2
echo ${file%%.*}
/home/tux/book/book</pre></dd><dt><span class="term"><code class="literal">${VAR/pattern_1/pattern_2}</code>
     </span></dt><dd><p>
       substitutes the content of <em class="replaceable"><code>VAR</code></em> from the
       <em class="replaceable"><code>pattern_1</code></em> with
       <em class="replaceable"><code>pattern_2</code></em>:
      </p><pre class="screen">file=/home/tux/book/book.tar.bz2
echo ${file/tux/wilber}
/home/wilber/book/book.tar.bz2</pre></dd></dl></div></div></div><div class="sect1" title="20.6. Grouping And Combining Commands"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="sec.adm.shell.grouping"></a>20.6. Grouping And Combining Commands<span class="permalink"><a alt="Permalink" title="Copy Permalink" href="#sec.adm.shell.grouping">¶</a></span></h2></div></div></div><p>
   Shells allow you to concatenate and group commands for conditional
   execution. Each command returns an exit code which determines the success
   or failure of its operation. If it is 0 (zero), the command was
   successful. Everything else marks an error which is specific to the
   command.
  </p><p>
   The following list shows, how commands can be grouped:
  </p><div class="variablelist"><dl><dt><span class="term"><code class="literal">Command1 ; Command2</code>
    </span></dt><dd><p>
      executes the commands in sequential order. The exit code is not
      checked. The following line displays the content of the file with
      <span class="command"><strong>cat</strong></span> and then prints its file properties with
      <span class="command"><strong>ls</strong></span> regardless of their exit codes:
     </p><pre class="screen">cat filelist.txt ; ls -l filelist.txt</pre></dd><dt><span class="term"><code class="literal">Command1 &amp;&amp; Command2</code>
    </span></dt><dd><p>
      runs the right command, if the left command was successful (logical
      AND). The following line displays the content of the file and prints
      its file properties only, when the previous command was successful
      (compare it with the previous entry in this list):
     </p><pre class="screen">cat filelist.txt &amp;&amp; ls -l filelist.txt</pre></dd><dt><span class="term"><code class="literal">Command1 || Command2</code>
    </span></dt><dd><p>
      runs the right command, when the left command has failed (logical OR).
      The following line creates only a directory in
      <code class="filename">/home/wilber/bar</code> when the creation of the
      directory in <code class="filename">/home/tux/foo</code> has failed:
     </p><pre class="screen">mkdir /home/tux/foo || mkdir /home/wilber/bar</pre></dd><dt><span class="term"><code class="literal">funcname(){ ... }</code>
    </span></dt><dd><p>
      creates a shell function. You can use the positional parameters to
      access its arguments. The following line defines the function
      <code class="literal">hello</code> to print a short message:
     </p><pre class="screen">hello() { echo "Hello $1"; }</pre><p>
      Call this function like this:
     </p><pre class="screen">hello Tux</pre><p>
      which prints:
     </p><pre class="screen">Hello Tux</pre></dd></dl></div></div><div class="sect1" title="20.7. Working with Common Flow Constructs"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="sec.adm.shell.commonconstructs"></a>20.7. Working with Common Flow Constructs<span class="permalink"><a alt="Permalink" title="Copy Permalink" href="#sec.adm.shell.commonconstructs">¶</a></span></h2></div></div></div><p>
   To control the flow of your script, a shell has <span class="command"><strong>while</strong></span>,
   <span class="command"><strong>if</strong></span>, <span class="command"><strong>for</strong></span> and <span class="command"><strong>case</strong></span>
   constructs.
  </p><div class="sect2" title="20.7.1. The if Control Command"><div class="titlepage"><div><div><h3 class="title"><a name="sec.adm.shell.if"></a>20.7.1. The <span class="command"><strong>if</strong></span> Control Command<span class="permalink"><a alt="Permalink" title="Copy Permalink" href="#sec.adm.shell.if">¶</a></span></h3></div></div></div><p>
    The <span class="command"><strong>if</strong></span> command is used to check expressions. For
    example, the following code tests whether the current user is Tux:
   </p><pre class="screen">if test $USER = "tux" ; then
  echo "Hello Tux."
else
  echo "You are not Tux."
fi</pre><p>
    The test expression can be as complex or simple as possible. The
    following expression checks if the file <code class="filename">foo.txt</code>
    exists:
   </p><pre class="screen">if test -e /tmp/foo.txt ; then
  echo "Found foo.txt"
fi</pre><p>
    The test expression can also be abbreviated in angled brackets:
   </p><pre class="screen">if [ -e /tmp/foo.txt ] ; then
  echo "Found foo.txt"
fi</pre><p>
    Find more useful expressions at
    <a class="ulink" href="http://bash.cyberciti.biz/guide/If..else..fi" target="_top">http://bash.cyberciti.biz/guide/If..else..fi</a>.
   </p></div><div class="sect2" title="20.7.2. Creating Loops With the For Command"><div class="titlepage"><div><div><h3 class="title"><a name="sec.adm.shell.for"></a>20.7.2. Creating Loops With the For Command<span class="permalink"><a alt="Permalink" title="Copy Permalink" href="#sec.adm.shell.for">¶</a></span></h3></div></div></div><p>
    The <span class="command"><strong>for</strong></span> loop allows you to execute commands to a list
    of entries. For example, the following code prints some information
    about PNG files in the current directory:
   </p><pre class="screen">for i in *.png;
 do
  ls -l $i
 done</pre></div></div><div class="sect1" title="20.8. For More Information"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="sec.adm.shell.moreinfo"></a>20.8. For More Information<span class="permalink"><a alt="Permalink" title="Copy Permalink" href="#sec.adm.shell.moreinfo">¶</a></span></h2></div></div></div><p>
   Important information about Bash is provided in the man pages
   <span class="command"><strong>man bash</strong></span>. More about this topic can be found in the
   following list:
  </p><div class="itemizedlist"><ul class="itemizedlist" type="bullet"><li class="listitem" style="list-style-type: disc"><p>
     <a class="ulink" href="http://tldp.org/LDP/Bash-Beginners-Guide/html/index.html" target="_top">http://tldp.org/LDP/Bash-Beginners-Guide/html/index.html</a>&#8212;Bash
     Guide for Beginners
    </p></li><li class="listitem" style="list-style-type: disc"><p>
     <a class="ulink" href="http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html" target="_top">http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html</a>&#8212;BASH
     Programming - Introduction HOW-TO
    </p></li><li class="listitem" style="list-style-type: disc"><p>
     <a class="ulink" href="http://tldp.org/LDP/abs/html/index.html" target="_top">http://tldp.org/LDP/abs/html/index.html</a>&#8212;Advanced
     Bash-Scripting Guide
    </p></li><li class="listitem" style="list-style-type: disc"><p>
     <a class="ulink" href="http://www.grymoire.com/Unix/Sh.html" target="_top">http://www.grymoire.com/Unix/Sh.html</a>&#8212;Sh - the
     Bourne Shell
    </p></li></ul></div></div></div><div class="navfooter"><table width="100%" summary="Navigation footer" border="0" class="bctable"><tr><td width="80%"><div class="breadcrumbs"><p><a href="index.html"> Documentation</a><span class="breadcrumbs-sep"> &gt; </span><a href="book.opensuse.reference.html">Reference</a><span class="breadcrumbs-sep"> &gt; </span><a href="part.reference.system.html">System</a><span class="breadcrumbs-sep"> &gt; </span><strong><a accesskey="p" title="Chapter 19. Dynamic Kernel Device Management with udev" href="cha.udev.html"><span>&#9664;</span></a> </strong></p></div></td></tr></table></div></body></html>

ACC SHELL 2018