|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Hi,
Usually, to test the return of a command (success or failure), I use a sequence of two instructions: the instruction, and then the if test: $ touch bar $ ls bar $ if [ "$?" = 0 ];then echo success;fi success I would like to run the command between the brackets: is this possible? The following command does not seem to work: $ if [ `ls bar 1>/dev/null 2>&1` ];then echo success;fi Thanks in advance Julien -- "Allez, Monsieur, allez, et la foi vous viendra." (D'Alembert). |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
> I would like to run the command between the brackets: is this possible?
> The following command does not seem to work: > > $ if [ `ls bar 1>/dev/null 2>&1` ];then echo success;fi ls bar >/dev/null 2>&1 && echo success || echo failure Regards Dimitre |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
>> I would like to run the command between the brackets: is this possible? >> The following command does not seem to work: >> >> $ if [ `ls bar 1>/dev/null 2>&1` ];then echo success;fi > > ls bar >/dev/null 2>&1 && echo success || echo failure If you just want to check if the file exists: [ -e bar ] && echo success || echo failure Regards Dimitre |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
On Sun, 05 Nov 2006 16:39:22 +0100, Tribulations Parallèles
<Tribulations@Paralleles.invalid> wrote: > Hi, > > Usually, to test the return of a command (success or failure), I use a > sequence of two instructions: the instruction, and then the if test: > > $ touch bar > $ ls bar > $ if [ "$?" = 0 ];then echo success;fi > success > > I would like to run the command between the brackets: is this possible? > The following command does not seem to work: > > $ if [ `ls bar 1>/dev/null 2>&1` ];then echo success;fi > You don't need brackets. The left bracket itself is a command, a synonym for "test". (when it's called '[' it requires a ']') if ls bar 1>/dev/null 2>&1; then echo success; fi You could also write if [ -e bar ]; then echo success; fi By the way, `ls bar 1>/dev/null 2>&1` results in an empty string, which evaluates as false inside [ ]. -- When asked by an anthropologist what the Indians called America before the white men came, an Indian said simply "Ours." -- Vine Deloria, Jr. |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
2006-11-05, 16:39(+01), Tribulations Parallèles:
> Hi, > > Usually, to test the return of a command (success or failure), I use a > sequence of two instructions: the instruction, and then the if test: > > $ touch bar > $ ls bar > $ if [ "$?" = 0 ];then echo success;fi > success > > I would like to run the command between the brackets: is this possible? > The following command does not seem to work: > > $ if [ `ls bar 1>/dev/null 2>&1` ];then echo success;fi [...] if ls -d bar > /dev/null 2>&1; then echo success; fi if relies on the exit status of the list of commands inbetween "if" and "then" to decide whether to run the "then" command list or the "else" command list. "[" is just another command. "[ "$?" -eq 0 ]" is a noop as it just asks the "[" command to replicate the exit status (at least the meaning of the exit status) from the previously run command. IOW, in cmd if [ "$?" -eq 0 ]; [ "$?" -eq 0 ]; [ "$?" -eq 0 ]; then... All the "[" commands are useless as they just perpetrate $?. It should be: if cmd; then [ "$?" -eq 0 ] sets $? to 0 if $? was 0 and $? to 1 if it wasn't 0. Another way to perpetrate $?, could be (exit "$?"), that is start a subshell and make it exit with the $? exit status. Again, not very useful. -- Stéphane |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
Radoulov, Dimitre wrote:
>> I would like to run the command between the brackets: is this possible? >> The following command does not seem to work: >> >> $ if [ `ls bar 1>/dev/null 2>&1` ];then echo success;fi > > ls bar >/dev/null 2>&1 && echo success || echo failure Thanks a lot, it works. But is it possible to keep the ls instruction in a if instruction? Indeed, if there is a lot of instructions to execute in case of success, I am compelled to use parenthesis: ls bar 1>/dev/null 2>&1 && ( echo success echo foo echo coco ) || echo failure It works, but I am used to using "if" instruction, so I just want to know if it could be possible to keep the classical construction. Julien -- "Allez, Monsieur, allez, et la foi vous viendra." (D'Alembert). |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
[...] > Indeed, if there is a lot of instructions to execute in case of success, I > am compelled to use parenthesis: > > ls bar 1>/dev/null 2>&1 && ( > echo success > echo foo > echo coco > ) || > echo failure > > It works, but I am used to using "if" instruction, so I just want to know > if > it could be possible to keep the classical construction. [...] if ls bar > /dev/null 2>&1 ; then echo success echo foo echo coco fi Regards Dimitre |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
2006-11-5, 11:00(-05), Bill Marcum:
[...] > You don't need brackets. The left bracket itself is a command, a > synonym for "test". (when it's called '[' it requires a ']') > > if ls bar 1>/dev/null 2>&1; then echo success; fi > > You could also write > if [ -e bar ]; then echo success; fi It should be noted though that the two commands above are not equivalent if "bar" is a directory or a symbolic link whose target we can't determine the existence. > By the way, `ls bar 1>/dev/null 2>&1` results in an empty string, which > evaluates as false inside [ ]. [...] Actually, as it is not quoted, it results in no argument at all (which is not the same thing as an empty argument) but in any case, "[" returns false for both [ ] and [ "" ]. -- Stéphane |
|
![]() |
| Outils de la discussion | |
|
|