Skip to content
Snippets Groups Projects
bashguide.html 65.5 KiB
Newer Older
Alexander Schoch's avatar
Alexander Schoch committed
  <code>select</code> construct lets the user choose which value is used next.
  This also means that a <code>select</code> construct can run indefinitely,
  because the user can keep selecting new choices. To avoid being trapped in it,
  we have to explicitly use <code>break</code>. <code>break</code> is a builtin
  command that makes bash jump out of the current <code>do</code> block.
  Execution will continue after the <code>done</code>. <code>break</code> also
  works in <code>for</code> and <code>while</code> loops.
</p>
<p>
  As you can see in the example above, we used an <code>if</code> command inside
  a <code>select</code> command. All of these conditional constructs
  (<code>if</code>, <code>for</code>, <code>while</code>, <code>case</code> and
  <code>select</code>) can be nested indefinitely.
</p>
<h1 data-number="7" id="input-and-output">
  <span class="header-section-number">7</span> Input and Output
</h1>
<p>
  Input and output in bash is very flexible and, consequentially, complex. We
  will only look at the most widely used components.
</p>
<h2 data-number="7.1" id="command-line-arguments">
  <span class="header-section-number">7.1</span> Command-line Arguments
</h2>
<p>
  For many bash scripts, the first input we care about are the arguments given
  to it via the command line. As we saw in the chapter on Parameters, these
  arguments are contained in some <em>special parameters</em>. These are called
  <em>positional parameters</em>. The first parameter is referred to with
  <code>$1</code>, the second with <code>$2</code>, and so on. After number 9,
  you have to enclose the numbers in curly braces: <code>${10}</code>,
  <code>${11}</code> and so on.
</p>
<p>
  In addition to referring to them one at a time, you may also refer to the
  entire set of positional parameters with the <code>"$@"</code> substitution.
  The double quotes here are <strong>extremely important</strong>. If you don’t
  use the double quotes, each one of the positional parameters will undergo word
  splitting and globbing. You don’t want that. By using the quotes, you tell
  Bash that you want to preserve each parameter as a separate word.
</p>
<p>
  There are even more ways to deal with parameters. For example, it is very
  common for commands to accept <em>options</em>, which are single letters
  starting with a <code>-</code>. For example, <code>ls -l</code> calls the
  <code>ls</code> program with the <code>-l</code> option, which makes it output
  more information. Usually, multiple options can be combined, as in
  <code>ls -la</code>, which is equivalent to <code>ls -l -a</code>.
</p>
<p>
  You might want to create your own scripts that accept some options. Bash has
  the so called <code>getopts</code> builtin command to parse passed options.
</p>
Alexander Schoch's avatar
Alexander Schoch committed
<pre><code>while getopts &#39;hlf:&#39; opt
do 
    case &quot;$opt&quot; in
    h|\?)
        echo &#39;available options: -h -l -f [filename]&#39;
        ;;
    f)
        file=&quot;$OPTARG&quot;
        ;;
    l)
        list=true
        ;;
    esac
done

shift &quot;$(( OPTIND - 1 ))&quot;</code></pre>
Alexander Schoch's avatar
Alexander Schoch committed
<p>
  As you can see, we use <code>getopts</code> within a while loop.
  <code>getopts</code> will return 0 as long as there are more options remaining
  and something else if there are no more options. That makes it perfectly
  suitable for a loop.
</p>
<p>
  <code>getopts</code> takes two arguments, the <em>optstring</em> and the
  <em>variable name</em>. The optstring contains all the letters that are valid
  options. In our example, these are <code>h</code>, <code>l</code> and
  <code>f</code>.
</p>
<p>
  The <code>f</code> is followed by a colon. This indicates that the f option
  requires an argument. The script could for example be called like so:
</p>
Alexander Schoch's avatar
Alexander Schoch committed
<pre><code>myscript -f file.txt</code></pre>
Alexander Schoch's avatar
Alexander Schoch committed
<p>
  <code>getopts</code> will set the variable that you specified as its second
  argument to the letter of the option it found first. If the option required an
  argument, the variable <code>OPTARG</code> is set to whatever the argument
  was. In the example above, a case statement is used to react to the different
  options.
