Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Welcome to bash programming. :-)</p> <p>First off, I'll refer you to the <a href="http://mywiki.wooledge.org/BashFAQ" rel="nofollow">Bash FAQ</a>. Great resource, lots of tips, perspectives and warnings.</p> <p>One of them is the classic <strong><a href="http://mywiki.wooledge.org/ParsingLs" rel="nofollow">Parsing LS</a></strong> problem that your script suffers from. The basic idea is that you don't want to trust the output of the <code>ls</code> command, because special characters like spaces and control characters may be represented in a way that doesn't allow you to <em>refer</em> to the file.</p> <p>You're opening the "last" file, as determined by a sort that the <code>ls</code> command is doing. In order to detect the most recent file <em>without</em> <code>ls</code>, we'll need some extra code. For example:</p> <pre><code>#!/bin/sh last=0 for filename in ~/Downloads/*; do when=$(stat -c '%Y' "$filename") if [ $when -gt $last ]; then last=$when to_open="$filename" fi done xdg-open "$to_open" </code></pre> <p>The idea is that we'll walk through each file in your <code>Downloads</code> directory and fine the one with the largest timestamp using the <code>stat</code> command. Then open that file using <code>xdg-open</code>, which may already be installed on your system because it's part of a tool set that's a dependency for a number of other applications.</p> <p>If you don't have xdg-open, you can probably install it from the <strong>xdg-utils</strong> package which using whatever package management system is around for your Linux distro.</p> <p>Another possibility is <code>gnome-open</code>, which is part of the Gnome desktop (the <code>libgnome</code> package, to be precise). YMMV. We'd need to know more about your distro and your desktop environment to come up with better advice.</p> <p>Note that if you <em>do</em> want to continue selecting your application by extension, you might want to consider using a switch instead of a series of <code>if</code>s:</p> <pre><code>... case "${filename##*.}" in txt) leafpad "$filename" ;; pdf) xdg-open "$filename" ;; *) echo "ERROR: can't open '$filename'" &gt;&amp;2 ;; esac </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload