<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<atom:link href="https://dev1galaxy.org/extern.php?action=feed&amp;tid=1808&amp;type=rss" rel="self" type="application/rss+xml" />
		<title><![CDATA[Dev1 Galaxy Forum / [Solved] Bash: Behavior of `test -x`]]></title>
		<link>https://dev1galaxy.org/viewtopic.php?id=1808</link>
		<description><![CDATA[The most recent posts in [Solved] Bash: Behavior of `test -x`.]]></description>
		<lastBuildDate>Thu, 04 Jan 2018 21:19:06 +0000</lastBuildDate>
		<generator>FluxBB</generator>
		<item>
			<title><![CDATA[Re: [Solved] Bash: Behavior of `test -x`]]></title>
			<link>https://dev1galaxy.org/viewtopic.php?pid=6982#p6982</link>
			<description><![CDATA[<div class="quotebox"><cite>ralph.ronnquist wrote:</cite><blockquote><div><p>The explanation is in how the bash expands commands, and how &quot;-x&quot; works.</p><p>Without quotes, a phrase like </p><div class="codebox"><pre><code>[ -x $(type -p gedit)]</code></pre></div><p> gets &quot;expanded&quot; into the following phrase when &quot;gedit&#039; is missing (aka &quot;type&quot; returns nothing): </p><div class="codebox"><pre><code>[-x  ]</code></pre></div><p> i.e., &quot;-x&quot; without argument, and that succeeds. Whereas with quotes around it, the expansion is </p><div class="codebox"><pre><code>[ -x &quot;&quot; ]</code></pre></div><p>i.e. &quot;-x&quot; with an empty string as argument, and that fails.</p></div></blockquote></div><p>I see. Thanks for explaining that.</p><div class="quotebox"><cite>ralph.ronnquist wrote:</cite><blockquote><div><p>Perhaps your statement should have been like </p><div class="codebox"><pre><code>if type -p $program &gt; /dev/null ; then echo UGH ; else echo NÖF; fi</code></pre></div><p> That would be using the return code of &quot;type&quot; as condition, with 0 meaning success and non-0 meaning fail.</p></div></blockquote></div><p>The problem with this is that, according to <span class="bbc">man bash</span>, <span class="bbc">type -p</span> only... </p><div class="quotebox"><blockquote><div><p>returns the name of the disk file that would be executed if <span class="bbu">name</span> were specified as a command name, or nothing if ``type -t name&#039;&#039; would not return <span class="bbu">file</span>.</p></div></blockquote></div><p> In other words, it just checks if <span class="bbc">$program</span> is in your <span class="bbc">$PATH</span>, not if it is executable.</p><p>So, the right way to perform that check would be to use <span class="bbc">text -x</span>, but with proper quoting:</p><div class="codebox"><pre><code>if [ -x &quot;$(type -p &quot;$program&quot;)&quot; ]
then
  [commands]
else
  [commands]
fi</code></pre></div>]]></description>
			<author><![CDATA[dummy@example.com (msi)]]></author>
			<pubDate>Thu, 04 Jan 2018 21:19:06 +0000</pubDate>
			<guid>https://dev1galaxy.org/viewtopic.php?pid=6982#p6982</guid>
		</item>
		<item>
			<title><![CDATA[Re: [Solved] Bash: Behavior of `test -x`]]></title>
			<link>https://dev1galaxy.org/viewtopic.php?pid=6958#p6958</link>
			<description><![CDATA[<p>The explanation is in how the bash expands commands, and how &quot;-x&quot; works.</p><p>Without quotes, a phrase like </p><div class="codebox"><pre><code>[ -x $(type -p gedit)]</code></pre></div><p> gets &quot;expanded&quot; into the following phrase when &quot;gedit&#039; is missing (aka &quot;type&quot; returns nothing): </p><div class="codebox"><pre><code>[-x  ]</code></pre></div><p> i.e., &quot;-x&quot; without argument, and that succeeds. Whereas with quotes around it, the expansion is </p><div class="codebox"><pre><code>[ -x &quot;&quot; ]</code></pre></div><p>i.e. &quot;-x&quot; with an empty string as argument, and that fails.</p><p>Perhaps your statement should have been like </p><div class="codebox"><pre><code>if type -p $program &gt; /dev/null ; then echo UGH ; else echo NÖF; fi</code></pre></div><p> That would be using the return code of &quot;type&quot; as condition, with 0 meaning success and non-0 meaning fail.</p>]]></description>
			<author><![CDATA[dummy@example.com (ralph.ronnquist)]]></author>
			<pubDate>Wed, 03 Jan 2018 00:06:22 +0000</pubDate>
			<guid>https://dev1galaxy.org/viewtopic.php?pid=6958#p6958</guid>
		</item>
		<item>
			<title><![CDATA[Re: [Solved] Bash: Behavior of `test -x`]]></title>
			<link>https://dev1galaxy.org/viewtopic.php?pid=6952#p6952</link>
			<description><![CDATA[<div class="quotebox"><cite>fsmithred wrote:</cite><blockquote><div><p>I&#039;l take a guess and say that -x is testing the executability of the command substitution. When you quote it, you&#039;re telling the shell to expand what&#039;s inside the quotes, so -x tests the result of that.</p></div></blockquote></div><p>That sounds like a reasonable explanation, though I&#039;m not sure it&#039;s accurate. But a look at exit codes also points into that direction:</p><div class="codebox"><pre><code>$ test -x /usr/bin/whoami
$ echo $?
0
$ test -x /usr/bin/whoarethey
$ echo $?
1
$ test -x $(type -p whoami)
$ echo $?
0
$ test -x $(type -p whoarethey)
$ echo $?
0
$ test -x &quot;$(type -p whoami)&quot;
$ echo $?
0
$ test -x &quot;$(type -p whoarethey)&quot;
$ echo $?
1</code></pre></div>]]></description>
			<author><![CDATA[dummy@example.com (msi)]]></author>
			<pubDate>Tue, 02 Jan 2018 19:16:31 +0000</pubDate>
			<guid>https://dev1galaxy.org/viewtopic.php?pid=6952#p6952</guid>
		</item>
		<item>
			<title><![CDATA[Re: [Solved] Bash: Behavior of `test -x`]]></title>
			<link>https://dev1galaxy.org/viewtopic.php?pid=6949#p6949</link>
			<description><![CDATA[<p>Well, without the quotes it tells me that gedit is found/executable, but gedit is not installed here. I&#039;l take a guess and say that -x is testing the executability of the command substitution. When you quote it, you&#039;re telling the shell to expand what&#039;s inside the quotes, so -x tests the result of that.</p><p>For more complication, try it with double-brackets for test: </p><div class="codebox"><pre><code>if [[ -x $(type -p gedit) ]]; then echo &quot;found executable&quot;; else echo &quot;command not found/not an executable&quot;; fi
command not found/not an executable</code></pre></div>]]></description>
			<author><![CDATA[dummy@example.com (fsmithred)]]></author>
			<pubDate>Tue, 02 Jan 2018 14:48:03 +0000</pubDate>
			<guid>https://dev1galaxy.org/viewtopic.php?pid=6949#p6949</guid>
		</item>
		<item>
			<title><![CDATA[[Solved] Bash: Behavior of `test -x`]]></title>
			<link>https://dev1galaxy.org/viewtopic.php?pid=6948#p6948</link>
			<description><![CDATA[<p>Trying to implement a check for the availability of a given text editor inside a bash script, I discovered that <em>test -x</em> needs its argument to be put in quotes if the argument is a command substitution. Maybe someone can exlplain to me why.</p><p>Here&#039;s an example of what happens if you don&#039;t put the quotes:</p><div class="codebox"><pre><code>$ type -p nano  # nano is installed
/usr/bin/nano
$ type -p gedit  # gedit is not installed
$
$if [ -x $(type -p nano) ]; then echo &quot;found executable&quot;; else echo &quot;command not found/not an executable&quot;; fi
found executable
$ if [ -x $(type -p gedit) ]; then echo &quot;found executable&quot;; else echo &quot;command not found/not an executable&quot;; fi
found executable  # This is strange.
$
$ if [ -x &quot;$(type -p nano)&quot; ]; then echo &quot;found executable&quot;; else echo &quot;command not found/not an executable&quot;; fi
found executable
$ if [ -x &quot;$(type -p gedit)&quot; ]; then echo &quot;found executable&quot;; else echo &quot;command not found/not an executable&quot;; fi
command not found/not an executable</code></pre></div>]]></description>
			<author><![CDATA[dummy@example.com (msi)]]></author>
			<pubDate>Tue, 02 Jan 2018 12:43:58 +0000</pubDate>
			<guid>https://dev1galaxy.org/viewtopic.php?pid=6948#p6948</guid>
		</item>
	</channel>
</rss>