</p>
<p>
  There’s also a special option <code>?</code>. Whenever
  <code>getopts</code> finds an option that is not present in the optstring, it
  sets the shell variable (<code>opt</code> in the example) to <code>?</code>.
  In the case statement above, that triggers the help message.
</p>
<p>
  After all options are parsed, the remaining arguments are “moved” such that
  they are now in <code>$1</code>, <code>$2</code>… even though previously,
  these positional parameters were occupied by the options.
</p>
<p>
  Also note the line <code>shift "$(( OPTIND - 1 ))"</code> at the end. The
  <code>shift</code> builtin can be used to discard command-line arguments. Its
  argument is a number and designates how many arguments we want to discard.
</p>
<p>
  This is needed because we don’t know how many options the user will pass to
  our script. If there are more positional parameters after all the options, we
  have no way of knowing at which number they start. Fortunately,
  <code>getopts</code> also sets the shell variable <code>OPTIND</code>, in
  which it stores the index of the option it’s going to parse next.
</p>
<p>
  So after parsing all the option, we just discard the first
  <code>OPTIND - 1</code> options, and the remaining arguments now start from
  <code>$1</code> onwards.
</p>
<h2 data-number="7.2" id="file-descriptors">
  <span class="header-section-number">7.2</span> File Descriptors
</h2>
<p>
  <em>File descriptors</em> are the way programs refer to files, or other things
  that work like files (such as pipes, devices, or terminals). You can think of
  them as pointers that point to data locations. Through these pointers,
  programs can write to or read from these locations.
</p>
Alexander Schoch's avatar
Alexander Schoch committed
<p>By default, every program has three file descriptors:</p>
<ul>
Alexander Schoch's avatar
Alexander Schoch committed
  <li>Standard Input (stdin): File Descriptor 0</li>
  <li>Standard Output (stdout): File Descriptor 1</li>
  <li>Standard Error (stderr): File Descriptor 2</li>
Alexander Schoch's avatar
Alexander Schoch committed
</ul>
Alexander Schoch's avatar
Alexander Schoch committed
<p>
  When you run a script in the terminal, then stdin contains everything you type
  in that terminal. stdout and stderr both point to the terminal, and everything
  that is written to these two is displayed as text in the terminal. stdout is
  where programs send their normal information, and stderr is where they send
  their error messages.
</p>
<p>
  Let’s make these definitions a little more concrete. Consider this example:
</p>
Alexander Schoch's avatar
Alexander Schoch committed
<pre><code>echo &#39;What is your name?&#39;
read name
echo &quot;Good day, $name. Would you like some tea?&quot;</code></pre>
Alexander Schoch's avatar
Alexander Schoch committed
<p>
  You already know <code>echo</code>. It simply prints its argument to
  <em>stdout</em>. Since stdout is connected to your terminal, you will see that
  message there.
</p>
<p>
  <code>read</code> is a command that reads one line of text from
  <em>stdin</em> and stores it in a variable, which we specified to be
  <code>name</code>. Because stdin is connected to what you type in your
  terminal, it will let you type a line of text, and as soon as you press enter,
  that line will be stored in the variable.
</p>
Alexander Schoch's avatar
Alexander Schoch committed
<p>So what about stderr? Consider this example:</p>
<pre><code>ehco &#39;Hello!&#39;</code></pre>
Alexander Schoch's avatar
Alexander Schoch committed
<p>
  The command <code>ehco</code> does not exist. If you run this, bash will print
  an error message to <em>stderr</em>. Because stderr is connected to your
  terminal, you will see that message there.
</p>
<h2 data-number="7.3" id="redirection">
  <span class="header-section-number">7.3</span> Redirection
</h2>
<p>
  <em>Redirection</em> is the most basic form of input/output manipulation in
  bash. It is used to change the source or destination of
  <em>File descriptors</em>, i.e. connect them to something other than your
  terminal. For example, you can send a command’s output to a file instead.
