11

Is it any better way to get return code from command in one line. eg:

$ test $(ls -l) ||echo 'ok'
-bash: test: too many arguments
ok

the above script have error in test command, because it seems parsing the output "ls - l" not return code.

I know use the "if" syntax is work fine, But need more then one lines.

ls -l
if [ $? -eq 0 ];then
   echo 'ok'
fi
Jens
  • 69,818
  • 15
  • 125
  • 179
Robber Pen
  • 1,033
  • 2
  • 12
  • 24
  • It is quite unclear what you are asking. Mind to [edit] and specify what is the exact problem you are facing? – fedorqui May 03 '16 at 11:46
  • 1
    You misunderstand `test` command. There is no need to use it to simply check return code. One-liner equivalent for second piece of code is `ls -l && echo 'ok'`. – gudok May 03 '16 at 11:59
  • See also [Why is testing "$?" to see if a command succeeded or not, an anti-pattern?](https://stackoverflow.com/questions/36313216/why-is-testing-to-see-if-a-command-succeeded-or-not-an-anti-pattern) – tripleee Jun 02 '23 at 08:13

4 Answers4

6

You can use && and || to make these things one-liner. For example, in the following:

ls -l && echo ok

echo ok will run only if the command before && (ls -l) returned 0.

On the other hand, in the following:

ls -l || echo 'not ok'

echo 'not ok' will run only if the command before || returned non zero.

Also, you can make your if..else block one-liner using ;:

if ls -l;then echo ok;else echo 'not ok';fi

But this may make your code hard to read, so not recommended.

Jahid
  • 21,542
  • 10
  • 90
  • 108
2

The if statement is catching the return value of a command, for example with ls:

if ls -l; then
    echo 'ok'
fi
oliv
  • 12,690
  • 25
  • 45
-1

Another question here is how to wor below sample.

ls -l xxx || (echo "file xxx not exist" ;  exit 1);echo "OK"

As the sample. If file xxx does not exist. the 2nd echo OK still work even exit 1 previously.

I expect to exit return code 1 if file xxx does not exist.

Robber Pen
  • 1,033
  • 2
  • 12
  • 24
-3

instead of using

test $(ls -l) ||echo 'ok'

you should use

[[ $(ls -l) ]] && echo "ok"

[[ ]] is test and if the command returns successfully (return code of zero) then it executes the command after the &&.

  • [[ ]] test
  • $( ) execute command
  • ls -l command to run
  • && run if test is successful

Hope this helps!

7ochem
  • 2,183
  • 1
  • 34
  • 42
jgshawkey
  • 2,034
  • 1
  • 9
  • 8
  • 2
    The command execution `$(...)` makes this check for the output text, not for the return code. Check this for example: `[[ $(echo abc && false) ]] && echo ok || echo no` - it will print out "ok" – viraptor May 03 '16 at 12:37
  • @viraptor, Thank you for the comments. The answer above checks for the return value and not the returned text of the command. You can see this by typing in `[[ $(ls -l dirDoesNotExist ) ]] && echo "ok" || echo "not ok"` and you will receive the "not ok" response. The command you included `[[ $(echo abc && false) ]]` successfully executes so it returns a return code of zero as expected. Let me know if you still disagree and we'll continue to pursue it. Thanks! – jgshawkey May 03 '16 at 13:31
  • 1
    I just want to add something here, in the short hand if statement using && and || does not equate directly to an if , else. If the command after && fails, the || clause will also run. Example: [[ true ]] && { echo "true"; false; } || echo "false" – Martin May 03 '16 at 14:23
  • @Martin, you are correct. Thank you for clarifying. – jgshawkey May 03 '16 at 14:43
  • 1
    @jgshawkey That's not why it fails in case of dirDoesNotExist. In that case the error message is printed to stderr, so there's no standard output and that's why the check fails. You can see that `[[ $(ls -l dirDoesNotExist 2>&1 ) ]] && echo "ok" || echo "not ok"` succeeds. Without redirection, this is equivalent to `[[ "" ]] && echo ...` – viraptor May 03 '16 at 22:42
  • @viraptor. Thank you for taking the time to explain. I see where I was wrong now. – jgshawkey May 04 '16 at 00:55
  • the answer is so clear, and it work fine as my expect. – Robber Pen May 04 '16 at 10:35
  • ls -l xxx && (echo "file xxx not exist" ; exit 1) – Robber Pen May 04 '16 at 11:59