Tag Info

New answers tagged

5

If you want portability outside the Bourne-like world, then: test -w /home/durrantm && echo writable is the most portable. It works in shells of the Bourne, csh and rc families. test -w /home/durrantm && echo "writable" would output "writable" instead of writable in shells of the rc family (rc, es, akanga, where " is not special). [ -w ...


0

My most important reason for choosing either if foo; then bar; fi or foo && bar is whether the exit status of the whole command is important. compare: #!/bin/sh set -e foo && bar do_baz with: #!/bin/sh set -e if foo; then bar; fi do_baz You might think they do the same; however if foo fails (or is false, depending on your point of ...


8

It's actually the && that is replacing the if, not the test: an if statement in shell scripting tests whether a command returned a "successful" (zero) exit status; in your example, the command is [. So, there are actually two things you are varying here: the command used to run the test, and the syntax used to execute code based on the result of ...


13

Please note, that [] && cmd is not the same as if .. fi construction. Sometimes its behaviour its pretty similar and you can use [] && cmd instead of if .. fi. But only sometimes. If you have more then one command to execute if condition or you need if .. else .. fi be careful and whatch the logic. A couple of examples: [ -z "$VAR" ] ...


6

For portability, use test / [. But if you don't need the portability, for the sake of the sanity of yourself and others reading your script use [[. :) Also see What is the difference between test, [ and [[ ? in the BashFAQ.


27

Yes, there are differences. The most portable are test or [ ]. These are both part of the POSIX test specification. The if ... fi construct is also defined by POSIX and should be completely portable. The [[ ]] is a ksh feature that is also present in some versions of bash (all modern ones), in zsh and perhaps in others but is not present in sh or dash or ...


21

[ is synonym of the test command and it is simultaneously a bash builtin and separate command. But [[ is a bash keyword and is workable in some versions only. So for reasons of portability you are better to use single [] or test [ -w "/home/durrantm" ] && echo "writable"


0

To open a file, you use a construct like the following: int fd; if ((fd = open(path, flags)) < 0) { /* An error occurred, the reason is in errno */ int _errno = errno; /* Save errno value */ fprintf(stderr, "Failed opening file '%s': %s\n", path, strerror(_errno)); return; } /* The file was successfully opened */ So, you have only one ...


1

The only rule for exit codes is that 0 means success and any other value means failure. This rule goes beyond unix: it's also a very convention on other operating systems (including DOS, Windows, and many embedded systems that have a notion of exit code, but VMS does things differently). In unix systems, it's baked into the shell's boolean constructs (if, ...


2

The documentation is incomplete. The code contains the following list of error codes used internally: enum xtables_exittype { OTHER_PROBLEM = 1, PARAMETER_PROBLEM, VERSION_PROBLEM, RESOURCE_PROBLEM, XTF_ONLY_ONCE, XTF_NO_INVERT, XTF_BAD_VALUE, XTF_ONE_ACTION, }; And when it tries to initialize, it does: if (!*handle) ...



Top 50 recent answers are included