</p>
Alexander Schoch's avatar
Alexander Schoch committed
<pre><code>echo &#39;It was a dark and stormy night. Too dark to write.&#39; &gt; story</code></pre>
Alexander Schoch's avatar
Alexander Schoch committed
<p>
  The <code>&gt;</code> operator begins an <em>output redirection</em>. It
  redirects the <em>stdout</em> file descriptor of the command to the left, and
  connects it to a file called “story”. That means if you run this, you will not
  see the output of <code>echo</code>—after all, stdout no longer points to your
  terminal.
</p>
<p>
  Note that <code>&gt;</code> will just open the file you specify without
  checking whether it already exists first. If the file already exists, its
  contents will be overwritten and you will lose whatever was stored in there
  before. <strong>Be careful.</strong>
</p>
<p>
  If you don’t want to overwrite the existing content of a file, but rather
  append your output to the end of that file, you can use
  <code>&gt;&gt;</code> instead of <code>&gt;</code>.
</p>
<p>
  Now, let’s look at <em>input redirection</em>. For that, we first introduce a
  command named <code>cat</code>. <code>cat</code> is often used to display the
  contents of a file, like so:
</p>
Alexander Schoch's avatar
Alexander Schoch committed
<pre><code>cat myfile</code></pre>
Alexander Schoch's avatar
Alexander Schoch committed
<p>
  If <code>cat</code> is called without an argument, however, it will simply
  read from <em>stdin</em> and print that directly to <em>stdout</em>.
</p>
<p>
  Try the following: Run <code>cat</code>, without arguments, in your terminal.
  Then type some characters and hit enter. Can you figure out what is happening?
</p>
<p>
  <em>Input redirection</em> uses the <code>&lt;</code> operator. It works as
  follows:
</p>
Alexander Schoch's avatar
Alexander Schoch committed
<pre><code>cat &lt; story</code></pre>
Alexander Schoch's avatar
Alexander Schoch committed
<p>
  The <code>&lt;</code> operator will take a command’s <em>stdin</em> file
  descriptor and point it to a file, “story” in this example. This means
  <code>cat</code> now ignores your terminal and reads from “story” instead.
  Note that this has the same effect as typing <code>cat story</code>.
</p>
<p>
  If you want to redirect <em>stderr</em> instead of <em>stdout</em>, you can do
  as follows:
</p>
Alexander Schoch's avatar
Alexander Schoch committed
<pre><code>ehco &#39;Hello!&#39; 2&gt; errors</code></pre>
Alexander Schoch's avatar
Alexander Schoch committed
<p>
  If you run this, you won’t see any error message, even though the command
  <code>ehco</code> doesn’t exist. That’s because <em>stderr</em> is no longer
  connected to your terminal, but to a file called “errors” instead.
</p>
<p>
  Now that you know about redirection, there is one subtlety that you have to be
  aware of: You can’t have two file descriptors point to the same file.
</p>
<p>
  If you wanted to log a command’s complete output–stdout
  <em>and</em> stderr–you might be tempted to do something like this:
</p>
Alexander Schoch's avatar
Alexander Schoch committed
<pre><code>mycommand &gt; logfile 2&gt; logfile</code></pre>
Alexander Schoch's avatar
Alexander Schoch committed
<p>
  However, this is a <strong>bad</strong> idea. The two file descriptors will
  now both point to the same file <em>independently</em>, which causes them to
  constantly overwrite each other’s text.
</p>
<p>
  If you still want to point both stdout and stderr to the same file, you can do
  it like this:
</p>
Alexander Schoch's avatar
Alexander Schoch committed
<pre><code>mycommand &gt; logfile 2&gt;&amp;1</code></pre>
Alexander Schoch's avatar
Alexander Schoch committed
<p>
  Here, we use the <code>&gt;&amp;</code> syntax to duplicate file descriptor 1.
  In this scenario, we no longer have two file descriptors pointing to one file.
  Instead, we have only one file descriptor that acts as both stdout and stderr
  at the same time.
</p>
<p>
  To help remember the syntax, you can think of <code>&amp;1</code> as “where 1
  is”, and of the <code>2&gt;</code> as “point 2 to”. The whole thing,
  <code>2&gt;&amp;1</code>, then becomes “point 2 to wherever 1 is”. This also
  makes it clear that <code>&gt; logfile</code> has to come <em>before</em>
  <code>2&gt;&amp;1</code>: First you point 1 to “logfile”, and only then you
  can point 2 to where 1 is.
</p>
<p>
  There’s also a quick way to completely get rid of a command’s output using
  redirections. Consider this:
</p>
Alexander Schoch's avatar
Alexander Schoch committed
<pre><code>mycommand &gt; /dev/null 2&gt;&amp;1</code></pre>
Alexander Schoch's avatar
Alexander Schoch committed
<p>
  <code>/dev/null</code> is a special file in your file system that you can
  write to, but the things you write to it are not stored. So,
  <code>mycommand</code>’s output is written somewhere where it’s not stored or
  processed in any way. It is discarded completely.
</p>
<p>
  You could also leave out the <code>2&gt;&amp;1</code>. Then, you’d still see
  error messages, but discard the normal output.
</p>
<h2 data-number="7.4" id="pipes">
  <span class="header-section-number">7.4</span> Pipes
</h2>
<p>
  Now that you know how to manipulate <em>file descriptors</em> to direct output
  to files, it’s time to learn another type of I/O redirection.
</p>
<p>
  The <code>|</code> operator can be used to connect one command’s
  <em>stdout</em> to another command’s <em>stdin</em>. Have a look at this:
</p>
Alexander Schoch's avatar
Alexander Schoch committed
<pre><code>echo &#39;This is a beautiful day!&#39; | sed &#39;s/beauti/wonder&#39;</code></pre>
Alexander Schoch's avatar
Alexander Schoch committed
<p>
  The <code>sed</code> command (“sed” is short for “stream editor”) is a utility
  that can be used to manipulate text “on the fly”. It reads text from stdin,
  edits it according to some commands, and then prints the result to stdout. It
  is very powerful. Here, we use it to replace “beauti” with “wonder”.
</p>
<p>
  First, the <code>echo</code> command writes some text to it’s stdout. The
  <code>|</code> operator connected <code>echo</code>’s stout to
  <code>sed</code>’s stdin, so everything <code>echo</code> sends there is
  immediately picked up by <code>sed</code>. <code>sed</code> will then edit the
  text and print the result to its own stdout. <code>sed</code>’s stdout is
  still connected to your terminal, so this is what you see.
</p>
<h1 data-number="8" id="compound-commands">
  <span class="header-section-number">8</span> Compound Commands
</h1>
<p>
  <em>Compound commands</em> is a catch-all phrase covering several different
  concepts. We’ve already seen <code>if</code>, <code>for</code>,
  <code>while</code>, <code>case</code>, <code>select</code> and the
  <code>[[</code> keyword, which all fall into this category. Now we’ll look at
  a few more.
</p>
<h2 data-number="8.1" id="subshells">
  <span class="header-section-number">8.1</span> Subshells
</h2>
<p>
  <em>Subshells</em> can be used to encapsulate a command’s effect. If a command
  has undesired side effects, you can execute it in a subshell. Once the
  subshell command ends, all side effects will be gone.
</p>
<p>
  To execute a command (or several commands) in a subshell, enclose them in
  parenthesis:
</p>
Alexander Schoch's avatar
Alexander Schoch committed
<pre><code>( 
    cd /tmp 
    pwd
)
pwd</code></pre>
Alexander Schoch's avatar
Alexander Schoch committed
<p>
  The <code>cd</code> and the first <code>pwd</code> commands are executed in a
  subshell. All side effects in that subshell won’t affect the second
  <code>pwd</code> command. Changing the current directory is such a side
  effect–even though we use <code>cd</code> to go to the
  <code>/tmp</code> folder, we jump back to our original folder as soon as the
  subshell ends.
</p>
<h2 data-number="8.2" id="command-grouping">
  <span class="header-section-number">8.2</span> Command Grouping
</h2>
<p>
  You can group several commands together by enclosing them in curly braces.
  This makes bash consider them as a unit with regard to pipes, redirections and
  control flow:
</p>
Alexander Schoch's avatar
Alexander Schoch committed
<pre><code>{
    echo &#39;Logfile of my backup&#39;
    rsync -av . /backup
    echo &quot;Backup finished with exit code $#&quot;
} &gt; backup.log 2&gt;&amp;1</code></pre>
Alexander Schoch's avatar
Alexander Schoch committed
<p>
  This redirects stdout and stderr of <em>all three commands</em> to a file
  called backup.log. Note that while this looks similar to subshells, it is not
  the same. Side effects that happen within the curly braces will still be
  present outside of them.
</p>
<h2 data-number="8.3" id="arithmetic-evaluation">
  <span class="header-section-number">8.3</span> Arithmetic Evaluation
</h2>
<p>
  So far, we’ve only been manipulating strings in bash. Sometimes, though, it is
  also necessary to manipulate numbers. This is done through arithmetic
  evaluation.
</p>
<p>
  Say you want to add the numbers 5 and 4. You might do something like this:
</p>
Alexander Schoch's avatar
Alexander Schoch committed
<pre><code>a=5+4</code></pre>
Alexander Schoch's avatar
Alexander Schoch committed
<p>
  However, this will result in the variable <code>a</code> containing the string
  <code>5+4</code>, rather than the number <code>9</code>. Instead, you should
  do this:
</p>
Alexander Schoch's avatar
Alexander Schoch committed
<pre><code>(( a=5+4 ))</code></pre>
Alexander Schoch's avatar
Alexander Schoch committed
<p>
  The double parenthesis indicate that something arithmetic is happening. In
  fact, <code>((</code> is a bash keyword, much like <code>[[</code>.
</p>
Alexander Schoch's avatar
Alexander Schoch committed
<p><code>((</code> can also be used to do arithmetic comparison:</p>
<pre><code>if (( 5 &gt; 9 ))
then
    echo &#39;5 is greater than 9&#39;
else 
    echo &#39;5 is not greater than 9&#39;
fi</code></pre>
Alexander Schoch's avatar
Alexander Schoch committed
<p>
  It is important not to confuse <code>((</code> and <code>[[</code>.
  <code>[[</code> is for comparing strings (among other things), while
  <code>((</code> is only for comparing numbers.
</p>
<p>
  There’s also <em>arithmetic substitution</em>, which works similarly to
  <em>command substitution</em>:
</p>
Alexander Schoch's avatar
Alexander Schoch committed
<pre><code>echo &quot;There are $(( 60 * 60 * 24 )) seconds in a day.&quot;</code></pre>
Alexander Schoch's avatar
Alexander Schoch committed
<h1 data-number="9" id="functions">
  <span class="header-section-number">9</span> Functions
</h1>
<p>
  Inside a bash script, functions are very handy. They are lists of
  commands–much like a normal script–except they don’t reside in their own file.
  They do however take arguments, just like scripts.
</p>
Alexander Schoch's avatar
Alexander Schoch committed
<p>Functions can be defined like this:</p>
<pre><code>sum() {
    echo &quot;$1 + $2 = $(( $1 + $2 ))&quot;
}</code></pre>
Alexander Schoch's avatar
Alexander Schoch committed
<p>
  If you put this in a script file and run it, absolutely nothing will happen.
  The function <code>sum</code> has been defined, but it is never used.
</p>
<p>
  You can use your function like any other command, but you have to define it
  <em>before</em> you use it:
</p>
Alexander Schoch's avatar
Alexander Schoch committed
<pre><code>sum() {
    echo &quot;$1 + $2 = $(( $1 + $2 ))&quot;
}
sum 1 2
sum 3 9
sum 6283 3141</code></pre>
Alexander Schoch's avatar
Alexander Schoch committed
<p>
  As you can see, you can use the function <code>sum</code> multiple times, but
  you only need to define it once. This is useful in larger scripts, where a
  certain task has to be performed multiple times. Whenever you catch yourself
  writing the same or very similar code twice in the same script, you should
  consider using a function.
</p>
<h1 data-number="10" id="useful-commands">
  <span class="header-section-number">10</span> Useful Commands
</h1>
<p>
  This chapter provides an overview of useful commands that you can use in your
  scripts. It is nowhere near complete, and serves only to provide a brief
  overview. If you want to know more about a specific command, you should read
  its manpage.
</p>
Alexander Schoch's avatar
Alexander Schoch committed
<p><strong>grep</strong></p>
Alexander Schoch's avatar
Alexander Schoch committed
<p>
  <code>grep</code> can be used to search for a string within a file, or within
  the output of a command.
</p>
Alexander Schoch's avatar
Alexander Schoch committed
<pre><code># searches logfile.txt for lines containing the word error
grep &#39;error&#39; logfile.txt  

# searches the directory &#39;folder&#39; for files 
# containing the word &#39;analysis&#39;
grep &#39;analysis&#39; folder/   

# searches the output of &#39;xrandr&#39; for lines that say &#39;connected&#39;. 
# only matches whole words, so &#39;disconnected&#39; will not match.
xrandr | grep -w &#39;connected&#39; </code></pre>
Alexander Schoch's avatar
Alexander Schoch committed
<p>
  <code>grep</code> returns 0 if it finds something, and returns an error if it
  doesn’t. This makes it useful for conditionals.
</p>
Alexander Schoch's avatar
Alexander Schoch committed
<p><strong>sed</strong></p>
Alexander Schoch's avatar
Alexander Schoch committed
<p>
  <code>sed</code> can be used to edit text “on the fly”. It uses its own
  scripting language to describe modifications to be made to the text, which
  makes it extremely powerful. Here, we provide examples for the most common
  usages of sed:
</p>
Alexander Schoch's avatar
Alexander Schoch committed
<pre><code># replaces the first occurrence of &#39;find&#39; in every line by &#39;replace&#39;
sed &#39;s/find/replace&#39; inputfile 

# replaces every occurrence of &#39;find&#39; in every line by &#39;replace&#39;
sed &#39;s/find/replace/g&#39; inputfile 

# deletes the first occurrence of &#39;find&#39; in every line
sed &#39;s/find//&#39; inputfile 

# deletes every occurrence of &#39;find&#39; in every line
sed &#39;s/find//g&#39; inputfile 

# displays only the 12th line
sed &#39;12q;d&#39; inputfile </code></pre>
Alexander Schoch's avatar
Alexander Schoch committed
<p>
  <code>sed</code> is often used in combination with pipes to format the output
  or get rid of unwanted characters.
</p>
Alexander Schoch's avatar
Alexander Schoch committed
<p><strong>curl and wget</strong></p>
Alexander Schoch's avatar
Alexander Schoch committed
<p>
  <code>curl</code> and <code>wget</code> are two commands that can be used to
  access websites or other content from the web. The difference is that
  <code>wget</code> will simply download the content to a file, while
  <code>curl</code> will output it to the console.
</p>
Alexander Schoch's avatar
Alexander Schoch committed
<pre><code>curl http://www.thealternative.ch
wget http://files.project21.ch/LinuxDays-Public/16FS-install-guide.pdf</code></pre>
<p><strong>xrandr</strong></p>
Alexander Schoch's avatar
Alexander Schoch committed
<p>
  <code>xrandr</code> can be used to manipulate your video outputs,
  i.e. enabling and disabling monitors or setting their screen resolution and
  orientation.
</p>
Alexander Schoch's avatar
Alexander Schoch committed
<pre><code># list all available outputs and their status info
xrandr  

# enables output HDMI-1
xrandr --output HDMI-1 --auto 

# puts output HDMI-1 to the left of output LVDS-1
xrandr --output HDMI-1 --left-of LVDS-1 

# disables output LVDS-1
xrandr --output LVDS-1 --off </code></pre>
<p><strong>ImageMagick (convert)</strong></p>
Alexander Schoch's avatar
Alexander Schoch committed
<p>
  The <code>convert</code> command makes it possible to do image processing from
  the commandline.
</p>
Alexander Schoch's avatar
Alexander Schoch committed
<pre><code># scale fullHDWallpaper.jpg to specified resolution
convert fullHDWallpaper.jpg -scale 3200x1800 evenBiggerWallpaper.jpg 

# &quot;automagically&quot; adjusts the gamma level of somePicture.jpg
convert somePicture.jpg -auto-gamma someOtherPicture.jpg 

# transform image to black and white
convert colorfulPicture.jpg -monochrome blackWhitePicture.jpg </code></pre>
Alexander Schoch's avatar
Alexander Schoch committed
<p>
  It is extremely powerful and has lots of options. A good resource is the
  <a href="http://www.imagemagick.org">official website</a>. It also provides
  examples for most options.
</p>
Alexander Schoch's avatar
Alexander Schoch committed
<p><strong>notify-send</strong></p>
Alexander Schoch's avatar
Alexander Schoch committed
<p>
  <code>notify-send</code> can be used to display a desktop notification with
  some custom text:
</p>
Alexander Schoch's avatar
Alexander Schoch committed
<pre><code>notify-send &#39;Battery warning&#39; &#39;Your battery level is below 10%&#39;</code></pre>
Alexander Schoch's avatar
Alexander Schoch committed
<p>
  The first argument is the notification’s title, the second is its description.
</p>
<p>
  <code>notify-send</code> requires a <em>notification daemon</em> to be
  running, else it won’t work. Most desktop environments come with a
  notification daemon set up and running. If you can’t see your notifications,
  it might be that you don’t have such a daemon.
</p>
Alexander Schoch's avatar
Alexander Schoch committed
<p><strong>find</strong></p>
<p><code>find</code> can be used to find files in a directory structure.</p>
<pre><code># finds all files in the current directory and all subdirectories that end
# in .png
find -name &#39;*.png&#39; 

# finds all files ending in .tmp and removes them. {} is replaced by the
# file&#39;s name when executing the command.
# Note that we don&#39;t use globbing here, but instead pass the * to find.
# find will then interpret the * as a wildcard.
find -name &#39;*.tmp&#39; -exec rm &#39;{}&#39;

# finds all files in the directory &#39;files&#39; and prints their size and path
find &#39;files/&#39; -printf &#39;%s %p\n&#39; </code></pre>
Alexander Schoch's avatar
Alexander Schoch committed
<p>
  <code>find</code> has many options that allow you to perform arbitrary actions
  on the files it found or pretty-print the output.
</p>
Alexander Schoch's avatar
Alexander Schoch committed
<p><strong>sort</strong></p>
Alexander Schoch's avatar
Alexander Schoch committed
<p>
  <code>sort</code> sorts lines of text files, or lines it reads from
  <em>stdin</em>.
</p>
Alexander Schoch's avatar
Alexander Schoch committed
<pre><code>sort listOfNames.txt # sorts all lines in listOfNames.txt alphabetically</code></pre>
<p><strong>head and tail</strong></p>
Alexander Schoch's avatar
Alexander Schoch committed
<p>
  <code>head</code> and <code>tail</code> can be used to show the beginning or
  the end of a long stream of text, respectively.
</p>
Alexander Schoch's avatar
Alexander Schoch committed
<pre><code># display the last few lines of the dmesg log
dmesg | tail 

# display only the first few lines of a very long text file
head verylongtext.txt </code></pre>
<p><strong>jshon</strong></p>
Alexander Schoch's avatar
Alexander Schoch committed
<p>
  <code>jshon</code> is a very simple command-line json parser. It can read data
  in json format and return specific values.
</p>
<p>
  Its most important options are <code>-e</code> and <code>-a</code>.
  <code>-e</code> extracts the value of a given key from a json array or object:
</p>
Alexander Schoch's avatar
Alexander Schoch committed
<pre><code># Find the object with key &quot;title&quot; from a json object stored in the file &quot;json&quot;
jshon -e &#39;title&#39; &lt; &#39;json&#39;</code></pre>
Alexander Schoch's avatar
Alexander Schoch committed
<p>
  The <code>-a</code> option maps all remaining options across the currently
  selected element. It has to be combined with other options, for example the
  <code>-e</code> option.
</p>
Alexander Schoch's avatar
Alexander Schoch committed
<pre><code># Find the names of all elements stored in the json object in file &quot;json&quot;
jshon -a -e &#39;name&#39; &lt; &#39;json&#39;</code></pre>
<p><strong>shuf</strong></p>
Alexander Schoch's avatar
Alexander Schoch committed
<p>
  <code>shuf</code> randomly permutates the lines of its input, effectively
  <em>shuffling</em> them.
</p>
Alexander Schoch's avatar
Alexander Schoch committed
<pre><code># Shuffle the lines of file &quot;playlist&quot;
shuf &#39;playlist&#39;

# Get a random line from file &quot;quotes&quot;
shuf -n 1 &#39;quotes&#39;</code></pre>
<p><strong>tee</strong></p>
<p><code>tee</code> takes input from <em>stdin</em> and writes it to a file:</p>
<pre><code>sudo zypper up | tee &#39;updatelog&#39;</code></pre>
<p>Note that this is equivalent to</p>
<pre><code>sudo zypper up &gt; &#39;updatelog&#39;</code></pre>
Alexander Schoch's avatar
Alexander Schoch committed
<p>
  <code>tee</code> is useful when you want to write to a file that requires root
  access. Then you can do the following:
</p>
Alexander Schoch's avatar
Alexander Schoch committed
<pre><code>echo &#39;some configuration&#39; | sudo tee &#39;/etc/systemconfig&#39;</code></pre>
Alexander Schoch's avatar
Alexander Schoch committed
<p>
  A normal redirection wouldn’t work in this case, as that would open the file
  as a normal user instead of root.
  <strong>Please be careful when modifying system files.</strong>
</p>
Alexander Schoch's avatar
Alexander Schoch committed
<p><strong>sleep</strong></p>
Alexander Schoch's avatar
Alexander Schoch committed
<p>
  <code>sleep</code> pauses the execution of your script for a number of
  seconds. It basically takes a number as an argument, then does nothing for
  that number of seconds, and then returns. It can be useful if you want to make
  your script do something in a loop, but want to throttle the speed a little.
</p>
Alexander Schoch's avatar
Alexander Schoch committed
<pre><code># prints &quot;Get back to work&quot; once per second. 
# Note that &#39;true&#39; is a command that always returns 0.
while true
do
    echo &#39;Get back to work!&#39;
    sleep 1
done</code></pre>
<p><strong>mplayer or mpv</strong></p>
Alexander Schoch's avatar
Alexander Schoch committed
<p>
  <code>mplayer</code> and <code>mpv</code> are media players that can be used
  from the console. <code>mpv</code> is based on <code>mplayer</code> and a lot
  more modern, but they can do essentially the same.
</p>
Alexander Schoch's avatar
Alexander Schoch committed
<pre><code># play file &quot;music.mp3&quot;
mplayer &#39;music.mp3&#39;
mpv &#39;music.mp3&#39;

# play file &quot;alarm.mp3&quot; in an infinite loop
mplayer -loop 0 &#39;alarm.mp3&#39;
mplayer --loop=inf &#39;alarm.mp3&#39;

# play a youtube video
# this only works with mpv and requires youtube-dl to be installed
mpv &#39;https://www.youtube.com/watch?v=lAIGb1lfpBw&#39;</code></pre>
<p><strong>xdotool</strong></p>
Alexander Schoch's avatar
Alexander Schoch committed
<p>
  <code>xdotool</code> can simulate keypresses. With this command, you can write
  macros that perform actions for you. It allows you to write scripts that
  interact with GUI programs.
</p>
Alexander Schoch's avatar
Alexander Schoch committed
<pre><code># press ctrl+s (in order to save a document)
xdotool key &#39;Control+s&#39;

# type &quot;hello&quot;
xdotool type &#39;hello&#39;</code></pre>

<!-- end pandoc -->